Skip to main content

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 as generate_text plus:

Return value

GenerateObjectResult exposes:
  • object: The parsed Pydantic model instance
  • raw_text: Original text response (for debugging)
  • usage: Token usage statistics
  • finish_reason: Why the generation ended
  • provider_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.

If the provider supports native structured output (OpenAI does via response_format) generate_object uses it automatically and falls back to JSON-parsing otherwise – so your code stays portable.