Open History to see the last commit
1---
2id: toHttpResponse
3title: toHttpResponse
4---
5
6```ts
7function toHttpResponse<TOffset>(stream, init?): Response;
8```
9
10Defined in: [packages/ai/src/stream-to-response.ts:1482](https://github.com/TanStack/ai/blob/main/packages/ai/src/stream-to-response.ts#L1482)
11
12Convert a StreamChunk async iterable to a Response in HTTP stream format (newline-delimited JSON)
13
14This creates a Response that emits chunks in HTTP stream format:
15- Each chunk is JSON.stringify'd and followed by "\n"
16- No SSE formatting (no "data: " prefix)
17
18This format is compatible with `fetchHttpStream` connection adapter.
19
20Pass a `durability` sink (`memoryStream(request)` / `durableStream(request)`)
21to make the stream resumable: fresh runs are appended to the log and each
22NDJSON line is emitted as an `{ id, chunk }` envelope carrying an opaque
23offset; a reconnect (native `Last-Event-ID` header) or a `?offset` join
24replays from the log without re-running the producer. `batch` controls how
25many chunks are buffered per `append` (default 32), and `batchWaitMs` how
26long a buffered chunk waits for more (default 50). This shares the exact
27`durableStreamSource` used by `toServerSentEventsResponse` — only the wire
28encoding differs.
29
30## Type Parameters
31
32### TOffset
33
34`TOffset` *extends* `string` = `string`
35
36## Parameters
37
38### stream
39
40`AsyncIterable`\<[`AGUIEvent`](../type-aliases/AGUIEvent.md)\>
41
42AsyncIterable of StreamChunks from chat()
43
44### init?
45
46`ResponseInit` & `object`
47
48Optional Response initialization options (including `abortController`, `durability` with its optional `batch` and `batchWaitMs`, and `debug`)
49
50## Returns
51
52`Response`
53
54Response in HTTP stream format (newline-delimited JSON)
55
56## Example
57
58```typescript
59export async function POST(request: Request) {
60 const stream = chat({ adapter: openaiText('gpt-5.5'), messages: [...] });
61 return toHttpResponse(stream, { durability: { adapter: memoryStream(request) } });
62}
63```