Docs
CodeRabbit
Cloudflare
AG Grid
SerpAPI
Netlify
OpenRouter
Neon
WorkOS
Clerk
Electric
PowerSync
Sentry
Railway
Prisma
Strapi
Unkey
CodeRabbit
Cloudflare
AG Grid
SerpAPI
Netlify
OpenRouter
Neon
WorkOS
Clerk
Electric
PowerSync
Sentry
Railway
Prisma
Strapi
Unkey
Persistence (Advanced)
Embeddings
Reranking
Evaluate
Sandboxes (Advanced)
Class References
Function References
Interface References
Type Alias References
Variable References
Function References

toServerSentEventsStream

ts
function toServerSentEventsStream(
   stream, 
   abortController?, 
getId?): ReadableStream<Uint8Array<ArrayBufferLike>>;
function toServerSentEventsStream(
   stream, 
   abortController?, 
getId?): ReadableStream<Uint8Array<ArrayBufferLike>>;

Defined in: packages/ai/src/stream-to-response.ts:473

Convert a StreamChunk async iterable to a ReadableStream in Server-Sent Events format

This creates a ReadableStream that emits chunks in SSE format:

  • Each chunk is prefixed with "data: "
  • Each chunk is followed by "\n\n"
  • Stream ends when the underlying iterable is exhausted (RUN_FINISHED is the terminal event)

Parameters

stream

AsyncIterable<AGUIEvent>

AsyncIterable of StreamChunks from chat()

abortController?

AbortController

Optional AbortController to abort when stream is cancelled

getId?

(chunk, index) => string | undefined

Optional per-chunk durability offset; when present, each event gets an id: line

Returns

ReadableStream<Uint8Array<ArrayBufferLike>>

ReadableStream in Server-Sent Events format