Real-time text responses
Display model output progressively in a chatbot or live application.
AI Tools · Function
streamText streams text generated by a language model, making it suitable for chatbots and other real-time applications. It can also work with tools to support streamed UI generation.
Call streamText with a language model and prompt to begin generation immediately. Consume the returned textStream as an async iterable to receive text incrementally; the result can also support tools and streamed UI generation.
streamText({ model, prompt, messages, tools, ...options })| Name | Type | Description |
|---|---|---|
model | LanguageModel | Language model used for the generation. |
prompt / messages | string / ModelMessage[] | Input prompt or messages supplied to the model. |
tools | ToolSet | Optional tools the model may call during generation. |
A StreamTextResult containing streamed text and related result properties, including textStream.
import { streamText } from 'ai';
const result = streamText({
model: 'anthropic/claude-sonnet-5.5',
prompt: 'Invent a new holiday and describe its traditions.',
});
for await (const textPart of result.textStream) {
process.stdout.write(textPart);
}Streams generated text to standard output as chunks arrive.
Display model output progressively in a chatbot or live application.
Let a model invoke application-defined functions during generation.
Produce interface content while model output is streamed.
streamText generates text incrementally rather than waiting for the complete response. Its result exposes textStream, which can be consumed with asynchronous iteration.
Follow-up: Why might a completed generation not finish? The stream must be consumed because generation uses backpressure and produces tokens as they are requested.
Provide a model and prompt in the configuration object, then iterate over result.textStream. This lets an application display or forward text as it arrives.
Follow-up: How can a server expose the output to a client? Convert or return the appropriate stream response using the framework integration for the target UI.
streamText supports tools in addition to text generation, allowing models to call functions while producing a streamed result. The function is intended for interactive applications such as chatbots and other real-time experiences.
Follow-up: How can multiple tool calls be continued automatically? Configure the documented multi-step tool-calling mechanism, such as stopWhen, with the available tools.
generateTextStreamTextResulttextStreamtoolsOutputonErrorstopWhen