Streaming Text and Formatting Server Responses
For conversational chat applications, you should stream text outputs. The Vercel AI SDK provides the streamText function to simplify this flow.
1. Using streamText in Node.js
Call streamText to receive a streamable chunk handler:
import { streamText } from "ai";
import { defaultModel } from "../lib/models";
async function runLocalStream() {
const result = await streamText({
model: defaultModel,
prompt: "Write a short poem about coding in rust.",
});
// Consume stream directly in server logs
for await (const textChunk of result.textStream) {
process.stdout.write(textChunk);
}
}2. Returning Streams in Route Handlers
When building Web APIs (such as in Next.js or Astro), return the stream using the built-in helper .toDataStreamResponse() to pipe data to the client:
// app/api/chat/route.ts
import { streamText } from "ai";
import { defaultModel } from "../../../lib/models";
export async function POST(req: Request) {
const { messages } = await req.json();
const result = await streamText({
model: defaultModel,
messages: messages,
system: "You are a helpful software store assistant.",
});
// Automatically sets up appropriate Server-Sent Events headers
return result.toDataStreamResponse();
}3. Benefits of toDataStreamResponse()
- Standardized Stream Protocol: It formats chunks (including text tokens, errors, and tool execution status updates) into a unified protocol.
- Simplifies Frontend Hooks: Compatible out-of-the-box with frontend React hooks (such as
useChat), removing the need to parse manual text decoders.
Published on Last updated: