Fix compatibility with pydantic 2.12+ Field defaults in Annotated types#1607
Closed
Fix compatibility with pydantic 2.12+ Field defaults in Annotated types#1607
Conversation
When using Annotated[T, Field(default)] without an explicit parameter default (= value), pydantic 2.12+ changed FieldInfo.from_annotated_attribute() to overwrite the Field's default with PydanticUndefined, incorrectly marking fields as required in the JSON schema. This fix checks if a Field with a default exists in the Annotated metadata and uses FieldInfo.from_annotation() to preserve that default, while still using from_annotated_attribute() for the standard case where parameter defaults take precedence. The fix maintains backward compatibility with pydantic 2.11 and earlier while ensuring correct behavior with 2.12+.
9 tasks
Contributor
Contributor
Author
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.
This PR fixes a compatibility issue with pydantic 2.12+ where fields using
Annotated[T, Field(default)]without explicit parameter defaults were incorrectly marked as required in JSON schemas.Motivation and Context
Pydantic 2.12.0 introduced a breaking change in how
FieldInfo.from_annotated_attribute()handles defaults. When passingPydanticUndefinedas the second parameter, it now overwrites any default value specified insideField()within anAnnotatedtype. This causes fields that should be optional (with defaults fromField()) to be incorrectly marked as required in the generated JSON schema.The issue affects function parameters like:
With pydantic 2.12+, this parameter would be marked as required in the JSON schema, even though
Field(1)specifies a default value.How Has This Been Tested?
test_complex_function_json_schemavalidates the fix (tests fields withField(default)in Annotated types)test_func_metadata.pypassThe solution detects when an
Annotatedtype contains aFieldInfowith a default and usesFieldInfo.from_annotation()to preserve that default, while continuing to usefrom_annotated_attribute()for standard cases where parameter defaults should take precedence.Breaking Changes
None. This is a bug fix that maintains backward compatibility with pydantic 2.11 while adding forward compatibility with pydantic 2.12+.
Types of changes
Checklist
Additional context
This fix prepares the codebase for pydantic 2.12+, which will be included in dependency updates. The change is minimal and focused on preserving the intended behavior: defaults specified in
Field()withinAnnotatedtypes should be respected in the JSON schema.The implementation checks for the presence of a
FieldInfowith a non-undefined default in theAnnotatedmetadata and chooses the appropriateFieldInfoconstruction method accordingly.