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

toHttpStream.md

51 lines1.3 KB

1---
2id: toHttpStream
3title: toHttpStream
4---
5​
6# Function: toHttpStream()
7​
8```ts
9function toHttpStream(stream, abortController?): ReadableStream<Uint8Array<ArrayBufferLike>>;
10```
11​
12Defined in: [packages/ai/src/stream-to-response.ts:171](https://github.com/TanStack/ai/blob/main/packages/ai/src/stream-to-response.ts#L171)
13​
14Convert a StreamChunk async iterable to a ReadableStream in HTTP stream format (newline-delimited JSON)
15​
16This creates a ReadableStream that emits chunks as newline-delimited JSON:
17- Each chunk is JSON.stringify'd and followed by "\n"
18- No SSE formatting (no "data: " prefix)
19​
20This format is compatible with `fetchHttpStream` connection adapter.
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## Returns
37​
38`ReadableStream`\<`Uint8Array`\<`ArrayBufferLike`\>\>
39​
40ReadableStream in HTTP stream format (newline-delimited JSON)
41​
42## Example
43​
44```typescript
45const stream = chat({ adapter: openaiText(), model: "gpt-4o", messages: [...] });
46const readableStream = toHttpStream(stream);
47// Use with Response for HTTP streaming (not SSE)
48return new Response(readableStream, {
49 headers: { 'Content-Type': 'application/x-ndjson' }
50});
51```