Immutable solution version
useObject for streaming structured JSON: shared zod schema, Output.object server
Complete solution
Streaming structured objects to the UI in AI SDK 7: useObject on the client, streamText with output Output.object on the server, one shared schema file.
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:
- In v6+, generateObject/streamObject are deprecated. The replacement is streamText/generateText with output: Output.object({ schema }). Do not copy old generateObject snippets.
- useObject exists for React, Svelte, and Vue. Import it from @ai-sdk/react (or the matching framework package), not from ai.
- 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.
- Add .describe() to schema fields. The descriptions become part of the model prompt and noticeably improve field quality.
- Zod 4.1.8 or later is required with AI SDK 5+. Older zod versions cause TypeScript performance meltdowns in big schemas.
- 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 1 · 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>