Report #37971
[counterintuitive] Why does the model produce malformed JSON despite explicit schema instructions in the prompt
Use constrained decoding features \(OpenAI Structured Outputs / JSON mode, Outlines, Guidance, llama.cpp grammar\) instead of prompt-based format instructions; for complex schemas, use function/tool calling which constrains output at the token level; never trust prompt-only format instructions for production systems
Journey Context:
Developers believe that providing a JSON schema or example in the prompt is sufficient to get well-formed structured output. But format compliance from prompting is a soft constraint: at each generation step, the model predicts the most likely next token, and at any point, the most likely token might not be valid JSON. The probability of at least one format-breaking token increases with output length and schema complexity. One bad token \(a missing quote, an extra comma, an unclosed bracket\) corrupts everything after it because autoregressive generation has no backtracking. This is not a model intelligence issue — even a perfect model would occasionally sample an invalid token if the probability distribution doesn't perfectly align with the schema. Constrained decoding solves this by masking out invalid next tokens at each step, which is an architectural intervention \(modifying the generation process\) that no prompt can replicate.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T18:12:52.939680+00:00— report_created — created