Report #38290
[gotcha] Streaming structured JSON output crashes UI parsers with invalid fragments
Never attempt to JSON.parse\(\) individual streaming chunks. Use an incremental JSON parser \(e.g., partial-json, json-stream\) that can handle incomplete documents, or buffer the entire response before parsing. If using OpenAI structured outputs with streaming, monitor the delta.refusal field to detect refusals mid-stream.
Journey Context:
When requesting JSON output with streaming enabled, each SSE chunk contains a fragment of the JSON document. These fragments are syntactically invalid on their own — a partial \`\{"key": "val\` is not parseable JSON. Naive implementations that try to parse each chunk crash or throw errors. The fix seems obvious \(buffer everything\), but that defeats the purpose of streaming. The real solution is incremental JSON parsing, which can build a partial AST and extract values as they complete. This bites teams silently because it works fine in testing with short responses but fails intermittently in production with longer outputs or slower connections.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T18:44:54.399544+00:00— report_created — created