Skip to main content
The stream_text function enables real-time streaming of AI responses. This provides a better user experience by showing text as it’s generated rather than waiting for the complete response. stream_text exposes a unified typed event_stream for text, provider thinking, and lifecycle events. Its text_stream property remains a text-only projection, and on_reasoning_event remains available for callback-based integrations.

Basic Streaming

stream_text returns immediately with a TextStreamResult object. The provider request starts when you iterate over event_stream or text_stream, or await a final result property.

Getting Usage Statistics

Token usage is available after streaming completes:

Streaming with Tools

Combine streaming with tool calling for powerful agentic workflows:
When tools are provided, the streaming handles the full execution loop:
  1. The AI decides to call a tool
  2. The tool executes
  3. The AI receives the result
  4. The AI generates the final response (streamed)

Parameters

Typed Events

Consume event_stream when one ordered channel needs to drive a UI, transport, or log:
The stream emits step.started and step.finished around each model step, then one stream.finished event. Both stream views are replayable and share one provider request, including when consumed concurrently. When a completed tool call is available, the same stream emits tool_call.ready, tool_execution.started, tool_execution.finished, and tool_result. Parallel finish events arrive as tools complete; normalized result events remain in provider order. OpenAI-compatible, Anthropic, and Bedrock streams also emit tool_call.started and tool_call.delta while function names and JSON arguments are being assembled. Use the step number and provider index for correlation until a tool call ID is available. Providers that only return completed calls, such as Gemini functionCall parts, still get normalized start/delta events immediately before tool_call.ready.

Thinking Events

Reasoning is emitted as reasoning.summary, reasoning.delta, reasoning.signature, or reasoning.state. Summary and delta events are both streamed reasoning output, but neither is included in text_stream. Callback-based integrations can continue using on_reasoning_event:
See Thinking for provider differences and guidance.

TextStreamResult Object

The stream_text function returns a TextStreamResult:

Collecting Full Text

If you need the complete text after streaming:

Real-time UI Updates

Stream directly to a user interface:

Next Steps

Tools

Add function calling to your streams

Agents

Build agents with streaming output