It's nice being able to stream text from an LLM, but sometimes you want that output to be more structured. For instance, you may want it to provide multiple queries that you search Google with.
In other words, sometimes we want text back from the LLM, but sometimes we want objects. We want to define the shapes of those objects, pass that shape to the LLM, and then get that shape back.
Our starting code calls streamText to generate a story about an imaginary planet:
const stream = streamText({model,prompt: "Give me the first paragraph of a story about an imaginary planet.",})
We then write that to stdout and await the final text:
for await (const chunk of stream.textStream) {process.stdout.write(chunk)}const finalText = await stream.text
Our first to-do is to call streamObject from the AI SDK, passing in:
Here's where we need to add our code:
// TODO: Replace this with a call to streamObject, passing:// - The model, same as above// - The prompt, asking for facts about the imaginary planet,// passing in the finalText as the story// - The schema, which should be an object with a facts property// that is an array of stringsconst factsResult = TODO
The schema we need to define should be an object with a facts property that's an array of strings. LLMs are very good at working with Zod schemas, so autocomplete should help you define this properly.
After we get our structured data stream, we'll log each chunk of the partial object stream:
for await (const chunk of factsResult.partialObjectStream) {console.log(chunk)}
This will show the object as it's being constructed by the LLM in real-time.
When you run this exercise, you should see:
The final result should be an array of facts about the imaginary planet described in the paragraph that was generated.
Call the streamObject function and store the result in factsResult
streamObject from 'ai' if not already imported Pass the model parameter to streamObject (same as used with streamText)
Create a prompt that asks for facts about the imaginary planet
finalText in your prompt so the LLM knows what story to reference Define a Zod schema for an object with a facts property that is an array of strings
z from 'zod' if not already importedz.array and z.string functions to define the schemaz.describe function to describe the schema, if you like Run the exercise with pnpm run dev to see both the streaming text and the structured object being built