Information extraction
Extract fields such as names, dates, or classifications from natural-language input into a predictable application object.
AI Tools · Function
generateObject creates structured, typed data from a prompt and schema with a language model. It supports several output strategies and validates schema-based results.
Call generateObject with a language model, an output strategy, and a schema. The schema is provided to the model to shape the response and is then used to validate the generated result, while the returned object exposes typed structured data.
generateObject({ model, output, schema, prompt })| Name | Type | Description |
|---|---|---|
model | LanguageModel | Language model used for generation. |
output | 'object' | 'array' | 'enum' | 'no-schema' | undefined | Output strategy; supported values include object, array, enum, and no-schema. Defaults to object. |
schema | Zod Schema | JSON Schema | Schema describing and validating generated data; use a Zod Schema or JSON Schema. |
prompt | string | Instruction or input used to generate the structured result. |
A result containing the generated structured object, including the example's object property.
import { generateObject } from 'ai';
import { z } from 'zod';
const { object } = await generateObject({
model: 'openai/gpt-4.1',
schema: z.object({
title: z.string(),
tags: z.array(z.string()),
}),
prompt: 'Create a title and tags for a guide to TypeScript generics.',
});
console.log(object);Generates a structured, schema-validated object from a prompt.
Extract fields such as names, dates, or classifications from natural-language input into a predictable application object.
Produce test fixtures or other machine-readable records that conform to a declared schema.
Return one of several structured labels or values for routing and decision logic.
generateObject produces structured data from a prompt instead of unstructured text. It uses a supplied schema both to describe the desired shape and to validate the generated result.
Follow-up: How would you handle incremental output? Use streamObject when the application needs structured data while generation is still in progress.
The default output strategy is object, which returns generated data as an object. The API also supports array, enum, and no-schema strategies.
Follow-up: When is no-schema useful? Choose it when structured generation is needed without providing a schema, while recognizing that schema validation is then unavailable.
A Zod schema or JSON Schema can describe the desired result. With JSON Schema, use the SDK's jsonSchema function to pass the schema in the documented form.
Follow-up: Why prefer a schema when possible? It gives the model explicit structure and lets the SDK validate the returned data.
streamObjectgenerateTextjsonSchemaZod SchemaJSON Schema