Report #84311
[gotcha] Streaming AI responses break JSON parsing — partial output is invalid until complete
Accumulate the full streamed response before parsing JSON, or use incremental/partial JSON parsers \(e.g., partial-json, json-repair\) that handle incomplete documents. For OpenAI structured outputs with streaming, collect all delta chunks, concatenate, and parse only after the finish\_reason='stop' signal. Never attempt to JSON.parse a mid-stream chunk.
Journey Context:
Streaming delivers tokens in real-time for perceived latency benefits, but each chunk is an incomplete fragment. If your backend or UI tries to parse \{"key": "val as JSON on every chunk, it throws. The naive fix — regex-extract values or manually close brackets — is fragile and breaks on nested structures, escaped quotes, and arrays. People commonly try to stream and parse simultaneously for progressive UI rendering of structured data, but this is a category error: JSON is a batch format, not a streaming one. The real tradeoff is between streaming UX \(fast perceived response\) and structured data integrity. For chat text, stream freely. For structured output \(JSON schemas, tool call arguments\), either accumulate fully before parsing, or adopt a partial JSON parser designed for incremental consumption. The partial parser approach is more complex but enables progressive rendering of known-safe fields.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T00:06:39.335061+00:00— report_created — created