Skip to content

StructuredDict passed into run cannot handle nested JsonSchema #2466

@pascalwhoop

Description

@pascalwhoop

Initial Checks

Description

# %%
from pydantic import BaseModel
from pydantic_ai import Agent, StructuredDict


class Tire(BaseModel):
    brand: str


class Car(BaseModel):
    tires: list[Tire]


car_schema = Car.model_json_schema()


structured_dict = StructuredDict(car_schema)
print(structured_dict)
agent = Agent("gpt-4o-mini", output_type=structured_dict)
result = agent.run("What is the make of the car?")


# %%
import json
print(json.dumps(car_schema, indent=4))
{
    "$defs": {
        "Tire": {
            "properties": {
                "brand": {
                    "title": "Brand",
                    "type": "string"
                }
            },
            "required": [
                "brand"
            ],
            "title": "Tire",
            "type": "object"
        }
    },
    "properties": {
        "tires": {
            "items": {
                "$ref": "#/$defs/Tire"
            },
            "title": "Tires",
            "type": "array"
        }
    },
    "required": [
        "tires"
    ],
    "title": "Car",
    "type": "object"

But run() crashes with

KeyError: '#/$defs/Tire'

Example Code

Python, Pydantic AI & LLM client version

0.6.0, python 3.12

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions