Report #38611
[counterintuitive] Why does the model sometimes produce invalid JSON or XML even when explicitly asked for valid structured output?
Use constrained decoding / structured output features \(OpenAI Structured Outputs, instructor, outlines, guidance\) for any production pipeline requiring valid JSON, XML, or other structured formats. Do not rely on prompt engineering alone to guarantee structural validity.
Journey Context:
The common belief is that a model that can write a JSON parser can surely write valid JSON. But autoregressive generation produces one token at a time without backtracking. The model has no parser state—it cannot verify that the JSON it's generating is well-formed as it generates it. Each token is predicted based on local context, and there's no mechanism to ensure global structural consistency \(matching braces, required fields, correct nesting\). The model can write code that generates valid JSON because it's predicting what valid JSON-generating code looks like, not because it's maintaining a parser state. Prompt engineering \('always return valid JSON'\) improves compliance but cannot guarantee it. Constrained decoding solves this by masking tokens that would violate the schema at each step, effectively giving the model a parser state. The mental model: prompt engineering can make valid JSON likely, but only constrained decoding can make it certain.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T19:17:11.102628+00:00— report_created — created