How to Stream Object in AI SDK by Vercel
AI SDK by Vercel is library helps us to build AI powered application pretty easily. It abstracts lots of things for us so that we can focus on what truly matters without having to reinvent the wheel.
Dependency
npm install ai
# In this example, we'll use OpenAI so we need to install model provider as well
npm install @ai-sdk/openai
# Zod
npm install zod
Here is a simple example of AI SDK using OpenAI. If you notice, changing a model is just one line rather than updating lot of code. That’s the main advantage that AI SDK brings in.
import { generateText } from "ai";
import { openai } from "@ai-sdk/openai";
const { text } = await generateText({
// Ensure OPENAI_API_KEY environment variable is set
model: openai("gpt-4o-mini"),
system: "You are a friendly assistant!",
prompt: "Why is the sky blue?",
});
console.log(text);
Usage - StreamObject
Sometime you might want to stream a typed structured object when using a language model.
Now let’s see a case in which you might want to stream an object
import { streamObject } from "ai";
import { openai } from "@ai-sdk/openai";
import { z } from "zod";
const mathResponseSchema = z.object({
steps: z.array(
z.object({
explanation: z.string(),
output: z.string(),
})
),
finalAnswer: z.string(),
});
const { partialObjectStream } = streamObject({
model: openai("gpt-4o-mini"),
schema: mathResponseSchema,
system: "You are a helpful math tutor.",
prompt: "solve 8x + 31 = 2",
});
for await (const partialObject of partialObjectStream) {
console.clear();
console.log(partialObject);
}
References
Happy streaming object!