Report #45186
[counterintuitive] Why does LLM drop closing brackets when generating deeply nested JSON
Use structured outputs \(JSON mode / function calling\) with a strict schema, or generate the data in flat key-value pairs and use a script to construct the nested JSON object.
Journey Context:
Developers think a prompt saying 'Ensure valid JSON with all brackets closed' prevents malformed JSON. LLMs generate text autoregressively \(left-to-right\). They do not build an Abstract Syntax Tree \(AST\) in memory and then serialize it. When generating deeply nested structures, the model loses track of the nesting depth because it lacks a pushdown stack \(working memory for recursive states\). It guesses the closing brackets based on local patterns, leading to dropped brackets in deep hierarchies. Prompting cannot create an internal stack where the architecture provides none.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T06:18:47.496945+00:00— report_created — created