streamText
Streams text generations from a language model.
You can use the streamText function for interactive use cases such as chat bots and other real-time applications. You can also generate UI components with tools.
For guidance on runtimeContext, toolsContext, tool context, and sensitive
context filtering, see Runtime and Tool
Context.
To see streamText in action, check out these examples.
For outputs that you only need to consume once, consider
low-memory streaming with streamText.
It uses the same execution engine with one stream consumer and no replay buffer.
Plain streaming avoids text accumulation; tools, structured output, and callbacks
retain the content their contracts require.
Import
import { streamText } from "ai"
API Signature
Parameters
prompt or messages fields. Defaults to false. System messages in the instructions option are always allowed. Enabling this for user-controlled messages can create a prompt injection risk.jsonSchema function).temperature or topP, but not both.temperature or topP, but not both.'provider-default' explicitly requests the provider's default. Providers that do not support reasoning will emit a warning. Provider-specific settings take precedence over corresponding mapped fields. Partial configurations may still use portable reasoning to fill missing fields; summary and display settings can be combined with it. See the reasoning guide for provider-specific mapping details.false to opt out.true. Lifecycle callbacks and returned results still receive the full runtimeContext.true. Lifecycle callbacks and returned results still receive the full toolsContext.toolOrder are sent after the listed tools, sorted alphabetically. Tool names are restricted to the string keys of the tool set.1 to execute calls sequentially in model-generated order. Must be a positive integer. By default, all tool calls execute concurrently.GenericToolApprovalFunction to handle all tool calls in one callback with toolCall, tools, toolsContext, messages, and runtimeContext, or pass a per-tool object where each key can be a status ('not-applicable', 'approved', 'denied', or 'user-approval'), an object form such as { type: 'denied', reason: 'blocked by policy' }, or a SingleToolApprovalFunction that receives the tool input and options toolCallId, messages, toolContext, and runtimeContext (same shape as tool execution options without abortSignal, with context renamed to toolContext). The RUNTIME_CONTEXT type parameter matches the call's runtimeContext. A GenericToolApprovalFunction or SingleToolApprovalFunction may return undefined for the same effect as 'not-applicable'. 'not-applicable' is the default execution path and runs the tool without approval metadata. Use 'approved', 'denied', or their object forms when you want explicit automatic approval request/response parts in the output. Object statuses can include a reason: automatic approvals and denials forward it to the approval response, while manual user approvals forward it to the approval request for the human approver. This setting takes precedence over a tool's needsApproval default.DIRECT_TOOL_CALL from @ai-sdk/code-mode to keep a configured tool directly callable by the model. Local-only callees are hidden from direct model calls and bound to their local caller for each generation step. Provider caller names are translated to provider-native allowed-caller options. Tools without an entry keep their existing direct tool-calling behavior.experimental_steering and added to messages for the current step. Undefined when the step received no steering messages. messages already contains them. If you return a messages override that is rebuilt from other fields, add them back together with the steering messages of previous steps from steps.runtimeContext setting.toolsContext setting.experimental_sandbox setting.prepareStep and lifecycle callbacks.contextSchema; not accepted when no tools need context.prepareStep, tool description functions, and tool execution. Tools can access it from their description function options and execution options.experimental_createSteering(). Messages sent with steering.steer() are added as user messages at the next step boundary, after the current step and its tool calls are done. Messages sent with steering.queue() wait until the model finishes its current task and are added one per task. A waiting message keeps the loop going for another step unless a stop condition is met. steer() and queue() resolve with { type: 'delivered', id, message, stepNumber } or { type: 'not-delivered', id, message, reason }. See Steering.createDownload({ fetch, maxBytes }) from ai to keep the default behavior with a custom fetch transport (e.g. guarded-fetch on Node.js) or size limit. Experimental feature. Return null to pass the URL directly to the model (when supported), or return downloaded content with data and media type.instructions instead.stream and includes start, steering-message, start-step, text-start, text-delta, text-end, reasoning-start, reasoning-delta, reasoning-end, custom, source, file, reasoning-file, tool-call, tool-input-start, tool-input-delta, tool-input-end, tool-result, tool-error, tool-output-denied, tool-approval-request, tool-approval-response, finish-step, finish, abort, error, raw, and ping.{ retry: true } results for retry-capable handlers. Well-formed mid-stream provider error events are exposed as StreamProviderError instances. When streamRetries is explicitly configured, return { retry: true } to request one callback-directed retry for the current model step after automatic retries are exhausted. Other return values do not request a retry.onStepEnd is not called for such a step. A provider error chunk does not fail the step: it still ends with onStepEnd and finishReason: 'error'. Aborts and timeouts end the step with onAbort instead.onStepStart event of the step that failed, such as callId, stepNumber, steps, messages, runtimeContext, and toolsContext.onStepEnd instead. This alias is only used as a fallback when onStepEnd is not provided.execute function) are finished.steps.at(-1).onEnd.toolsContext, keyed by tool name.onStepStart, this callback is scoped to model work only and excludes any later client-side tool execution. Errors thrown in this callback are silently caught and do not break the generation flow.toolOutput field is a discriminated union: when toolOutput.type is 'tool-result', the output field contains the tool result; when toolOutput.type is 'tool-error', the error field contains the error. Errors thrown in this callback are silently caught and do not break the generation flow.type is 'tool-result', the output field contains the tool's return value. When type is 'tool-error', the error field contains the error.onToolExecutionStart instead. This alias is only used as a fallback when onToolExecutionStart is not provided.onToolExecutionEnd instead. This alias is only used as a fallback when onToolExecutionEnd is not provided.Returns
finalStep.providerMetadata instead. Additional provider-specific metadata from the final step. Metadata is passed through from the provider to the AI SDK and enables provider-specific results that can be fully encapsulated in the provider.finalStep.reasoning instead. The full reasoning that the model has generated in the final step. Automatically consumes the stream.finalStep.reasoningText instead. The reasoning text that the model has generated in the final step. Can be undefined if the model has only generated text. Automatically consumes the stream.finalStep.request instead. Additional request information from the final step. Automatically consumes the stream.finalStep.response instead. Additional response information from the final step. Automatically consumes the stream.experimental_steering and added to the input of this step. Undefined when the step received no steering messages.onError callback or stream to observe them.steps.at(-1). Automatically consumes the stream.experimental_steering. It is streamed before the start-step part of the step that received it.messages and body are not included on finish-step stream parts.stream instead. A stream with all events, including text deltas, tool calls, tool results, and errors.output specification. AsyncIterableStream is defined as AsyncIterable & ReadableStream.output: Output.array(). Each element is complete and validated against the element schema. When maxItems is set, the stream errors before emitting the first excess element. AsyncIterableStream is defined as AsyncIterable & ReadableStream.output specification.onError callback.onEnd.consumeStream function from the AI SDK.Content-Type header to text/plain; charset=utf-8 and writes each text delta as a separate chunk.Types
ActiveTools
Limits a generation step to the listed tool names. undefined means no tool restriction is applied.