smoothStream()
smoothStream
is a utility function that creates a TransformStream
for the streamText
transform
option
to smooth out text streaming by buffering and releasing complete words with configurable delays.
This creates a more natural reading experience when streaming text responses.
import { smoothStream, streamText } from 'ai';
const result = streamText({ model, prompt, experimental_transform: smoothStream({ delayInMs: 20, // optional: defaults to 10ms chunking: 'line', // optional: defaults to 'word' }),});
Import
import { smoothStream } from "ai"
API Signature
Parameters
delayInMs?:
number | null
The delay in milliseconds between outputting each chunk. Defaults to 10ms. Set to `null` to disable delays.
chunking?:
"word" | "line" | RegExp
Controls how the text is chunked for streaming. Use "word" to stream word by word (default), "line" to stream line by line, or provide a custom RegExp pattern for custom chunking.
Returns
Returns a TransformStream
that:
- Buffers incoming text chunks
- Releases text when the chunking pattern is encountered
- Adds configurable delays between chunks for smooth output
- Passes through non-text chunks (like step-finish events) immediately