Function generateObject

  • 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 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<z.infer<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({
    model: 'openai: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.