Skip to main content
Generates text and calls tools for a given prompt using a language model, streaming the output. If you do not want to stream the output, use generateText instead.
import { streamText } from 'ai';
import { openai } from '@ai-sdk/openai';

const result = streamText({
  model: openai('gpt-4-turbo'),
  prompt: 'Invent a new holiday.',
});

for await (const textPart of result.textStream) {
  console.log(textPart);
}

Parameters

model
LanguageModel
required
The language model to use.
prompt
string
A simple text prompt. You can either use prompt or messages but not both.
messages
Array<CoreMessage>
A list of messages. You can either use prompt or messages but not both.
system
string
A system message that will be part of the prompt.
tools
ToolSet
Tools that are accessible to and can be called by the model. The model needs to support calling tools.
toolChoice
ToolChoice
The tool choice strategy. Default: 'auto'.
maxOutputTokens
number
Maximum number of tokens to generate.
temperature
number
Temperature setting. The value is passed through to the provider. The range depends on the provider and model. It is recommended to set either temperature or topP, but not both.
topP
number
Nucleus sampling. The value is passed through to the provider. The range depends on the provider and model. It is recommended to set either temperature or topP, but not both.
topK
number
Only sample from the top K options for each subsequent token. Used to remove “long tail” low probability responses. Recommended for advanced use cases only. You usually only need to use temperature.
presencePenalty
number
Presence penalty setting. It affects the likelihood of the model to repeat information that is already in the prompt. The value is passed through to the provider. The range depends on the provider and model.
frequencyPenalty
number
Frequency penalty setting. It affects the likelihood of the model to repeatedly use the same words or phrases. The value is passed through to the provider. The range depends on the provider and model.
stopSequences
Array<string>
Stop sequences. If set, the model will stop generating text when one of the stop sequences is generated.
seed
number
The seed (integer) to use for random sampling. If set and supported by the model, calls will generate deterministic results.
maxRetries
number
default:"2"
Maximum number of retries. Set to 0 to disable retries.
abortSignal
AbortSignal
An optional abort signal that can be used to cancel the call.
timeout
number
An optional timeout in milliseconds. The call will be aborted if it takes longer than the specified timeout.
headers
Record<string, string>
Additional HTTP headers to be sent with the request. Only applicable for HTTP-based providers.
stopWhen
StopCondition | Array<StopCondition>
default:"stepCountIs(1)"
Condition for stopping the generation when there are tool results in the last step. When the condition is an array, any of the conditions can be met to stop the generation.
output
Output
Optional specification for parsing structured outputs from the LLM response.
activeTools
Array<keyof TOOLS>
Limits the tools that are available for the model to call without changing the tool call and result types in the result.
prepareStep
PrepareStepFunction
Optional function that you can use to provide different settings for a step.
experimental_repairToolCall
ToolCallRepairFunction
A function that attempts to repair a tool call that failed to parse.
experimental_transform
StreamTextTransform | Array<StreamTextTransform>
Optional stream transformations. They are applied in the order they are provided. The stream transformations must maintain the stream structure for streamText to work correctly.
experimental_download
DownloadFunction
Custom download function to use for URLs. By default, files are downloaded if the model does not support the URL for the given media type.
includeRawChunks
boolean
default:"false"
Whether to include raw chunks from the provider in the stream. When enabled, you will receive raw chunks with type ‘raw’ that contain the unprocessed data from the provider. This allows access to cutting-edge provider features not yet wrapped by the AI SDK.
experimental_context
unknown
Context that is passed into tool execution.
experimental_telemetry
TelemetrySettings
Optional telemetry configuration (experimental).
providerOptions
ProviderOptions
Additional provider-specific options. They are passed through to the provider from the AI SDK and enable provider-specific functionality that can be fully encapsulated in the provider.
onChunk
(chunk: TextStreamPart) => void
Callback that is called for each chunk of the stream. The stream processing will pause until the callback promise is resolved.
onError
(error: Error) => void
Callback that is invoked when an error occurs during streaming. You can use it to log errors. The stream processing will pause until the callback promise is resolved.
experimental_onStart
(event: OnStartEvent) => void
Callback invoked when generation begins, before any LLM calls.
experimental_onStepStart
(event: OnStepStartEvent) => void
Callback invoked when each step begins, before the provider is called.
experimental_onToolCallStart
(event: OnToolCallStartEvent) => void
Callback invoked before each tool execution begins.
experimental_onToolCallFinish
(event: OnToolCallFinishEvent) => void
Callback invoked after each tool execution completes.
onStepFinish
(event: OnStepFinishEvent) => void
Callback that is called when each step (LLM call) is finished, including intermediate steps.
onFinish
(event: OnFinishEvent) => void
Callback that is called when the LLM response and all request tool executions are finished. The usage is the combined usage of all steps.
onAbort
(event: OnAbortEvent) => void
Callback that is called when the stream is aborted.

Returns

textStream
AsyncIterableStream<string>
A text stream that returns only the generated text deltas. You can use it as an async iterable or call textStream.getReader() to get a reader.
fullStream
AsyncIterableStream<TextStreamPart>
A stream with all events, including text deltas, tool calls, tool results, and metadata.
usage
Promise<LanguageModelUsage>
A promise that resolves to the total token usage.
finishReason
Promise<FinishReason>
A promise that resolves to the finish reason.
steps
Promise<Array<StepResult>>
A promise that resolves to the details for all steps.
toTextStreamResponse
(init?: ResponseInit) => Response
Creates a simple text stream response for easier integration with the Vercel AI SDK UI hooks. The response is a text/plain stream that streams the text parts.
pipeTextStreamToResponse
(response: ServerResponse, init?: ResponseInit) => void
Pipes the text stream to a Node.js response-like object.

Examples

Basic streaming

import { streamText } from 'ai';
import { openai } from '@ai-sdk/openai';

const result = streamText({
  model: openai('gpt-4-turbo'),
  prompt: 'Invent a new holiday.',
});

for await (const textPart of result.textStream) {
  console.log(textPart);
}

Full stream with events

import { streamText } from 'ai';
import { openai } from '@ai-sdk/openai';

const result = streamText({
  model: openai('gpt-4-turbo'),
  prompt: 'Invent a new holiday.',
});

for await (const part of result.fullStream) {
  switch (part.type) {
    case 'text-delta':
      console.log('Text delta:', part.text);
      break;
    case 'tool-call':
      console.log('Tool call:', part.toolName);
      break;
    case 'tool-result':
      console.log('Tool result:', part.result);
      break;
    case 'finish':
      console.log('Finish reason:', part.finishReason);
      break;
  }
}

Stream to response

import { streamText } from 'ai';
import { openai } from '@ai-sdk/openai';

export async function POST(req: Request) {
  const { prompt } = await req.json();

  const result = streamText({
    model: openai('gpt-4-turbo'),
    prompt,
  });

  return result.toTextStreamResponse();
}