Overview
generate_object parses model output directly into your own Pydantic models with automatic validation and retries. It solves the problem of LLM hallucination by validating responses against a schema and retrying when needed.
Why structured output?
LLMs love to hallucinate – a missing comma can break your JSON parser.generate_object solves this by validating the response against a Pydantic schema and retries when needed.
Basic usage
generate_object.py
Parameters
Same asgenerate_text plus:
Return value
GenerateObjectResult exposes:
object: The parsed Pydantic model instanceraw_text: Original text response (for debugging)usage: Token usage statisticsfinish_reason: Why the generation endedprovider_metadata: Provider-specific metadata
Examples
Basic object generation
With complex schemas
With system instructions
With custom parameters
Error handling
generate_object automatically retries when the model output doesn’t match the schema:
Tool-calling with objects
See the dedicated Tool page for a complete walkthrough.