smoothStream
Stream transformer for smoothing text and reasoning output
smoothStream is a utility function that creates a TransformStream
for the streamText experimental_transform option
to smooth out text and reasoning streaming by buffering and releasing complete chunks with configurable delays.
This creates a more natural reading experience when streaming text and reasoning responses.
Import
import { smoothStream } from "ai"
API Signature
Parameters
delayInMs?:
null to disable delays. The delay is skipped while the document is hidden (e.g. browser background tabs), where timer throttling would otherwise stall the stream.chunking?:
Word chunking caveats with non-latin languages
The word based chunking does not work well with the following languages that do not delimit words with spaces:
- Chinese
- Japanese
- Korean
- Vietnamese
- Thai
Using Intl.Segmenter (recommended)
For these languages, we recommend using Intl.Segmenter for proper locale-aware word segmentation.
This is the preferred approach as it provides accurate word boundaries for CJK and other languages.
Intl.Segmenter is available in Node.js 16+ and all modern browsers (Chrome
87+, Firefox 125+, Safari 14.1+).
Regex based chunking
To use regex based chunking, pass a RegExp to the chunking option. Global
and sticky expressions are supported. The expression must not match the empty
string.
Custom callback chunking
To use a custom callback for chunking, pass a function to the chunking option.
Returns
Returns a TransformStream that:
- Buffers incoming text and reasoning chunks
- Releases content when the chunking pattern is encountered
- Adds configurable delays between chunks for smooth output
- Passes through non-text/reasoning chunks (like tool calls, step-finish events) immediately