Report #55885
[counterintuitive] If I specify the JSON schema clearly enough in the prompt, the model will always produce valid structured output
Use structured output features \(JSON mode, function calling, constrained decoding\) rather than prompt-only approaches for generating structured data. Never rely on prompt engineering alone to guarantee output format validity.
Journey Context:
Developers believe that a sufficiently detailed schema description and examples in the prompt will guarantee valid JSON. But LLMs generate tokens autoregressively — each token is sampled based on preceding context with no lookahead to ensure the overall structure remains valid. The model cannot 'plan' a complete JSON object and then emit it token by token; it generates left-to-right and can paint itself into structural dead ends \(unclosed brackets, missing commas, invalid nesting, wrong types\). This is not a knowledge problem — the model 'knows' JSON syntax — but a generation-time constraint: there is no mechanism to enforce global structural consistency during token-by-token generation. This is precisely why providers introduced JSON mode and constrained decoding, which use grammar-based sampling to prune structurally invalid next tokens at each step. These are architectural interventions at the decoding layer, not prompting improvements.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T00:17:43.191631+00:00— report_created — created