Streaming structured content
Progressively render generated recipe fields while the model is still producing the object.
AI Tools · Function
useObject consumes a streamed JSON response and parses it into a structured object according to a supplied schema. It is intended for AI SDK UI applications that progressively render generated structured data.
useObject is an AI SDK UI hook for consuming a streamed JSON response and incrementally parsing it into an object that conforms to a supplied schema. Provide an endpoint that emits schema-matching JSON as chunked text, then use the returned object and controls to submit input and render progressive results.
useObject({ api, schema, id? })| Name | Type | Description |
|---|---|---|
api | string | Endpoint that generates the object and streams schema-matching JSON as chunked text. It may be a relative path beginning with / or an absolute URL. |
schema | Zod Schema | JSON Schema | Schema describing the complete object; use a Zod schema or JSON Schema with the jsonSchema function. |
id | string | Optional unique identifier. A random identifier is generated when omitted. |
A hook result for submitting input and consuming the progressively parsed structured object, including the generated object and request controls.
import { useObject } from 'ai';
import { z } from 'zod';
export default function Recipe() {
const { object, submit } = useObject({
api: '/api/recipe',
schema: z.object({
title: z.string(),
ingredients: z.array(z.string()),
}),
});
return (
<main>
<button onClick={() => submit('pasta')}>Generate</button>
{object?.title && <h1>{object.title}</h1>}
<ul>{object?.ingredients?.map(item => <li key={item}>{item}</li>)}</ul>
</main>
);
}This React component submits a prompt and renders the structured object as partial fields arrive.
Progressively render generated recipe fields while the model is still producing the object.
Build forms or dashboards that display typed JSON results as they arrive.
Classify or categorize input using a predefined structured output schema.
streamTextOutput.object()jsonSchemaStructuredObjectuseChatuseCompletion