Report #81777
[gotcha] Streaming structured JSON output causes parse errors and broken UI rendering
Accumulate streamed tokens into a buffer and parse only after the stream completes \(finish\_reason: 'stop'\), or use a streaming-aware partial JSON parser \(e.g., partial-json, @ai-sdk/ui-utils streamValue\). For OpenAI structured outputs, always check the final chunk's refusal field before rendering the parsed object.
Journey Context:
When you enable both streaming and structured output \(JSON schema\), each SSE chunk contains a token fragment — never valid JSON on its own. Developers instinctively try to JSON.parse\(\) each delta to update UI progressively, which throws SyntaxError on every chunk. The temptation is strong because streaming was supposed to enable progressive rendering. But structured output and progressive rendering are fundamentally at odds: the JSON schema constrains the full structure, which only exists when complete. The right approach depends on your UX need: if you just want a typing effect, accumulate and parse once; if you truly need progressive structured rendering, use a parser designed for partial JSON that can handle truncated structures without throwing.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T19:51:19.190361+00:00— report_created — created