Skip to content

Commit eb160dc

Browse files
committed
Updated failing tests and flatten logic
1 parent 17c7434 commit eb160dc

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

tests/models/test_openai.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1946,6 +1946,43 @@ def test_openai_transformer_with_recursive_ref() -> None:
19461946
assert 'foo' in result['required']
19471947

19481948

1949+
def test_openai_transformer_fallback_when_defs_missing() -> None:
1950+
"""Test fallback path when root_key is not in result['$defs'] (line 165).
1951+
1952+
This tests the safety net fallback that shouldn't happen in normal flow.
1953+
The fallback uses self.defs (original schema) when the transformed $defs
1954+
doesn't contain the root_key. This edge case is simulated using a mock.
1955+
"""
1956+
from unittest.mock import patch
1957+
1958+
from pydantic_ai.profiles.openai import OpenAIJsonSchemaTransformer
1959+
1960+
schema: dict[str, Any] = {
1961+
'$ref': '#/$defs/MyModel',
1962+
'$defs': {
1963+
'MyModel': {
1964+
'type': 'object',
1965+
'properties': {'foo': {'type': 'string'}},
1966+
'required': ['foo'],
1967+
},
1968+
},
1969+
}
1970+
1971+
transformer = OpenAIJsonSchemaTransformer(schema, strict=True)
1972+
1973+
# Simulate edge case: super().walk() returns $defs without root_key
1974+
# This shouldn't happen in normal flow, but we test the fallback path
1975+
with patch.object(
1976+
transformer.__class__.__bases__[0],
1977+
'walk',
1978+
return_value={'$defs': {'OtherModel': {'type': 'object'}}},
1979+
):
1980+
result = transformer.walk()
1981+
# Fallback should use self.defs.get(root_key) which contains MyModel
1982+
assert isinstance(result, dict)
1983+
assert 'properties' in result or 'type' in result
1984+
1985+
19491986
def test_openai_transformer_flattens_allof() -> None:
19501987
"""Test that OpenAIJsonSchemaTransformer flattens allOf schemas."""
19511988
from pydantic_ai._json_schema import JsonSchema

0 commit comments

Comments
 (0)