Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

toServerSentEventsStream.md

47 lines1.2 KB

1---
2id: toServerSentEventsStream
3title: toServerSentEventsStream
4---
5​
6# Function: toServerSentEventsStream()
7​
8```ts
9function toServerSentEventsStream(stream, abortController?): ReadableStream<Uint8Array<ArrayBufferLike>>;
10```
11​
12Defined in: [stream-to-response.ts:56](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/stream-to-response.ts#L56)
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 with "data: [DONE]\n\n"
20​
21## Parameters
22​
23### stream
24​
25`AsyncIterable`\<[`StreamChunk`](../type-aliases/StreamChunk.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
40​
41## Example
42​
43```typescript
44const stream = chat({ adapter: openaiText(), model: "gpt-4o", messages: [...] });
45const readableStream = toServerSentEventsStream(stream);
46// Use with Response, or any API that accepts ReadableStream
47```