Open History to see the last commit
1---
2id: toServerSentEventsStream
3title: toServerSentEventsStream
4---
5
6# Function: toServerSentEventsStream()
7
8```ts
9function toServerSentEventsStream(stream, abortController?): ReadableStream<Uint8Array<ArrayBufferLike>>;
10```
11
12Defined in: [packages/ai/src/stream-to-response.ts:50](https://github.com/TanStack/ai/blob/main/packages/ai/src/stream-to-response.ts#L50)
13
14Convert a StreamChunk async iterable to a ReadableStream in Server-Sent Events format
15
16This creates a ReadableStream that emits chunks in SSE format:
17- Each chunk is prefixed with "data: "
18- Each chunk is followed by "\n\n"
19- Stream ends when the underlying iterable is exhausted (RUN_FINISHED is the terminal event)
20
21## Parameters
22
23### stream
24
25`AsyncIterable`\<[`AGUIEvent`](../type-aliases/AGUIEvent.md)\>
26
27AsyncIterable of StreamChunks from chat()
28
29### abortController?
30
31`AbortController`
32
33Optional AbortController to abort when stream is cancelled
34
35## Returns
36
37`ReadableStream`\<`Uint8Array`\<`ArrayBufferLike`\>\>
38
39ReadableStream in Server-Sent Events format