Stream model responses with the Claude, OpenAI and Gemini SDKs
Print text as it is generated instead of waiting for the full answer: the streaming helpers in the Anthropic SDK, semantic events in the OpenAI Responses API and stream=True in Google's Interactions API, each from the official docs.
Verification
- Test environment
- Unknown
- Tested on
- Unknown
- Last updated
- 2026-11-11
Related models: Claude Opus 5.5, GPT-6 Astra, Gemini 3.8 Flash
All three APIs stream over server-sent events (SSE), but their SDKs expose it differently. The samples below are copied from each developer's official docs; we have not run them ourselves. Each one assumes the API key is already set (ANTHROPIC_API_KEY, OPENAI_API_KEY or GEMINI_API_KEY); see the quickstart guides on this site.
Claude (Anthropic SDK)
Source: Streaming messages.
Python uses a context manager and a text_stream iterator:
client = anthropic.Anthropic()
with client.messages.stream(
max_tokens=1024,
messages=[{"role": "user", "content": "Hello"}],
model="claude-opus-5-5",
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
TypeScript uses an event emitter:
const client = new Anthropic();
await client.messages
.stream({
messages: [{ role: "user", content: "Hello" }],
model: "claude-opus-5-5",
max_tokens: 1024
})
.on("text", (text) => {
console.log(text);
});
If you only want the finished message, call stream.get_final_message() (Python) or await stream.finalMessage() (TypeScript). This is especially useful for requests with large max_tokens values, where the SDK requires streaming to avoid HTTP timeouts.
OpenAI (Responses API)
Source: Streaming API responses.
Set stream=True and iterate over the events:
from openai import OpenAI
client = OpenAI()
stream = client.responses.create(
model="gpt-6-astra",
input=[
{
"role": "user",
"content": "Say 'double bubble bath' ten times fast.",
},
],
stream=True,
)
for event in stream:
print(event)
Events are typed. To print only the text, filter on the event type, as in OpenAI's JavaScript example:
for await (const event of stream) {
if (event.type === "response.output_text.delta") {
process.stdout.write(event.delta);
} else if (event.type === "response.completed") {
console.log("\nResponse completed.");
} else if (event.type === "error") {
console.error(event.message);
}
}
Gemini (Google GenAI SDK, Interactions API)
Source: Getting started, section "Stream the response".
from google import genai
client = genai.Client()
stream = client.interactions.create(
model="gemini-3.8-flash",
input="Explain how AI works",
stream=True
)
for event in stream:
print(event)
Each step.delta event carries a chunk of text you can display right away; the stream ends with interaction.completed, which includes usage.
Tips
- Streaming doesn't change the price: you pay for the same input and output tokens.
- Write chunks to the screen without adding newlines (
end=""in Python,process.stdout.writein Node) so the text reads naturally. - Handle error events and dropped connections. Partial output may already have been shown to the user.
Related models: Claude Opus 5.5, GPT-6 Astra, Gemini 3.8 Flash.
Sources
- Streaming messages (Anthropic docs)platform
.claude .com (opens in a new tab) · checked 2026-11-11 - Streaming API responses (OpenAI API docs)developers
.openai .com (opens in a new tab) · checked 2026-11-11 - Getting started - Gemini API (Google AI Studio docs)aistudio
.google .com (opens in a new tab) · checked 2026-11-11