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

streamToText.md

44 lines1.0 KB

1---
2id: streamToText
3title: streamToText
4---
5​
6```ts
7function streamToText(stream): Promise<ChatResult>;
8```
9​
10Defined in: [packages/ai/src/stream-to-response.ts:60](https://github.com/TanStack/ai/blob/main/packages/ai/src/stream-to-response.ts#L60)
11​
12Read a StreamChunk async iterable to the end and return its text and chunks.
13​
14`text` joins the deltas of every TEXT_MESSAGE_CONTENT event. `chunks` keeps
15every chunk in order, so tool calls and interrupt outcomes are not lost.
16​
17## Parameters
18​
19### stream
20​
21`AsyncIterable`\<[`AGUIEvent`](../type-aliases/AGUIEvent.md)\>
22​
23AsyncIterable of StreamChunks from chat()
24​
25## Returns
26​
27`Promise`\<[`ChatResult`](../interfaces/ChatResult.md)\>
28​
29A [ChatResult](../interfaces/ChatResult.md) with the joined text and every chunk.
30​
31## Throws
32​
33The error from the first RUN_ERROR chunk.
34​
35## Example
36​
37```typescript
38const stream = chat({
39 adapter: openaiText('gpt-5.5'),
40 messages: [{ role: 'user', content: 'Hello!' }]
41});
42const { text } = await streamToText(stream);
43console.log(text); // "Hello! How can I help you today?"
44```