Open History to see the last commit
1---
2id: streamToText
3title: streamToText
4---
5
6# Function: streamToText()
7
8```ts
9function streamToText(stream): Promise<string>;
10```
11
12Defined in: [stream-to-response.ts:23](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/stream-to-response.ts#L23)
13
14Collect all text content from a StreamChunk async iterable and return as a string.
15
16This function consumes the entire stream, accumulating content from 'content' type chunks,
17and returns the final concatenated text.
18
19## Parameters
20
21### stream
22
23`AsyncIterable`\<[`StreamChunk`](../type-aliases/StreamChunk.md)\>
24
25AsyncIterable of StreamChunks from chat()
26
27## Returns
28
29`Promise`\<`string`\>
30
31Promise<string> - The accumulated text content
32
33## Example
34
35```typescript
36const stream = chat({
37 adapter: openaiText(),
38 model: 'gpt-4o',
39 messages: [{ role: 'user', content: 'Hello!' }]
40});
41const text = await streamToText(stream);
42console.log(text); // "Hello! How can I help you today?"
43```