A real-time financial dashboard must stream token-by-token responses from Claude and display partial JSON as the response accumulates. During testing, the SSE stream intermittently delivers malformed partial JSON that crashes the parser before the closing brace arrives. What is the correct architectural pattern to handle this?
Select an answer to reveal the explanation.
Short Explanation
A streamed JSON reply is like a sentence arriving one syllable at a time, so you can't grade each syllable as if it were the whole sentence. Keep a growing buffer for each content block, let a partial-JSON parser show fields as they finish, and save the strict parse for content_block_stop. You get a live dashboard and a parser that never chokes on half an object.
Full Explanation
Streaming delivers JSON as partial string fragments in content_block_delta events, and a single object can span many deltas, so no individual fragment is guaranteed to be valid on its own. The documented pattern is to accumulate those string deltas and parse once content_block_stop arrives, using a partial-JSON parser such as Pydantic or the SDK streaming helpers, which also expose the parsed value incrementally while it streams. Keeping one buffer per content block matters because a message can contain several blocks, and each one closes independently with its own stop event. Parsing each delta as independent JSON and discarding failures is wrong because nearly every fragment would be thrown away and the view would never fill in. Detecting JSON at content_block_start and then cancelling to re-issue a non-streaming request is wrong because it wastes the tokens already generated, doubles latency, and abandons the live display the dashboard requires. Disabling streaming entirely is wrong for the same reason, since nothing renders until the whole response is complete. The exam caveat is that the strict parse belongs at content_block_stop for that block, not at message_stop. To check an implementation, log each delta and the partial parse result, then confirm the final strict parse matches the last partial view.