Report #103562
[tooling] Local model returns malformed JSON or invents schema fields, forcing regex cleanup
Pass response\_format with type=json\_schema/schema to llama-server /v1/chat/completions, or pass grammar to /completion. The server converts the schema to a GBNF grammar and masks the logits at each step, guaranteeing syntactic validity. For schemas the converter cannot handle, pre-convert with examples/json\_schema\_to\_grammar.py and send the grammar field directly.
Journey Context:
Prompting for JSON and parsing the output is fragile: small models emit trailing prose, wrong quotes, or hallucinated keys. llama.cpp's grammar sampler constrains the token distribution so only tokens that keep the output in grammar are selectable. The OpenAI-compatible endpoint accepts response\_format with a JSON schema, while the native /completion endpoint accepts a grammar string. The schema converter supports a subset of JSON Schema; unsupported features include additionalProperties, integer min/max constraints, remote $ref, and some PCRE shorthands. Pre-converting with the shipped Python script avoids runtime surprises and lets you inspect the GBNF.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-11T04:36:33.924609+00:00— report_created — created