stream_text() starts a streaming model/tool loop and returns a TextStreamResult immediately.
It is not an async function. Streaming begins when you iterate over result.text_stream.
Import
Signature
Parameters
LanguageModel
required
Language model instance.
str | None
Convenience user prompt. Ignored when
messages is provided.str | None
Optional system prompt. Only used when
messages is not provided.list[Message] | list[dict[str, Any]] | None
Explicit message list. When provided,
prompt and system are ignored.ToolSet | None
Mapping of tool name to
Tool.StopCondition | list[StopCondition] | None
Stop condition or list of stop conditions. Defaults to
step_count_is(1).OnStepStart | None
Called before each provider request. May return
StepControl.OnStepFinish | None
Called after each step completes.
OnReasoningEvent | None
Called for provider reasoning/thinking events. These are not mixed into
text_stream.RetryPolicy | None
Provider-call retry policy. By default, retries are conservative: transient HTTP statuses (
408, 409, 425, 429, 500, 502, 503, 504) and network/timeout failures are retried; clear client errors such as 400, 401, and 403 are not. Streaming retries only happen before the provider has emitted any output for that attempt.OnRetry | None
Called before a retry attempt with the step number, attempt, delay, and sanitized error string.
ProviderOptions | None
Provider-specific options keyed by provider name.
ReasoningConfig | None
Provider-agnostic reasoning controls.
AbortSignal | None
Abort signal used to cancel the streaming loop, including in-flight provider stream reads, retry sleeps, and async tool calls.
Any
Additional provider-specific keyword arguments passed to the provider stream call.
Returns
TextStreamResult
Lazy streaming result object.
AsyncIterator[TextStreamEvent]
Async iterator yielding typed events in generation order. Event types are
step.started, text.delta, reasoning.summary, reasoning.delta,
reasoning.signature, reasoning.state, tool_call.started,
tool_call.delta, tool_call.ready,
tool_execution.started, tool_execution.finished, tool_result,
step.finished, and stream.finished.AsyncIterator[str]
Text-only projection of
event_stream.Awaitable[str]
Complete accumulated text. Await after or during stream consumption.
Awaitable[Usage | None]
Usage after the stream completes.
Awaitable[str | None]
Finish reason after the stream completes.
Awaitable[list[StepResult]]
Step history after the stream completes.
TextStreamEvent
TextStreamEvent is a discriminated union. Branch on event.type before reading
event-specific fields.
The
reasoning.* discriminator preserves the underlying ReasoningEvent.kind:
summary, delta, signature, or state. Treat reasoning.summary and
reasoning.delta as streamed reasoning output; they are intentionally separate
from text.delta and are not included in text_stream. Step history fields are
snapshots of the completed steps at the time that event was emitted.
Tool calls receive a stable provider-order index. Ready and result events are
emitted in provider order. Parallel execution finish events are emitted in actual
completion order. tool_execution.finished captures the direct execution result;
tool_result contains the final result after after_tool_call hooks and matches
the result stored in StepResult.tool_results.
Blocked and unknown tools emit tool_call.ready and an error tool_result, but
do not emit execution start or finish events because no tool function ran.
Every completed tool call emits tool_call.started, at least one
tool_call.delta, then tool_call.ready. Concatenate name_delta and
arguments_delta by (step_number, index); IDs may be unavailable on the first
fragment and appear on a later delta. tool_call.ready is the normalization
boundary and contains the parsed ToolCall.
Provider coverage:
Always handle
tool_call.ready as the authoritative parsed call. Treat
start/delta events as progressive updates when the provider exposes fragments,
or as normalized one-shot call previews when it does not.
Raises
Errors occur while consuming the stream, not when callingstream_text().
Exception
Raised when neither
prompt nor messages is provided.Exception
Raised while consuming if
signal is aborted.Examples
Typed event stream
text_stream. Reading
both views does not make a second provider request.
Basic stream
Read final values
Reasoning events
on_reasoning_event remains supported for compatibility. New integrations that
need text, reasoning, and lifecycle ordering should consume event_stream.