The Vercel AI SDK · Chapter 6 of 21

Streaming objects

By the end of this chapter you can stream structured output: watch an object fill in as the model writes it, get each item of a list the moment it is whole, and still end with a finished, checked object. You will stream a care plan with partialOutputStream, hand over plant suggestions one by one with elementStream, and learn why a partial object must be shown with care. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: The SDK on the server
  • 3 tasks
  • TypeScript
  • In the all labs plan

The first 2 chapters of The Vercel AI SDK are free. This one comes with the all labs plan.

Why you need this

Ananya wants two new things on the website. A care plan button: a customer who just bought a plant gets a week of care, day by day. And suggestion cards: "Plants for a bright kitchen window under ₹300" shows three plant cards a customer can tap.

Chapter 5 can make both, with generateText and Output. But the plan is 96 tokens of JSON, about two seconds at a model's pace, and the customer would stare at an empty panel until the last brace arrives. Chapter 2 fixed that for text with streamText. Structured output can stream too, but it needs more care than text: half an object is not a valid object.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    care-plan.ts: a care plan that fills in

    Export a zod schema CarePlan from care-plan.ts: plant (string) and week (an array of { day: string, task: string }), each field with a .describe(). Export an async function streamCarePlan(request, onUpdate) that calls streamText with lab('lab-chat'), the request as the prompt and output: Output.object({ schema: CarePlan }), calls onUpdate with each partial of result.partialOutputStream, and returns await result.output.

    Under if (import.meta.main), stream A week of care for my Money Plant counting the updates, then print N updates and a line DAY: TASK for each day. The Check wants every partial, growing, and the finished plan.

  2. Task 2

    suggestions.ts: suggestions one by one

    Export a zod schema Plant from suggestions.ts: name (string), price (number) and petSafe (boolean), each with a .describe(). Export an async function streamSuggestions(request, onPlant) that calls streamText with lab('lab-chat'), the request as the prompt and output: Output.array({ element: Plant }), calls onPlant with each plant of result.elementStream as it arrives, and returns await result.output.

    Under if (import.meta.main), stream Plants for a sunny balcony, safe for my dog and print N ms: NAME, ₹PRICE for each plant as it arrives. The Check streams at the real pace and wants each plant handed over before the list is finished.

  3. Task 3 · interview question

    Interview: ready-days.ts, show only whole days

    Stream a care plan: streamText with lab('lab-chat'), the prompt, exactly: A week of care for my Parlour Palm, and output: Output.object({ schema }) with plant (string) and week (an array of { day, task } strings), each with a .describe().

    Loop over result.partialOutputStream counting the updates. When partial.week has N entries, the first N - 1 are whole: print each day once, the moment it is whole, as day ready: DAY: TASK. After the loop, print the last day the same way from await result.output, then done: DAYS days in UPDATES updates.

    Ananya will ask: the partials are right there, so why not show each one to the customer as it comes?

What you will learn

  • streamText with output: the same Output.object and Output.array, streamed.
  • partialOutputStream: the object so far, growing with every piece.
  • elementStream: each item of a list, whole and checked, as soon as it is done.
  • result.output: the finished object, checked against the schema, at the end.
  • Partial objects are not checked: what that means for what you show.

More in The SDK on the server

Build "Streaming objects" yourself

HariBhari, a Bengaluru plant shop, wants Mali, a plant helper on its website. You build it end to end, from the first call to a chat that streams cards, asks before it adds to the cart and stays within budget. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of The Vercel AI SDK are free. This one comes with the all labs plan.