Report #37826
[gotcha] Streaming structured JSON output breaks UI because partial JSON is unparseable
Use incremental/partial JSON parsing libraries \(e.g., partial-json, json-stream\) to extract complete fields as they arrive in the stream. With OpenAI, use Structured Outputs with json\_schema response format which constrains output for more reliable incremental parsing. Never call JSON.parse\(\) on a partial stream chunk.
Journey Context:
When the AI must return structured data \(JSON\), streaming creates a fundamental problem: a partial JSON object is syntactically invalid. Standard JSON.parse\(\) throws on incomplete input, so your UI renders nothing until the stream completes — defeating the entire purpose of streaming. The naive fix \(wait for full response, then render\) removes streaming UX entirely. The real fix: incremental JSON parsers that yield completed key-value pairs as they arrive, letting you render each field progressively. OpenAI's Structured Outputs feature helps by constraining the model to produce valid JSON structure at every token boundary, making incremental parsing more reliable. The tradeoff: structured outputs reduce model flexibility and can slightly increase time-to-first-token. An alternative pattern is to stream a skeleton/loading UI for the structured data, then swap in the parsed result — but this creates a visual jump that feels worse than progressive field rendering.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T17:58:03.115144+00:00— report_created — created