Skip to main content

Overview

The stream() function streams responses from a chat model in real-time, allowing you to process tokens as they’re generated. This is ideal for interactive applications where you want to display responses progressively.

Function signature

Parameters

ChatModel
required
The chat model instance to use for streaming.
Message[]
required
Array of messages in the conversation. Must not be empty.
number
Sampling temperature (0-2). Higher values make output more random.
number
Maximum number of tokens to generate.
number
Nucleus sampling parameter (0-1).
ReasoningConfig
Configuration for extended thinking/reasoning capabilities.
ToolSet
Object mapping tool names to tool definitions.
ToolChoice
Controls how the model uses tools: 'auto', 'none', 'required', or { type: 'tool', toolName: string }.
GenerateProviderOptions
Provider-specific options, namespaced by provider name.
AbortSignal
AbortSignal for cancelling the stream.

Return value

Returns a Promise<ChatStream>. ChatStream is an async iterable of StreamEvent objects with two additional properties:
Promise<GenerateResult>
Resolves with the aggregated final response when the stream completes. Rejects on abort or upstream failure.
Promise<readonly StreamEvent[]>
Resolves with all observed events, including abort and failure cases.
The HTTP request starts as soon as you create the stream. You do not need to iterate before the model begins responding.

StreamEvent types

{ type: 'reasoning-start' }
Emitted when reasoning/thinking begins.
{ type: 'reasoning-delta'; text: string }
Emitted for each chunk of reasoning text.
{ type: 'reasoning-end'; metadata?: Record<string, unknown>; providerMetadata?: Record<string, Record<string, unknown>> }
Emitted when reasoning completes. May include provider-namespaced metadata.
{ type: 'text-start' }
Emitted when a new text segment begins.
{ type: 'text-delta'; text: string }
Emitted for each chunk of response text.
{ type: 'text-end'; metadata?: Record<string, unknown> }
Emitted when a text segment completes. Middleware can attach application-owned metadata, which is copied onto the matching AssistantTextPart in .result.
{ type: 'tool-call-start'; toolCallId: string; toolName: string }
Emitted when a tool call begins.
{ type: 'tool-call-delta'; toolCallId: string; argumentsDelta: string }
Emitted for each chunk of tool call arguments.
{ type: 'tool-call-end'; toolCall: ToolCall; providerMetadata?: Record<string, Record<string, unknown>> }
Emitted when a tool call completes with the full tool call object. providerMetadata carries provider-owned data that has to be replayed with the call, such as a Google thought signature.
{ type: 'finish'; finishReason: FinishReason; usage: ChatUsage }
Emitted when streaming completes with final metadata.

Examples

Basic streaming

Handling all event types

Using .result

Using .events

.result resolves when the stream completes, regardless of event consumption. .events resolves with the observed history even on abort or upstream failure. Streams are replayable, so late iteration replays buffered events before continuing live.

Text segment metadata

stream() emits text-start and text-end around each aggregated text segment. Provider adapters emit these boundaries as they map native provider streams into core-ai events. createChatStream() uses the same events to aggregate the matching AssistantTextPart in .result. Middleware that annotates streamed text can wrap the stream and yield text-end with metadata. The final .result.parts includes that metadata on the matching AssistantTextPart.

Streaming with tools

Cancellation

Important notes

ChatStream is replayable: iterating after events have already arrived replays the buffered event history before waiting for later events.
.result resolves with the aggregated final response when the stream completes, regardless of whether you consumed the events via iteration.

Error handling

Throws ValidationError if:
  • Messages array is empty
May also throw:
  • ProviderError if the provider returns an error during streaming
Throws StreamAbortedError if the stream is aborted via the signal.