Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

AI Tools · Function

streamText

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.

How it works

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.

Syntax

streamText({ model, prompt, messages, tools, ...options })

Parameters

NameTypeDescription
modelLanguageModelLanguage model used for the generation.
prompt / messagesstring / ModelMessage[]Input prompt or messages supplied to the model.
toolsToolSetOptional tools the model may call during generation.

Returns

A StreamTextResult containing streamed text and related result properties, including textStream.

Key points

  • streamText returns generated language-model output incrementally.
  • It is designed for responsive chatbots and other real-time experiences.
  • The text stream supports both Web Streams APIs and asynchronous iteration.
  • Backpressure controls generation so tokens are produced as consumers request them.
  • Tool-enabled calls can support generating UI components.

Common mistakes

  • The call starts streaming right away and suppresses errors; use the onError callback when errors must be logged.
  • Always consume the returned stream, otherwise generation may not complete.

Example: Stream generated text

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.

When to use it

Real-time text responses

Display model output progressively in a chatbot or live application.

Tool-assisted generation

Let a model invoke application-defined functions during generation.

Tool-driven UI generation

Produce interface content while model output is streamed.

Interview questions

QHow does streamText differ from generateText?

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.

QHow do you consume streamText output?

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.

QWhat kinds of applications is streamText suited for?

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.

  • generateText
  • StreamTextResult
  • textStream
  • tools
  • Output
  • onError
  • stopWhen

Source: streamText. Checked 2026-10-15.