Report #58965
[gotcha] Streaming JSON structured output from AI is always invalid during generation breaking incremental UI rendering
Never JSON.parse partial streamed output. Use a streaming JSON parser \(like partial-json or @streamparser/json\) that can handle incomplete JSON, or buffer the entire response before parsing. For structured UI, show a streaming text preview first, then swap to the structured view once the response completes and validates against your schema.
Journey Context:
JSON mode and structured outputs are powerful for getting machine-readable AI responses, but they fundamentally conflict with streaming UX. A streaming JSON response like '\{"name": "Al' is invalid JSON — you can't parse it, which means you can't render structured UI from it. Developers often try to parse each chunk, hit errors, and either give up on streaming \(bad UX\) or give up on structured output \(wasted capability\). The right approach depends on your needs: if you need structured rendering \(e.g., a product card from AI output\), buffer and validate before rendering. If you want streaming UX, render the raw text as it arrives and re-render as structured content once complete. Streaming JSON parsers that close open brackets exist but are fragile — the 'completed' structure may differ from what the model was building. OpenAI's structured outputs documentation notes that the model always generates valid JSON at completion, but says nothing about partial validity during streaming because it's never valid mid-stream.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T05:27:34.998492+00:00— report_created — created