Parse model output directly into your own Pydantic models with automatic validation and retries.
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.
generate_object
solves this by validating the response against a Pydantic schema and retries when needed.
generate_text
plus:
Name | Type | Required | Description |
---|---|---|---|
schema | Type[BaseModel] | ✓ | Pydantic model defining the desired output shape. |
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 metadatagenerate_object
automatically retries when the model output doesn’t match the schema:
response_format
)
generate_object
uses it automatically and falls back to JSON-parsing otherwise – so your code
stays portable.