Skip to main content

Overview

The generateObject() function generates structured objects from chat models. With a Standard Schema, such as a Zod 4.2+ schema, core-ai validates the model output and returns a typed object. With plain JSON Schema, it returns the parsed JSON for you to validate.

Function signature

Parameters

ChatModel
required
The chat model instance to use for generation.
Message[]
required
Array of messages in the conversation. Must not be empty.
SchemaInput
required
Schema of the object to generate: a Standard Schema with JSON Schema support, such as a Zod 4.2+ schema, or plain JSON Schema. See Schemas for the supported libraries.
string
Optional name for the schema. Used by some providers for better results.
string
Optional description of the schema. Helps the model understand what to generate.
number
Sampling temperature (0-2).
number
Total output allowance for the response, covering reasoning tokens and the visible answer. When omitted, the model’s output ceiling applies. If the value cannot hold the reasoning budget of the requested effort, the call throws a ValidationError. See maxTokens.
number
Nucleus sampling parameter (0-1).
ReasoningConfig
Configuration for extended thinking/reasoning capabilities.
GenerateProviderOptions
Provider-specific options, namespaced by provider name.
AbortSignal
AbortSignal for cancelling the request.

Return value

Returns a Promise<GenerateObjectResult<TSchema>> with the following properties:
InferSchemaOutput<TSchema>
For a Standard Schema, the validated output of the schema, typed with its output type. For plain JSON Schema, the parsed JSON, typed unknown and not validated.
FinishReason
Why generation stopped: 'stop', 'length', 'tool-calls', 'content-filter', or 'unknown'.
ChatUsage
Token usage statistics including input/output tokens and cache details.

Examples

Basic object generation

Complex schema

With configuration

Plain JSON Schema

Pass plain JSON Schema as schema to get the parsed JSON, typed unknown and not validated. See Plain JSON Schema for an example.

Error handling

generateObject() throws:
  • StructuredOutputValidationError when the output does not match a Standard Schema. issues holds the schema library’s messages, formatted as path.to.key: message. When the schema’s validator throws instead, such as a transform calling new URL(), its error is the cause.
  • StructuredOutputParseError when the output is not valid JSON.
  • StructuredOutputNoObjectGeneratedError when the model returns no object.
  • ValidationError when the schema cannot be converted to JSON Schema, before any provider call.

Type safety

The type of object is inferred from the output type of a Standard Schema. For plain JSON Schema it is unknown.