Fix #7203: schema_to_pydantic_model loses $defs type information (array -> object)#7259
Open
cobyfrombrooklyn-bot wants to merge 1 commit intomicrosoft:mainfrom
Open
Fix #7203: schema_to_pydantic_model loses $defs type information (array -> object)#7259cobyfrombrooklyn-bot wants to merge 1 commit intomicrosoft:mainfrom
cobyfrombrooklyn-bot wants to merge 1 commit intomicrosoft:mainfrom
Conversation
When $defs entries have non-object types (e.g. array, integer, string), _process_definitions was incorrectly calling json_schema_to_pydantic on them, which always creates a BaseModel with properties. This caused array types like list[str] to be converted into empty object models. Added _resolve_non_object_def() that detects non-object $defs entries and resolves them to their correct Python types using _extract_field_type(). Only object-type definitions are processed as Pydantic models. Fixes microsoft#7203
Contributor
|
@cobyfrombrooklyn-bot please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
schema_to_pydantic_modelincorrectly converts non-object$defsentries (like arrays) into empty object models. When a$defsentry hastype: "array"withitems: {"type": "string"}, the function creates a Pydantic BaseModel with no properties instead of preserving the array type.This was reported in #7203 with a clear repro case using Pydantic v2's
type TaskData = list[str].Root Cause
_process_definitions()callsjson_schema_to_pydantic()for every$defsentry, which ultimately calls_json_schema_to_model(). That method always creates a BaseModel fromproperties, but non-object definitions (arrays, primitives) have noproperties, resulting in an empty model.Fix
Added
_resolve_non_object_def()that checks if a$defsentry has a non-object type. If so, it resolves it to the correct Python type using the existing_extract_field_type()method (which already handles arrays, constrained integers, etc.). Only object-type definitions are processed as Pydantic models.Tests
Added 3 test cases in
test_json_to_pydantic.py:test_defs_with_array_type_preserved- Reproduces the exact bug from schema_to_pydantic_model loses $defs type information (array → object) #7203. Verifies that a$defsentry withtype: array, items: {type: string}is correctly preserved asList[str].test_defs_with_integer_type_preserved- Tests primitive type definitions (type: integerwith constraints) in$defs.test_defs_with_mixed_object_and_array_types- Tests schemas with both object and non-object$defsentries to ensure they coexist correctly.All 78 tests pass (75 existing + 3 new). Tested on macOS ARM (Apple Silicon).