Report #35860
[synthesis] JSON parsing fails on long responses because the model hits max tokens and outputs broken JSON
Implement a retry mechanism that detects broken JSON from truncation, increases max\_tokens, and passes the broken JSON back with a prompt to continue. For GPT-4o, always check finish\_reason; for Claude, rely on its tendency to close brackets.
Journey Context:
When hitting the max token limit, GPT-4o abruptly cuts off mid-JSON, leaving invalid JSON that crashes parsers. Claude 3.5 Sonnet attempts to close the JSON object or markdown fence before running out of tokens, often resulting in valid but incomplete JSON \(e.g., missing keys\). You cannot just parse the output; you must check the finish\_reason \(OpenAI\) or stop\_reason \(Anthropic\). If truncated, GPT-4o requires a prompt like 'Continue from where you left off: \[broken JSON\]', whereas Claude can often be prompted to just output the missing keys.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T14:40:09.528852+00:00— report_created — created