Report #48805
[gotcha] Streaming AI responses breaks JSON and structured output parsing
Use incremental/partial JSON parsers \(e.g. \`partial-json\` library or \`instructor\`'s \`create\_stream\`\) that can extract structure from incomplete JSON, or buffer the full response before parsing. Never call JSON.parse\(\) on each streaming chunk.
Journey Context:
Streaming is essential for perceived latency, but structured output \(JSON schemas, function calls\) requires complete valid JSON. Each streaming chunk is a fragment — missing closing braces, truncated strings, incomplete arrays — so naive JSON.parse\(\) fails on every chunk except the last. The temptation is to disable streaming for structured output entirely, but that creates unacceptable latency for long responses. The right approach is incremental parsing that can yield partial structure from incomplete JSON, letting you render extracted fields as they arrive while the rest streams in.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T12:24:09.266821+00:00— report_created — created