File size: 12,000 Bytes
664fbed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/usr/bin/env python3
"""Regression / reliability check for structured (schema-constrained) output.

Exercises a llama.cpp-compatible /v1/chat/completions endpoint (either the
llama-server directly, or through the FastAPI gateway in this repo) with:

  A. an ordinary, unconstrained completion
  B. response_format={"type": "json_object"}
  C. response_format={"type": "json_schema", ...} with a simple schema
  D. the same, with a realistic tool-call repair schema, fed a malformed
     tool-call and checked against the expected repaired structure
  E. test D repeated 10x, checking for zero sampler-initialization failures

Usage:
    python3 scripts/validate_structured_output.py --base-url http://127.0.0.1:8080/v1
    python3 scripts/validate_structured_output.py \\
        --base-url https://leon4gr45-minicpm.hf.space/v1 --api-key-env API_KEY

The API key (if any) is read from the environment variable named by
--api-key-env and is never printed or logged.
"""

from __future__ import annotations

import argparse
import json
import os
import sys
import time
import urllib.error
import urllib.request
from dataclasses import dataclass, field

SCHEMA_C = {
    "type": "object",
    "properties": {
        "repairable": {"type": "boolean"},
        "name": {"type": "string"},
        "arguments": {"type": "object"},
    },
    "required": ["repairable"],
    "additionalProperties": False,
}

SCHEMA_D = {
    "type": "object",
    "properties": {
        "repairable": {"type": "boolean"},
        "name": {"type": "string", "enum": ["bash"]},
        "arguments": {
            "type": "object",
            "properties": {"command": {"type": "string"}},
            "required": ["command"],
            "additionalProperties": False,
        },
    },
    "required": ["repairable", "name", "arguments"],
    "additionalProperties": False,
}

MALFORMED_TOOL_CALL = '<|tool_call>call:bash{command:<|"|>ls -la<|"|>}<tool_call|>'

REPAIR_SYSTEM_PROMPT = (
    "You repair malformed tool calls. A tool call is repairable if its "
    "intended tool name and arguments can be confidently recovered from the "
    "malformed text, even if the syntax (quotes, brackets, tags) is wrong. "
    "Given a malformed tool call, output the repaired call as JSON matching "
    "the schema."
)


@dataclass
class CallResult:
    ok: bool
    status: int
    body: str
    latency_s: float
    content: str | None = None
    parsed: object | None = None
    error: str | None = None
    usage: dict = field(default_factory=dict)
    timings: dict = field(default_factory=dict)


def call(base_url: str, api_key: str | None, payload: dict, timeout: float = 60.0) -> CallResult:
    url = base_url.rstrip("/") + "/chat/completions"
    headers = {"Content-Type": "application/json"}
    if api_key:
        headers["Authorization"] = f"Bearer {api_key}"
    data = json.dumps(payload).encode()
    req = urllib.request.Request(url, data=data, headers=headers, method="POST")

    start = time.monotonic()
    try:
        with urllib.request.urlopen(req, timeout=timeout) as resp:
            body = resp.read().decode()
            status = resp.status
    except urllib.error.HTTPError as exc:
        body = exc.read().decode()
        status = exc.code
    except urllib.error.URLError as exc:
        return CallResult(ok=False, status=0, body="", latency_s=time.monotonic() - start, error=str(exc))
    latency_s = time.monotonic() - start

    if status != 200:
        return CallResult(ok=False, status=status, body=body, latency_s=latency_s, error=f"HTTP {status}: {body}")

    try:
        response_json = json.loads(body)
        content = response_json["choices"][0]["message"]["content"]
        usage = response_json.get("usage", {})
        timings = response_json.get("timings", {})
    except (KeyError, IndexError, json.JSONDecodeError) as exc:
        return CallResult(ok=False, status=status, body=body, latency_s=latency_s, error=f"malformed response envelope: {exc}")

    return CallResult(ok=True, status=status, body=body, latency_s=latency_s, content=content, usage=usage, timings=timings)


def try_parse_json(text: str):
    try:
        return json.loads(text)
    except json.JSONDecodeError:
        # Some models wrap JSON in ```json fences despite instructions not to.
        stripped = text.strip()
        if stripped.startswith("```"):
            stripped = stripped.strip("`")
            if stripped.lower().startswith("json"):
                stripped = stripped[4:]
            try:
                return json.loads(stripped.strip())
            except json.JSONDecodeError:
                return None
        return None


def validates_schema_c(parsed) -> bool:
    return isinstance(parsed, dict) and isinstance(parsed.get("repairable"), bool)


def validates_schema_d_repair(parsed) -> bool:
    if not isinstance(parsed, dict):
        return False
    if parsed.get("repairable") is not True:
        return False
    if parsed.get("name") != "bash":
        return False
    args = parsed.get("arguments")
    return isinstance(args, dict) and args.get("command") == "ls -la"


def test_ordinary_completion(base_url: str, api_key: str | None, model: str) -> tuple[bool, str]:
    payload = {"model": model, "stream": False, "messages": [{"role": "user", "content": "Say hi in one word."}]}
    result = call(base_url, api_key, payload)
    if not result.ok:
        return False, result.error or "unknown failure"
    if not result.content or not result.content.strip():
        return False, "empty assistant content"
    return True, f"latency={result.latency_s:.2f}s content={result.content!r}"


def test_json_object(base_url: str, api_key: str | None, model: str) -> tuple[bool, str]:
    payload = {
        "model": model,
        "stream": False,
        "messages": [{"role": "user", "content": 'Return {"ok":true} as JSON.'}],
        "response_format": {"type": "json_object"},
    }
    result = call(base_url, api_key, payload)
    if not result.ok:
        return False, result.error or "unknown failure"
    parsed = try_parse_json(result.content)
    if parsed is None or not isinstance(parsed, dict):
        return False, f"not valid JSON object: {result.content!r}"
    return True, f"latency={result.latency_s:.2f}s parsed={parsed}"


def test_json_schema_simple(base_url: str, api_key: str | None, model: str) -> tuple[bool, str]:
    payload = {
        "model": model,
        "stream": False,
        "messages": [{"role": "user", "content": "Return a JSON object describing whether this is repairable."}],
        "response_format": {
            "type": "json_schema",
            "json_schema": {"name": "tool_repair_simple", "schema": SCHEMA_C},
        },
    }
    result = call(base_url, api_key, payload)
    if not result.ok:
        return False, result.error or "unknown failure"
    parsed = try_parse_json(result.content)
    if parsed is None:
        return False, f"not valid JSON: {result.content!r}"
    if not validates_schema_c(parsed):
        return False, f"does not validate against schema: {parsed!r}"
    return True, f"latency={result.latency_s:.2f}s parsed={parsed}"


def build_repair_payload(model: str) -> dict:
    return {
        "model": model,
        "stream": False,
        # Greedy decoding: this is a verifier/repair role where deterministic,
        # repeatable output matters more than sampling diversity, and it
        # keeps this reliability check meaningful (a schema grammar can force
        # *valid* JSON at any temperature, but whether the model's content
        # judgement is repeatable is a separate, real property to check).
        "temperature": 0,
        "messages": [
            {"role": "system", "content": REPAIR_SYSTEM_PROMPT},
            {"role": "user", "content": f"Repair this malformed tool call:\n{MALFORMED_TOOL_CALL}"},
        ],
        "response_format": {
            "type": "json_schema",
            "json_schema": {"name": "tool_repair", "strict": True, "schema": SCHEMA_D},
        },
    }


def test_json_schema_repair(base_url: str, api_key: str | None, model: str) -> tuple[bool, str, CallResult | None]:
    payload = build_repair_payload(model)
    result = call(base_url, api_key, payload)
    if not result.ok:
        return False, result.error or "unknown failure", result
    parsed = try_parse_json(result.content)
    if parsed is None:
        return False, f"not valid JSON: {result.content!r}", result
    if not validates_schema_d_repair(parsed):
        return False, f"does not validate against repair schema: {parsed!r}", result
    return True, f"latency={result.latency_s:.2f}s parsed={parsed}", result


def test_repeated_repair(base_url: str, api_key: str | None, model: str, n: int = 10) -> tuple[bool, str]:
    payload = build_repair_payload(model)
    passed = 0
    sampler_failures = 0
    details = []
    for i in range(n):
        result = call(base_url, api_key, payload)
        if not result.ok:
            details.append(f"#{i}: HTTP {result.status} {result.error}")
            if result.error and "sampler" in result.error.lower():
                sampler_failures += 1
            continue
        parsed = try_parse_json(result.content)
        if parsed is None or not validates_schema_d_repair(parsed):
            details.append(f"#{i}: schema-invalid content={result.content!r}")
            continue
        passed += 1
    ok = passed == n
    summary = f"{passed}/{n} passed, {sampler_failures} sampler-init failures"
    if details:
        summary += "; " + "; ".join(details[:5])
    return ok, summary


def main() -> int:
    parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument("--base-url", required=True, help="Base URL including /v1, e.g. http://127.0.0.1:8080/v1")
    parser.add_argument("--model", default="minicpm4-0.5b")
    parser.add_argument("--api-key-env", default=None, help="Env var name holding a bearer API key (never printed)")
    parser.add_argument("--repeat", type=int, default=10, help="Number of repair calls for the reliability test")
    args = parser.parse_args()

    api_key = os.environ.get(args.api_key_env) if args.api_key_env else None

    results: list[tuple[str, bool, str]] = []

    ok, detail = test_ordinary_completion(args.base_url, api_key, args.model)
    results.append(("ordinary_completion", ok, detail))

    ok, detail = test_json_object(args.base_url, api_key, args.model)
    results.append(("json_object", ok, detail))

    ok, detail = test_json_schema_simple(args.base_url, api_key, args.model)
    results.append(("json_schema_simple", ok, detail))

    ok, detail, last_result = test_json_schema_repair(args.base_url, api_key, args.model)
    results.append(("json_schema_repair", ok, detail))

    ok, detail = test_repeated_repair(args.base_url, api_key, args.model, n=args.repeat)
    results.append((f"repeated_repair_{args.repeat}x", ok, detail))

    print()
    width = max(len(name) for name, _, _ in results) + 4
    failures = 0
    for name, ok, detail in results:
        status = "PASS" if ok else "FAIL"
        if not ok:
            failures += 1
        print(f"{name.ljust(width)}{status}")
        if not ok or "--verbose" in sys.argv:
            print(f"{'':{width}}{detail}")

    if last_result is not None and last_result.ok:
        print()
        print("Performance (last repair call):")
        print(f"  prompt_tokens     = {last_result.usage.get('prompt_tokens')}")
        print(f"  completion_tokens = {last_result.usage.get('completion_tokens')}")
        print(f"  prompt_ms         = {last_result.timings.get('prompt_ms')}")
        print(f"  predicted_ms      = {last_result.timings.get('predicted_ms')}")
        print(f"  wall_time_s       = {last_result.latency_s:.3f}")

    print()
    print(f"failures: {failures}")
    return 1 if failures else 0


if __name__ == "__main__":
    raise SystemExit(main())