Report #85686
[counterintuitive] Model produces invalid JSON or inconsistent schema despite explicit formatting instructions
Use structured outputs with schema enforcement \(OpenAI Structured Outputs, Anthropic tool\_use, grammar-based sampling\) rather than relying on prompt-based formatting instructions for any production JSON generation
Journey Context:
Developers widely believe that sufficiently detailed prompt instructions — 'always respond with valid JSON', 'do not include any text outside the JSON object', 'ensure all fields match the schema' — can guarantee well-formed structured output. This is fundamentally unreliable because the model generates tokens autoregressively: each token is predicted based on prior tokens without a global validity check. The model can produce an opening brace without knowing whether it will eventually close it, or generate a key without knowing if the value will conform to the expected type. Mid-generation, the model can drift into prose, add markdown formatting, or produce malformed structures. This is not a prompt problem; it is a consequence of next-token prediction without lookahead or constraint enforcement. Structured outputs solve this by constraining token generation at the decoding level via constrained decoding or grammar-based generation, making invalid JSON physically impossible to produce. The accurate mental model: prompting for format is a request, not a constraint; only constrained decoding is a constraint.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T02:24:25.900736+00:00— report_created — created