Word-by-word display
Streaming generated prose in a chat interface
AI Tools · Function
smoothStream smooths text and reasoning output from streamText by buffering content into configurable chunks and releasing them with optional delays. It returns a TransformStream for streamText's experimental_transform option.
smoothStream creates a TransformStream for streamText. It buffers text and reasoning output, releases content according to the selected chunking strategy, optionally inserts delays between chunks, and forwards other event types immediately.
smoothStream(options?: { delayInMs?: number | null; chunking?: "word" | "line" | RegExp | Intl.Segmenter | ((buffer: string) => string | undefined | null) })| Name | Type | Description |
|---|---|---|
delayInMs | number | null | Optional delay in milliseconds between output chunks; defaults to 10 ms, or use null to disable delays. |
chunking | "word" | "line" | RegExp | Intl.Segmenter | (buffer: string) => string | undefined | null | Optional chunking strategy: word, line, RegExp, Intl.Segmenter, or a custom callback. |
A TransformStream that buffers text and reasoning chunks, releases them at chunk boundaries with optional delays, and immediately passes through other chunk types.
import { smoothStream, streamText } from 'ai';
const result = streamText({
model,
prompt: 'Explain photosynthesis.',
experimental_transform: smoothStream({
delayInMs: 20,
chunking: 'word',
}),
});Applies smoothStream to streamText so text is buffered into words and released with a 20 ms interval.
Streaming generated prose in a chat interface
Displaying generated content organized by paragraphs or lines
Supporting languages whose word boundaries differ from whitespace
Preserving immediate delivery of tool and lifecycle events
smoothStream is a utility that returns a TransformStream for the streamText experimental_transform option. It buffers text and reasoning chunks and releases them according to a chunking rule, creating smoother output.
Follow-up: What happens to tool-call events? They bypass the smoothing behavior and are passed through immediately.
The optional delayInMs controls the interval between emitted chunks and defaults to 10 milliseconds. Passing null disables those delays while retaining the transformation.
Follow-up: What happens when the page is hidden? The delay is skipped so browser timer throttling does not stall the stream.
chunking defaults to word and also accepts line, a regular expression, Intl.Segmenter, or a callback. Intl.Segmenter is recommended when locale-aware segmentation is needed, including for CJK languages.
Follow-up: What constraint applies to a custom regular expression? It must not match the empty string.
streamTextexperimental_transformIntl.Segmenter