
# `simulateReadableStream()`

`simulateReadableStream` is a utility function that creates a ReadableStream which emits provided values sequentially with configurable delays. This is particularly useful for testing streaming functionality or simulating time-delayed data streams.

```ts
import { simulateReadableStream } from 'ai';

const stream = simulateReadableStream({
  chunks: ['Hello', ' ', 'World'],
  initialDelayInMs: 100,
  chunkDelayInMs: 50,
});
```

## Import

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

## API Signature

### Parameters

<PropertiesTable
  content={[
    {
      name: 'chunks',
      type: 'T[]',
      isOptional: false,
      description: 'Array of values to be emitted by the stream',
    },
    {
      name: 'initialDelayInMs',
      type: 'number | null',
      isOptional: true,
      description:
        'Initial delay in milliseconds before emitting the first value. Defaults to 0. Set to null to skip the initial delay entirely.',
    },
    {
      name: 'chunkDelayInMs',
      type: 'number | null',
      isOptional: true,
      description:
        'Delay in milliseconds between emitting each value. Defaults to 0. Set to null to skip delays between chunks.',
    },
  ]}
/>

### Returns

Returns a `ReadableStream<T>` that:

- Emits each value from the provided `chunks` array sequentially
- Waits for `initialDelayInMs` before emitting the first value (if not `null`)
- Waits for `chunkDelayInMs` between emitting subsequent values (if not `null`)
- Closes automatically after all chunks have been emitted

### Type Parameters

- `T`: The type of values contained in the chunks array and emitted by the stream

## Examples

### Basic Usage

```ts
const stream = simulateReadableStream({
  chunks: ['Hello', ' ', 'World'],
});
```

### With Delays

```ts
const stream = simulateReadableStream({
  chunks: ['Hello', ' ', 'World'],
  initialDelayInMs: 1000, // Wait 1 second before first chunk
  chunkDelayInMs: 500, // Wait 0.5 seconds between chunks
});
```

### Without Delays

```ts
const stream = simulateReadableStream({
  chunks: ['Hello', ' ', 'World'],
  initialDelayInMs: null, // No initial delay
  chunkDelayInMs: null, // No delay between chunks
});
```


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