Open History to see the last commit
1---
2id: toHttpStream
3title: toHttpStream
4---
5
6```ts
7function toHttpStream(
8 stream,
9 abortController?,
10getId?): ReadableStream<Uint8Array<ArrayBufferLike>>;
11```
12
13Defined in: [packages/ai/src/stream-to-response.ts:1416](https://github.com/TanStack/ai/blob/main/packages/ai/src/stream-to-response.ts#L1416)
14
15Convert a StreamChunk async iterable to a ReadableStream in HTTP stream format (newline-delimited JSON)
16
17This creates a ReadableStream that emits chunks as newline-delimited JSON:
18- Each chunk is JSON.stringify'd and followed by "\n"
19- No SSE formatting (no "data: " prefix)
20
21This format is compatible with `fetchHttpStream` connection adapter.
22
23When `getId` is supplied (delivery durability), each chunk is emitted as an
24envelope `{"id":"<offset>","chunk":{…}}` instead of a bare chunk. NDJSON has
25no native event-id field like SSE's `id:` line, so the resumable offset rides
26inside the payload. Untagged chunks (no id) stay bare, so a non-durable
27stream is byte-identical to before and the client auto-detects either form.
28
29## Parameters
30
31### stream
32
33`AsyncIterable`\<[`AGUIEvent`](../type-aliases/AGUIEvent.md)\>
34
35AsyncIterable of StreamChunks from chat()
36
37### abortController?
38
39`AbortController`
40
41Optional AbortController to abort when stream is cancelled
42
43### getId?
44
45(`chunk`, `index`) => `string` \| `undefined`
46
47Optional per-chunk durability offset; when present, chunks are envelope-encoded
48
49## Returns
50
51`ReadableStream`\<`Uint8Array`\<`ArrayBufferLike`\>\>
52
53ReadableStream in HTTP stream format (newline-delimited JSON)
54
55## Example
56
57```typescript
58const stream = chat({ adapter: openaiText('gpt-5.5'), messages: [...] });
59const readableStream = toHttpStream(stream);
60// Use with Response for HTTP streaming (not SSE)
61return new Response(readableStream, {
62 headers: { 'Content-Type': 'application/x-ndjson' }
63});
64```