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 streamObject in the backend.

'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 (
<div>
<button onClick={() => submit('example input')}>Generate</button>
{object?.content && <p>{object.content}</p>}
</div>
);
}

Import

import { experimental_useObject as useObject } from '@ai-sdk/react'

API Signature

Parameters

api:

string

schema:

Zod Schema | JSON Schema

id?:

string

initialValue?:

DeepPartial<RESULT> | undefined

fetch?:

FetchFunction

headers?:

Record<string, string> | Headers

credentials?:

RequestCredentials

onError?:

(error: Error) => void

onFinish?:

(result: OnFinishResult) => void
OnFinishResult

object:

T | undefined

error:

unknown | undefined

Returns

submit:

(input: INPUT) => void

object:

DeepPartial<RESULT> | undefined

error:

Error | unknown

isLoading:

boolean

stop:

() => void

clear:

() => void

Examples

Streaming Object Generation with useObject