Structured Output
with_structured_output() turns any model into a typed data extractor. Pydantic schemas, JSON mode, and provider-specific strategies.
Quick Reference
- →model.with_structured_output(Schema) returns typed Pydantic objects
- →Works with Anthropic, OpenAI, Gemini — same API everywhere
- →Supports Pydantic BaseModel, TypedDict, and JSON Schema dict
- →Provider strategies: tool_calling (default), json_mode, json_schema
with_structured_output()
The standard way to get typed, validated data from an LLM. Pass a schema and get a typed object back. No parsing code, no format instructions, no regex — just define a schema and call invoke().
with_structured_output() accepts three different schema formats. Here's what each one is and when to use it:
Pydantic validates the output — if the model returns a string where you expect an int, it will raise a ValidationError rather than silently passing bad data through. Field descriptions are also sent to the model as instructions, which improves extraction accuracy.
Use TypedDict when you don't want a Pydantic dependency, or when you're integrating with code that expects plain dicts. The tradeoff: no runtime validation — if the model returns the wrong type for a field, it won't raise an error.
Use a raw JSON Schema dict when your schema is built dynamically at runtime (e.g., from a database or config file), or when you need features not supported by Pydantic's JSON Schema conversion. For most cases, Pydantic BaseModel is cleaner and safer.