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