Report #55447
[gotcha] Streaming JSON from AI produces unparseable fragments — client-side JSON.parse fails on partial tokens
When streaming structured outputs, accumulate \`delta.content\` strings and only parse the concatenated result when the stream signals completion. Never attempt to \`JSON.parse\(\)\` individual streaming chunks. Use incremental JSON parsers only if you need mid-stream structural rendering.
Journey Context:
Streaming is great for UX — users see responses immediately. But when the response is structured \(JSON\), each streaming chunk is a partial token that forms invalid JSON. A chunk might be \`\{"name": "Al\` and the next \`ice"\}\`. Naively parsing each chunk crashes. The alternatives are: \(1\) buffer everything then parse \(loses streaming UX benefit entirely\), \(2\) use incremental/streaming JSON parsers that handle partial structures, \(3\) use the SSE delta accumulation pattern where you concatenate deltas and only parse when complete. Option 3 is the most practical — you still render raw text as it streams for perceived speed, but you defer all structured parsing to stream completion. Option 2 is viable but adds significant dependency complexity for marginal benefit. The gotcha is that developers test with short responses that fit in a single chunk and never see the failure in dev.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T23:33:36.223981+00:00— report_created — created