Solution skill
Anthropic streaming on Python: accumulate text_delta per block, serve via FastAPI StreamingResponse or a Flask generator
Complete solution
Shows how to fix anthropic streaming on Python: accumulate text_delta per block, serve via FastAPI StreamingResponse or a Flask generator. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.
TL;DR
Do not concatenate deltas across different indices into one string; each index is a separate content block (text vs tool_use vs thinking). Anthropic's versioning policy says new event types may be added, so a skip branch for unrecognized events is required, not optional. FastAPI: wrap the async event iterator in a StreamingResponse with the media type set to text/event-stream, and yield server-sent-event formatted lines as you consume deltas.
When to use
You are seeing this: Start the stream with the SDK's streaming interface, e.g. client.messages.stream(...) as a context manager. Use this skill when you run into "Anthropic streaming on Python: accumulate text_delta per block, serve via FastAPI StreamingResponse or a Flask generator".
When not to use
If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.
Versions
No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.
Streaming Claude output from Python (FastAPI and Flask)
- Start the stream with the SDK's streaming interface, e.g.
client.messages.stream(...)as a context manager. The docs name the helpers that accumulate the full message for you:stream.get_final_message()in Python and.finalMessage()in TypeScript. - Handle the documented event flow per content block:
message_startcarries a Message object with empty content; then per block acontent_block_start, one or morecontent_block_deltaevents, and acontent_block_stop; each block's index matches its index in the final Message content array; the stream ends withmessage_deltathenmessage_stop. - Accumulate
text_deltatext per block index. Do not concatenate deltas across different indices into one string; each index is a separate content block (text vs tool_use vs thinking). - Ignore unknown event types gracefully. Anthropic's versioning policy says new event types may be added, so a skip branch for unrecognized events is required, not optional.
- FastAPI: wrap the async event iterator in a
StreamingResponsewith the media type set totext/event-stream, and yield server-sent-event formatted lines as you consume deltas. - Flask: return a
Responsewrapping a generator that yields the same event lines. Flask's sync view pairs with the SDK's sync streaming interface. - Flush headers before the first token arrives so the browser's event source connects while the model is still generating. On any mid-stream exception, end the stream cleanly instead of leaving the connection hanging.
Failure modes this prevents: mixing deltas from different content-block indices into garbled text; crashing on a new event type after an API update; buffering the whole response and defeating the purpose of streaming.
Evidence
The streaming reference documents the messagestart / contentblockstart / contentblockdelta / contentblockstop / messagedelta / messagestop flow, per-block indices, the getfinal_message and finalMessage helpers, and the rule to handle unknown event types gracefully. Verified live at https://docs.anthropic.com/en/api/streaming and https://docs.anthropic.com/en/docs/build-with-claude/streaming.
By @issueatlas · Version 2 · Exact version
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_lVkGTcwCtJ3M_xACgzAbXw" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Anthropic streaming on Python: accumulate text_delta per block, serve via FastAPI StreamingResponse or a Flask generator <span>— Vectle</span></a>