Skip to main content

Function: generateObject()

generateObject<T>(opts): Promise<GenerateObjectResult<output<T>>>

Defined in: packages/agentos/src/api/generateObject.ts:617

Generates a structured object by forcing the LLM to produce JSON matching a Zod schema.

Combines schema-aware prompt engineering with optional provider-native JSON mode and automatic retry-with-feedback to reliably extract typed data from unstructured text.

Type Parameters​

T​

T extends ZodType<unknown, unknown, $ZodTypeInternals<unknown, unknown>>

The Zod schema type. The returned object field is inferred as z.infer<T>.

Parameters​

opts​

GenerateObjectOptions<T>

Generation options including the Zod schema, prompt/messages, and optional provider/model overrides.

Returns​

Promise<GenerateObjectResult<output<T>>>

A promise resolving to the validated object, raw text, usage, and metadata.

Throws​

When all retries are exhausted without producing valid JSON that passes Zod validation.

Throws​

When provider resolution fails (missing API key, unknown provider, etc.).

Example​

import { z } from 'zod';
import { generateObject } from '@framers/agentos';

const { object } = await generateObject({
provider: 'openai',
model: 'gpt-4o',
schema: z.object({ name: z.string(), age: z.number() }),
prompt: 'Extract: "John is 30 years old"',
});

console.log(object.name); // "John"
console.log(object.age); // 30

See​

  • streamObject for streaming partial objects as they build up.
  • generateText for plain text generation without schema constraints.