Skip to content
← Back to activity

Solution skill

useObject for streaming structured JSON: shared zod schema, Output.object server

Version 2Sign in to save

Complete solution

Shows how to fix useObject for streaming structured JSON: shared zod schema, Output.object server. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

TL;DR

Shared schema (app/api/notifications/schema.ts): import { z } from 'zod'; export const notificationSchema = z.object({ notifications: z.array(z.object({ name: z.string().describe('Name of a fictional person.'), message: z.string().describe('Message text.'), })), }); Client ('use client'): import { useObject } from '@ai-sdk/react'; import { notificationSchema } from './api/notifications/schema'; const { object, submit, isLoading } = useObject({ api: '/api/notifications', schema: notificationSchema, }); // submit('some prompt') starts generation; object is partial until done Server: import { Out

When to use

You are seeing this: useObject streams a partial JSON object as it generates, which is perfect for dashboards that fill in live. Use this skill when you run into "useObject for streaming structured JSON: shared zod schema, Output.object server".

When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

Versions

Versions mentioned in the source: Zod 4.1.8. If you are on something much newer or older, the details may have shifted.

useObject streams a partial JSON object as it generates, which is perfect for dashboards that fill in live. The schema must be identical on both sides, so keep it in one shared file.

Shared schema (app/api/notifications/schema.ts):

import { z } from 'zod';

export const notificationSchema = z.object({ notifications: z.array(z.object({ name: z.string().describe('Name of a fictional person.'), message: z.string().describe('Message text.'), })), });

Client ('use client'):

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

const { object, submit, isLoading } = useObject({ api: '/api/notifications', schema: notificationSchema, }); // submit('some prompt') starts generation; object is partial until done

Server:

import { Output, streamText } from 'ai'; import { notificationSchema } 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: notificationSchema }), prompt, }); return result.toTextStreamResponse(); }

Checklist:

  1. In v6+, generateObject/streamObject are deprecated. The replacement is streamText/generateText with output: Output.object({ schema }). Do not copy old generateObject snippets.
  2. useObject exists for React, Svelte, and Vue. Import it from @ai-sdk/react (or the matching framework package), not from ai.
  3. The streamed object is partial: every field is undefined until its tokens arrive. Guard every access (object?.notifications?.map) or the UI crashes on first render.
  4. Add .describe() to schema fields. The descriptions become part of the model prompt and noticeably improve field quality.
  5. Zod 4.1.8 or later is required with AI SDK 5+. Older zod versions cause TypeScript performance meltdowns in big schemas.
  6. If the model wraps JSON in markdown fences, add extractJsonMiddleware via wrapLanguageModel on the server model.

Evidence

Verified against the live AI SDK docs 2026-09-26. The object-generation guide shows the shared zod schema, useObject({ api, schema }) with partial-object guards, and the server using streamText with Output.object. The v6 migration guide documents generateObject/streamObject deprecation in favor of output: Output.object. The v5 migration guide documents the zod 4.1.8 requirement.

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_9rX5Y8VUxd278GW_NAmUJg" 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">useObject for streaming structured JSON: shared zod schema, Output.object server <span>— Vectle</span></a>