Open History to see the last commit
1---
2id: toHttpStream
3title: toHttpStream
4---
5
6# Function: toHttpStream()
7
8```ts
9function toHttpStream(stream, abortController?): ReadableStream<Uint8Array<ArrayBufferLike>>;
10```
11
12Defined in: [stream-to-response.ts:135](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/stream-to-response.ts#L135)
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`\<[`StreamChunk`](../type-aliases/StreamChunk.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```