
# `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.

```ts highlight={"6-9"}
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

<Snippet text={`import { smoothStream } from "ai"`} prompt={false} />

## API Signature

### Parameters

<PropertiesTable
  content={[
    {
      name: 'delayInMs',
      type: 'number | null',
      isOptional: true,
      description:
        'The delay in milliseconds between outputting each chunk. Defaults to 10ms. Set to `null` to disable delays.',
    },
    {
      name: 'chunking',
      type: '"word" | "line" | RegExp | (buffer: string) => string | undefined | null',
      isOptional: true,
      description:
        '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 callback or RegExp pattern for custom chunking.',
    },
  ]}
/>

#### Word chunking caveats with non-latin languages

<Note>
    The word based chunking **does not work well** with the following languages that do not delimit words with spaces:

    For these languages we recommend using a custom regex, like the following:

    - Chinese - `/[\u4E00-\u9FFF]|\S+\s+/`
    - Japanese - `/[\u3040-\u309F\u30A0-\u30FF]|\S+\s+/`

    For these languages you could pass your own language aware chunking function:

    - Vietnamese
    - Thai
    - Javanese (Aksara Jawa)

</Note>

#### Regex based chunking

To use regex based chunking, pass a `RegExp` to the `chunking` option.

```ts
// To split on underscores:
smoothStream({
  chunking: /_+/,
});

// Also can do it like this, same behavior
smoothStream({
  chunking: /[^_]*_/,
});
```

#### Custom callback chunking

To use a custom callback for chunking, pass a function to the `chunking` option.

```ts
smoothStream({
  chunking: text => {
    const findString = 'some string';
    const index = text.indexOf(findString);

    if (index === -1) {
      return null;
    }

    return text.slice(0, index) + findString;
  },
});
```

### 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


## Navigation

- [generateText](/v4/docs/reference/ai-sdk-core/generate-text)
- [streamText](/v4/docs/reference/ai-sdk-core/stream-text)
- [generateObject](/v4/docs/reference/ai-sdk-core/generate-object)
- [streamObject](/v4/docs/reference/ai-sdk-core/stream-object)
- [embed](/v4/docs/reference/ai-sdk-core/embed)
- [embedMany](/v4/docs/reference/ai-sdk-core/embed-many)
- [generateImage](/v4/docs/reference/ai-sdk-core/generate-image)
- [transcribe](/v4/docs/reference/ai-sdk-core/transcribe)
- [generateSpeech](/v4/docs/reference/ai-sdk-core/generate-speech)
- [tool](/v4/docs/reference/ai-sdk-core/tool)
- [experimental_createMCPClient](/v4/docs/reference/ai-sdk-core/create-mcp-client)
- [Experimental_StdioMCPTransport](/v4/docs/reference/ai-sdk-core/mcp-stdio-transport)
- [jsonSchema](/v4/docs/reference/ai-sdk-core/json-schema)
- [zodSchema](/v4/docs/reference/ai-sdk-core/zod-schema)
- [valibotSchema](/v4/docs/reference/ai-sdk-core/valibot-schema)
- [CoreMessage](/v4/docs/reference/ai-sdk-core/core-message)
- [createProviderRegistry](/v4/docs/reference/ai-sdk-core/provider-registry)
- [customProvider](/v4/docs/reference/ai-sdk-core/custom-provider)
- [cosineSimilarity](/v4/docs/reference/ai-sdk-core/cosine-similarity)
- [wrapLanguageModel](/v4/docs/reference/ai-sdk-core/wrap-language-model)
- [LanguageModelV1Middleware](/v4/docs/reference/ai-sdk-core/language-model-v1-middleware)
- [extractReasoningMiddleware](/v4/docs/reference/ai-sdk-core/extract-reasoning-middleware)
- [simulateStreamingMiddleware](/v4/docs/reference/ai-sdk-core/simulate-streaming-middleware)
- [defaultSettingsMiddleware](/v4/docs/reference/ai-sdk-core/default-settings-middleware)
- [simulateReadableStream](/v4/docs/reference/ai-sdk-core/simulate-readable-stream)
- [smoothStream](/v4/docs/reference/ai-sdk-core/smooth-stream)
- [generateId](/v4/docs/reference/ai-sdk-core/generate-id)
- [createIdGenerator](/v4/docs/reference/ai-sdk-core/create-id-generator)


[Full Sitemap](/sitemap.md)
