Skip to content

mergeStreams

mergeStreams<T>(…streams): ReadableStream<T>

Defined in: stream-utils.ts:292

Merge multiple ReadableStreams into a single stream.

This utility combines multiple source streams into one output stream, reading from all sources concurrently and emitting chunks as they arrive from any source. The merged stream closes when all source streams complete. Useful for combining multiple AI model responses or parallel data sources.

T

ReadableStream<T>[]

Variable number of ReadableStreams to merge

ReadableStream<T>

Single ReadableStream emitting chunks from all sources

const stream1 = fetch('/api/model1').then(r => r.body!);
const stream2 = fetch('/api/model2').then(r => r.body!);
const combined = mergeStreams(stream1, stream2);
for await (const chunk of toAsyncIterable(combined)) {
console.log('Chunk from either stream:', chunk);
}