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

toJsonResponse.md

241 lines6.9 KB

1---
2id: toJsonResponse
3title: toJsonResponse
4---
5​
6## Call Signature
7​
8```ts
9function toJsonResponse(result, init?): Promise<Response>;
10```
11​
12Defined in: [packages/ai/src/stream-to-response.ts:1684](https://github.com/TanStack/ai/blob/main/packages/ai/src/stream-to-response.ts#L1684)
13​
14Send a chat run as one JSON body, for hosts that cannot stream a response.
15Pair it with the `fetchJson` connection adapter.
16​
17The body is `{ chunks, offset?, done }`:
18- `chunks`: every chunk, encoded for the wire like the SSE and NDJSON helpers.
19- `offset`: the durable offset of the last chunk (durable routes only).
20- `done`: `false` when the run is still going. The client asks again with
21 `GET ?runId=<id>&offset=<offset>`, served by [resumeJsonResponse](resumeJsonResponse.md).
22​
23Pass a `ChatResult` from `chat({ stream: false })` to send it as is. Pass the
24object from `chat({ outputSchema })` to send it as a complete structured
25output. A Promise of either is awaited. Pass a stream to read it here.
26Without `durability` the stream is read to its end.
27With `durability`, a fresh run fills the log, and the reply goes out when the
28run ends or when `maxWaitMs` passes, whichever is first. The run keeps going
29after an early reply. A request with a resume offset reads the log instead.
30​
31### Parameters
32​
33#### result
34​
35[`ChatResult`](../interfaces/ChatResult.md)
36​
37#### init?
38​
39`ResponseInit`
40​
41Response init. For a stream, also `abortController`, `signal`, `maxWaitMs`, `durability`, and `debug`.
42​
43### Returns
44​
45`Promise`\<`Response`\>
46​
47A promise of a JSON Response
48​
49### Example
50​
51```typescript
52// A finished result
53export async function POST(request: Request) {
54 const result = await chat({
55 adapter: openaiText('gpt-5.5'),
56 messages: [...],
57 stream: false,
58 });
59 return toJsonResponse(result);
61​
62// A structured output
63export async function POST(request: Request) {
64 const person = await chat({
65 adapter: openaiText('gpt-5.5'),
66 messages: [...],
67 outputSchema: PersonSchema,
68 });
69 return toJsonResponse(person);
71​
72// A durable stream that replies at least once a second
73export async function POST(request: Request) {
74 const stream = chat({ adapter: openaiText('gpt-5.5'), messages: [...] });
75 return toJsonResponse(stream, {
76 durability: { adapter: memoryStream(request) },
77 signal: request.signal,
78 maxWaitMs: 1000,
79 });
81```
82​
83## Call Signature
84​
85```ts
86function toJsonResponse<TOffset>(stream, init?): Promise<Response>;
87```
88​
89Defined in: [packages/ai/src/stream-to-response.ts:1688](https://github.com/TanStack/ai/blob/main/packages/ai/src/stream-to-response.ts#L1688)
90​
91Send a chat run as one JSON body, for hosts that cannot stream a response.
92Pair it with the `fetchJson` connection adapter.
93​
94The body is `{ chunks, offset?, done }`:
95- `chunks`: every chunk, encoded for the wire like the SSE and NDJSON helpers.
96- `offset`: the durable offset of the last chunk (durable routes only).
97- `done`: `false` when the run is still going. The client asks again with
98 `GET ?runId=<id>&offset=<offset>`, served by [resumeJsonResponse](resumeJsonResponse.md).
99​
100Pass a `ChatResult` from `chat({ stream: false })` to send it as is. Pass the
101object from `chat({ outputSchema })` to send it as a complete structured
102output. A Promise of either is awaited. Pass a stream to read it here.
103Without `durability` the stream is read to its end.
104With `durability`, a fresh run fills the log, and the reply goes out when the
105run ends or when `maxWaitMs` passes, whichever is first. The run keeps going
106after an early reply. A request with a resume offset reads the log instead.
107​
108### Type Parameters
109​
110#### TOffset
111​
112`TOffset` *extends* `string` = `string`
113​
114### Parameters
115​
116#### stream
117​
118`AsyncIterable`\<[`AGUIEvent`](../type-aliases/AGUIEvent.md)\>
119​
120#### init?
121​
122`JsonResponseInit`\<`TOffset`\>
123​
124Response init. For a stream, also `abortController`, `signal`, `maxWaitMs`, `durability`, and `debug`.
125​
126### Returns
127​
128`Promise`\<`Response`\>
129​
130A promise of a JSON Response
131​
132### Example
133​
134```typescript
135// A finished result
136export async function POST(request: Request) {
137 const result = await chat({
138 adapter: openaiText('gpt-5.5'),
139 messages: [...],
140 stream: false,
141 });
142 return toJsonResponse(result);
144​
145// A structured output
146export async function POST(request: Request) {
147 const person = await chat({
148 adapter: openaiText('gpt-5.5'),
149 messages: [...],
150 outputSchema: PersonSchema,
151 });
152 return toJsonResponse(person);
154​
155// A durable stream that replies at least once a second
156export async function POST(request: Request) {
157 const stream = chat({ adapter: openaiText('gpt-5.5'), messages: [...] });
158 return toJsonResponse(stream, {
159 durability: { adapter: memoryStream(request) },
160 signal: request.signal,
161 maxWaitMs: 1000,
162 });
164```
165​
166## Call Signature
167​
168```ts
169function toJsonResponse(structuredOutput, init?): Promise<Response>;
170```
171​
172Defined in: [packages/ai/src/stream-to-response.ts:1692](https://github.com/TanStack/ai/blob/main/packages/ai/src/stream-to-response.ts#L1692)
173​
174Send a chat run as one JSON body, for hosts that cannot stream a response.
175Pair it with the `fetchJson` connection adapter.
176​
177The body is `{ chunks, offset?, done }`:
178- `chunks`: every chunk, encoded for the wire like the SSE and NDJSON helpers.
179- `offset`: the durable offset of the last chunk (durable routes only).
180- `done`: `false` when the run is still going. The client asks again with
181 `GET ?runId=<id>&offset=<offset>`, served by [resumeJsonResponse](resumeJsonResponse.md).
182​
183Pass a `ChatResult` from `chat({ stream: false })` to send it as is. Pass the
184object from `chat({ outputSchema })` to send it as a complete structured
185output. A Promise of either is awaited. Pass a stream to read it here.
186Without `durability` the stream is read to its end.
187With `durability`, a fresh run fills the log, and the reply goes out when the
188run ends or when `maxWaitMs` passes, whichever is first. The run keeps going
189after an early reply. A request with a resume offset reads the log instead.
190​
191### Parameters
192​
193#### structuredOutput
194​
195`object`
196​
197#### init?
198​
199`ResponseInit`
200​
201Response init. For a stream, also `abortController`, `signal`, `maxWaitMs`, `durability`, and `debug`.
202​
203### Returns
204​
205`Promise`\<`Response`\>
206​
207A promise of a JSON Response
208​
209### Example
210​
211```typescript
212// A finished result
213export async function POST(request: Request) {
214 const result = await chat({
215 adapter: openaiText('gpt-5.5'),
216 messages: [...],
217 stream: false,
218 });
219 return toJsonResponse(result);
221​
222// A structured output
223export async function POST(request: Request) {
224 const person = await chat({
225 adapter: openaiText('gpt-5.5'),
226 messages: [...],
227 outputSchema: PersonSchema,
228 });
229 return toJsonResponse(person);
231​
232// A durable stream that replies at least once a second
233export async function POST(request: Request) {
234 const stream = chat({ adapter: openaiText('gpt-5.5'), messages: [...] });
235 return toJsonResponse(stream, {
236 durability: { adapter: memoryStream(request) },
237 signal: request.signal,
238 maxWaitMs: 1000,
239 });
241```