Docs
CodeRabbit
Cloudflare
AG Grid
SerpAPI
Netlify
OpenRouter
Neon
WorkOS
Clerk
Electric
PowerSync
Sentry
Railway
Prisma
Strapi
Unkey
CodeRabbit
Cloudflare
AG Grid
SerpAPI
Netlify
OpenRouter
Neon
WorkOS
Clerk
Electric
PowerSync
Sentry
Railway
Prisma
Strapi
Unkey
Class References
Function References
Interface References
Type Alias References
Variable References

streamToText

Function: streamToText()

ts
function streamToText(stream): Promise<string>;
function streamToText(stream): Promise<string>;

Defined in: packages/typescript/ai/src/stream-to-response.ts:24

Collect all text content from a StreamChunk async iterable and return as a string.

This function consumes the entire stream, accumulating content from TEXT_MESSAGE_CONTENT events, and returns the final concatenated text.

Parameters

stream

AsyncIterable<AGUIEvent>

AsyncIterable of StreamChunks from chat()

Returns

Promise<string>

Promise - The accumulated text content

Example

typescript
const stream = chat({
  adapter: openaiText(),
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'Hello!' }]
});
const text = await streamToText(stream);
console.log(text); // "Hello! How can I help you today?"
const stream = chat({
  adapter: openaiText(),
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'Hello!' }]
});
const text = await streamToText(stream);
console.log(text); // "Hello! How can I help you today?"