--- title: useObject description: API reference for the useObject hook. --- # `experimental_useObject()` `useObject` is an experimental feature and only available in React. Allows you to consume text streams that represent a JSON object and parse them into a complete object based on a Zod schema. You can use it together with [`streamObject`](/docs/reference/ai-sdk-core/stream-object) in the backend. ```tsx 'use client'; import { experimental_useObject as useObject } from 'ai/react'; export default function Page() { const { object, submit } = useObject({ api: '/api/use-object', schema: z.object({ content: z.string() }), }); return (
{object?.content &&

{object.content}

}
); } ``` ## Import ## API Signature ### Parameters ', description: 'A Zod schema that defines the shape of the complete object.', }, { name: 'id?', type: 'string', description: 'Allows you to consume text streams that represent a JSON object and parse them into a complete object based on a Zod schema.', }, { name: 'initialValue?', type: 'DeepPartial | undefined', description: 'An optional value for the initial object.', }, { name: 'fetch', type: 'FetchFunction', optional: true, description: 'Optional. A custom fetch function to be used for the API call. Defaults to the global fetch function.', }, { name: 'onError', type: '(error: Error) => void', optional: true, description: 'Optional. Callback function to be called when an error is encountered.', }, { name: 'onFinish', type: '(result: OnFinishResult) => void', isOptional: true, description: 'Called when the streaming response has finished.', properties: [ { type: 'OnFinishResult', parameters: [ { name: 'object', type: 'T | undefined', description: 'The generated object (typed according to the schema). Can be undefined if the final object does not match the schema.', }, { name: 'error', type: 'unknown | undefined', description: 'Optional error object. This is e.g. a TypeValidationError when the final object does not match the schema.', }, ], }, ], }, ]} /> ### Returns void', description: 'Calls the API with the provided input as JSON body.', }, { name: 'object', type: 'DeepPartial | undefined', description: 'The current value for the generated object. Updated as the API streams JSON chunks.', }, { name: 'error', type: 'undefined | unknown', description: 'The error object if the API call fails.', }, { name: 'isLoading', type: 'boolean', description: 'Boolean flag indicating whether a request is currently in progress.', }, { name: 'stop', type: '() => void', description: 'Function to abort the current API request.', }, ]} /> ## Examples