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

toStreamResponse.md

55 lines1.2 KB

1---
2id: toStreamResponse
3title: toStreamResponse
4---
5​
6# ~~Function: toStreamResponse()~~
7​
8```ts
9function toStreamResponse(stream, init?): Response;
10```
11​
12Defined in: [stream-to-response.ts:213](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/stream-to-response.ts#L213)
13​
14Create a streaming HTTP response from a StreamChunk async iterable
15Includes proper headers for Server-Sent Events
16​
17## Parameters
18​
19### stream
20​
21`AsyncIterable`\<[`StreamChunk`](../type-aliases/StreamChunk.md)\>
22​
23AsyncIterable of StreamChunks from chat()
24​
25### init?
26​
27`ResponseInit` & `object`
28​
29Optional Response initialization options
30​
31## Returns
32​
33`Response`
34​
35Response object with SSE headers and streaming body
36​
37## Deprecated
38​
39Use `toServerSentEventsStream` instead. This function will be removed in a future version.
40​
41## Example
42​
43```typescript
44export async function POST(request: Request) {
45 const { messages } = await request.json();
46 const abortController = new AbortController();
47 const stream = chat({
48 adapter: openaiText(),
49 model: "gpt-4o",
50 messages,
51 options: { abortSignal: abortController.signal }
52 });
53 return toStreamResponse(stream, undefined, abortController);
55```