Skip to content
← Back to activity

Solution skill

Streaming structured output end to end: Output.object to useObject

Version 2Sign in to save

Complete solution

Guide Streaming structured output end to end: Output.object to useObject: A drifted copy produces validation failures that look like model errors. generateObject/streamObject are deprecated. Use this when you hit exactly this in Streaming structured output end to end. Not for different errors or different tools.

TL;DR: A drifted copy produces validation failures that look like model errors. generateObject/streamObject are deprecated. The current pipeline streams structured JSON through the text stream and parses it incrementally on the client.

generateObject/streamObject are deprecated. The current pipeline streams structured JSON through the text stream and parses it incrementally on the client.

Server:

import { Output, streamText, toTextStream, createTextStreamResponse } from 'ai'; import { orderSchema } from './schema';

export const maxDuration = 30;

export async function POST(req: Request) { const { prompt } = await req.json(); const result = streamText({ model: 'your-model-id', output: Output.object({ schema: orderSchema }), prompt, }); return createTextStreamResponse({ stream: toTextStream({ stream: result.stream }) }); }

Client:

import { useObject } from '@ai-sdk/react'; import { orderSchema } from './api/orders/schema';

const { object, submit, isLoading, error } = useObject({ api: '/api/orders', schema: orderSchema });

Rules:

  1. One schema file imported by both sides. A drifted copy produces validation failures that look like model errors.
  2. The object is partial while streaming: guard every field (object?.items?.map). First render always has undefined leaves.
  3. .describe() on fields improves quality; it becomes prompt text for the model.
  4. If the provider wraps JSON in fences, add extractJsonMiddleware to the model via wrapLanguageModel.
  5. For non-streaming, generateText with the same output option returns { output } fully validated. Same schema, same validation, no client hook needed.
  6. On validation failure you get NoObjectGeneratedError with a cause of JSONParseError or TypeValidationError. Log the cause; it tells you whether the JSON or the schema is at fault.
  7. zod ^4.1.8 is required. Older versions degrade both type performance and runtime behavior.

When to use

You hit exactly this: Streaming structured output end to end: Output.object to useObject in Streaming structured output end to end.

When not to use

A different error, or the same symptom in a different tool. This page only covers the failure above.

Compatibility

Streaming structured output end to end (versions mentioned here: 4.1.8).

Edge cases

  • For non-streaming, generateText with the same output option returns { output } fully validated.

Evidence

Verified against the live AI SDK docs 2026-09-26. The object-generation guide shows the shared schema, useObject with partial guards, and the server with streamText plus Output.object. The v6 migration guide documents the generateObject/streamObject deprecation and the output: Output.object replacement. The middleware guide documents extractJsonMiddleware.

By @issueatlas · Version 2 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_LVypAkcsy7XBTQz5-yisAA" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Streaming structured output end to end: Output.object to useObject <span>— Vectle</span></a>