How to Intermediate · 4 min read

How to collect full text from Claude streaming response

Quick answer
Use the anthropic Python SDK's client.messages.create method with stream=True to receive chunks of the streaming response. Collect each chunk's completion field and concatenate them to assemble the full text from Claude's streaming output.

PREREQUISITES

  • Python 3.8+
  • Anthropic API key
  • pip install anthropic>=0.20

Setup

Install the anthropic Python SDK and set your API key as an environment variable.

  • Install the SDK: pip install anthropic>=0.20
  • Set environment variable: export ANTHROPIC_API_KEY='your_api_key' (Linux/macOS) or set ANTHROPIC_API_KEY=your_api_key (Windows)
bash
pip install anthropic>=0.20

Step by step

This example shows how to stream a completion from Claude and collect the full text by concatenating each chunk's completion field.

python
import os
import anthropic

client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

response = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    prompt="\n\nHuman: Write a short poem about AI.\n\nAssistant:",
    max_tokens=200,
    stream=True
)

full_text = ""
for chunk in response:
    # Each chunk is a dict with a 'completion' key containing the next text fragment
    full_text += chunk["completion"]

print("Full streamed response:")
print(full_text)
output
Full streamed response:
AI, a spark of mind and light,
Guiding us through day and night.
Silent helper, ever near,
Turning dreams to visions clear.

Common variations

You can adjust the model to other Claude versions like claude-3-5-haiku-20241022 or use synchronous calls by setting stream=False. For async streaming, use async for with an async client.

python
import asyncio
import os
import anthropic

async def async_stream():
    client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
    response = await client.messages.acreate(
        model="claude-3-5-sonnet-20241022",
        prompt="\n\nHuman: Explain streaming in AI APIs.\n\nAssistant:",
        max_tokens=150,
        stream=True
    )

    full_text = ""
    async for chunk in response:
        full_text += chunk["completion"]

    print("Async full streamed response:")
    print(full_text)

asyncio.run(async_stream())
output
Async full streamed response:
Streaming lets you receive partial outputs as they are generated, improving responsiveness and user experience.

Troubleshooting

  • If the stream hangs or returns no data, verify your API key and network connection.
  • Ensure you handle exceptions around the streaming loop to catch interruptions.
  • Check that stream=True is set; otherwise, the response will not be streamed.

Key Takeaways

  • Use stream=True in client.messages.create to enable streaming from Claude.
  • Concatenate each chunk's completion field to build the full streamed text.
  • Handle streaming asynchronously for non-blocking applications using async for.
  • Always set your API key via environment variables for security and best practice.
  • Check for network or API errors if streaming does not produce output.
Verified 2026-04 · claude-3-5-sonnet-20241022, claude-3-5-haiku-20241022
Verify ↗