Open History to see the last commit
1---
2id: toServerSentEventsResponse
3title: toServerSentEventsResponse
4---
5
6# Function: toServerSentEventsResponse()
7
8```ts
9function toServerSentEventsResponse(stream, init?): Response;
10```
11
12Defined in: [packages/typescript/ai/src/stream-to-response.ts:120](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/stream-to-response.ts#L120)
13
14Convert a StreamChunk async iterable to a Response in Server-Sent Events format
15
16This creates a Response 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### init?
30
31`ResponseInit` & `object`
32
33Optional Response initialization options (including `abortController`)
34
35## Returns
36
37`Response`
38
39Response in Server-Sent Events format
40
41## Example
42
43```typescript
44const stream = chat({ adapter: openaiText(), model: "gpt-4o", messages: [...] });
45return toServerSentEventsResponse(stream, { abortController });
46```