Skip to content

Commit c839ef3

Browse files
authored
Accept recursive objects in return_type (#1017)
1 parent 654d96d commit c839ef3

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

pydantic_ai_slim/pydantic_ai/_utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ def check_object_json_schema(schema: JsonSchemaValue) -> ObjectJsonSchema:
4848

4949
if schema.get('type') == 'object':
5050
return schema
51+
elif schema.get('$ref') is not None:
52+
return schema.get('$defs', {}).get(schema['$ref'][8:]) # This removes the initial "#/$defs/".
5153
else:
5254
raise UserError('Schema must be an object')
5355

tests/test_utils.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,30 @@ def test_check_object_json_schema():
4444
object_schema = {'type': 'object', 'properties': {'a': {'type': 'string'}}}
4545
assert check_object_json_schema(object_schema) == object_schema
4646

47+
ref_schema = {
48+
'$defs': {
49+
'JsonModel': {
50+
'properties': {
51+
'type': {'title': 'Type', 'type': 'string'},
52+
'items': {'anyOf': [{'$ref': '#/$defs/JsonModel'}, {'type': 'null'}]},
53+
},
54+
'required': ['type', 'items'],
55+
'title': 'JsonModel',
56+
'type': 'object',
57+
}
58+
},
59+
'$ref': '#/$defs/JsonModel',
60+
}
61+
assert check_object_json_schema(ref_schema) == {
62+
'properties': {
63+
'type': {'title': 'Type', 'type': 'string'},
64+
'items': {'anyOf': [{'$ref': '#/$defs/JsonModel'}, {'type': 'null'}]},
65+
},
66+
'required': ['type', 'items'],
67+
'title': 'JsonModel',
68+
'type': 'object',
69+
}
70+
4771
array_schema = {'type': 'array', 'items': {'type': 'string'}}
4872
with pytest.raises(UserError, match='^Schema must be an object$'):
4973
check_object_json_schema(array_schema)

0 commit comments

Comments
 (0)