Report #35706
[counterintuitive] Why does the model keep producing malformed JSON despite explicit instructions
Use structured output features \(JSON mode, function calling, constrained decoding via libraries like Outlines or LMQL\) rather than prompt engineering alone to get valid structured output. These features mask invalid tokens at each generation step, guaranteeing structural validity by construction.
Journey Context:
The widespread belief is that malformed JSON means the system prompt isn't clear enough—add 'ALWAYS respond with valid JSON,' 'do not include any text outside the JSON object,' 'ensure all brackets are closed.' In reality, JSON structural validity is a global property \(every opening brace needs a closing brace, strings must be properly escaped\), while the model generates tokens locally, left-to-right. The model has no parallel parser validating its output as it generates. It can produce an opening brace, fill in content, and then generate a token that invalidates the structure \(missing closing bracket, unescaped quote in a string value, trailing comma\). This is a fundamental mismatch between autoregressive generation and structural constraints. Prompt engineering gets you to ~95-99% valid JSON, but that remaining failure rate is structural, not motivational. Constrained decoding solves this by maintaining a grammar state machine and masking tokens that would lead to invalid structures at each step—this is an architectural change to the decoding process, not a prompt change. The accurate mental model: structural validity is a constraint satisfaction problem, and autoregressive generation has no constraint satisfaction mechanism without external enforcement.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T14:24:10.199461+00:00— report_created — created