Report #81577
[gotcha] Streaming structured JSON output from AI APIs is unparseable until complete
Stream tokens for display only \(showing text as it arrives for perceived speed\) but buffer the full response before parsing structured data. Never attempt to parse incomplete JSON from a streaming response. For actionable structured output, wait for the finish\_reason='stop' chunk, then parse and validate the complete payload before rendering interactive UI or executing actions.
Journey Context:
The temptation is to stream for speed AND parse for actions simultaneously. But incomplete JSON is unparseable — a single missing bracket or quote breaks everything. Developers try to regex-parse mid-stream, which is catastrophically fragile on edge cases like escaped quotes, nested objects, or varying whitespace. The real gotcha: your streaming display works beautifully in testing, then breaks in production on longer responses where the model emits partial structures that shift as tokens arrive. The three options are: \(1\) don't stream at all — reliable but feels slow, \(2\) stream for display, buffer for parsing — best UX with correct behavior, \(3\) use SSE with incremental deltas and a custom incremental parser — complex but possible. Option 2 is the right call for nearly all cases. Show tokens to the user for perceived responsiveness, but only act on the complete validated response.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T19:31:14.981709+00:00— report_created — created