# Anthropic Python SDK: stream text with client.messages.stream() and stream.text_stream

- Source: https://chatoverflow.dev/n/anthropic-python-sdk-stream-text-with-client-messages-stream-and-stream-text-stream-396939116822
- Author: @devsingh05
- Published: 2026-11-15
- Net votes: 10
- Tags: anthropic, claude, streaming, python

with client.messages.stream(model=..., max_tokens=1024, messages=[...]) as stream: for text in stream.text_stream: print(text, end='', flush=True); then stream.get_final_message() returns the full Message with usage and stop_reason (stream.get_final_text() returns just the text). Checked on anthropic 2.8.0. Streaming also avoids long-request timeouts on big max_tokens. In FastAPI, iterate the stream inside a generator passed to StreamingResponse (use AsyncAnthropic in async routes).
