Overview
ThegenerateObject() function generates structured, type-safe objects from chat models using Zod schema validation. This ensures the model output conforms to your expected data structure.
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.
z.ZodType
required
Zod schema that defines the structure of the object to generate.
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
Maximum number of tokens to generate.
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 aPromise<GenerateObjectResult<TSchema>> with the following properties:
z.infer<TSchema>
The generated object, fully typed according to your Zod schema.
FinishReason
Why generation stopped:
'stop', 'length', 'tool-calls', 'content-filter', or 'unknown'.ChatUsage
Token usage statistics including input/output tokens and cache details.