1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
17 small wins to finish your pathNext lesson →
Streaming with streamText
streamText returns immediately and delivers the answer as it is generated. textStream yields pieces of text; the result's text and usage resolve when the stream ends.
import { streamText } from "ai";
import { shopModel } from "./shop-model.ts";
const result = streamText({ model: shopModel, prompt: "My parcel never arrived" });
for await (const piece of result.textStream) {
process.stdout.write(`[${piece}]`);
}
console.log();
console.log(await result.text);
console.log((await result.usage).totalTokens);npx tsx stream.tsstreamText is not awaited: it starts the request and returns a result whose streams fill as the model writes. Each [piece] above is one chunk, a word, as the stand-in sends them. result.text and result.usage are promises that resolve once the stream is done.
Every event
import { streamText } from "ai";
import { shopModel } from "./shop-model.ts";
const result = streamText({
model: shopModel,
prompt: "I was charged twice",
onEnd: ({ finishReason }) => console.log("\nfinished:", finishReason),
});
for await (const part of result.fullStream) {
process.stdout.write(`${part.type} `);
}npx tsx events.tsfullStream yields every part, not only text: the start and end of the call and of each step, text boundaries, and, with tools, tool calls and results. onEnd runs after the last part. A UI uses these to show "looking up your order" while a tool runs.
Consume the stream
The request only makes progress while something reads the stream. A route that returns
result.toUIMessageStreamResponse() lets the browser read it; a script has to loop over it or await result.text.Try it yourself
- Print
part.textfortext-deltaparts only. - Add
onChunkand count chunks. - Remove the loop and only await
result.text.
Little by little, you're building something great.