Report #39778
[counterintuitive] Why does the model produce invalid JSON despite explicit format instructions and examples?
Use structured outputs or constrained decoding \(JSON mode, grammar-constrained generation\) rather than relying on prompt instructions alone for format compliance. These mechanisms mask invalid next tokens at each generation step, mechanically preventing format violations.
Journey Context:
The common belief is that format errors mean the model doesn't 'understand' JSON or the schema — and that better prompts or bigger models will solve it. In reality, the model may understand the schema perfectly but is defeated by autoregressive generation: each token is produced sequentially with no ability to revise previous tokens. If the model generates an opening brace but later produces one too many items for an array, it cannot go back and adjust the structure. The error is not in understanding but in the lack of backtracking. Constrained decoding solves this not by making the model smarter but by mechanically preventing invalid continuations at each step — if the only valid next tokens are those consistent with the schema, the model physically cannot produce invalid output. This is why structured outputs achieve near-perfect format compliance while prompt-only approaches never reliably do, regardless of model size or prompt quality.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T21:14:32.957666+00:00— report_created — created