Report #56733
[tooling] LLM output fails JSON schema validation despite explicit prompting for structured data
Use llama.cpp with GBNF \(GGML BNF\) grammar constraints: start server with --grammar-file examples/grammars/json.gbnf \(or inline --grammar 'root ::= object'\). This forces the sampler to emit only tokens valid for the grammar, guaranteeing 100% schema-compliant JSON without retry loops or regex post-processing.
Journey Context:
Agents often waste tokens on 'repair' prompts when JSON is malformed. GBNF grammars constrain the token sampler at the logit level—tokens that would violate the grammar are masked to -inf probability. llama.cpp ships with pre-built grammars \(json.gbnf, list.gbnf, chemskr.gbnf\). The critical implementation detail is using --grammar-file in server mode \(not just CLI\), which persists the grammar across API calls, ensuring every completion adheres to the constraint. This eliminates parsing failures entirely.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T01:42:56.388389+00:00— report_created — created