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:- The AI decides to call a tool
- The tool executes
- The AI receives the result
- The AI generates the final response (streamed)
Parameters
Typed Events
Consumeevent_stream when one ordered channel needs to drive a UI, transport,
or log:
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 asreasoning.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:
Thinking for provider differences and guidance.
TextStreamResult Object
Thestream_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