@@ -70,6 +70,13 @@ class Person(BaseModel):
7070 address : Optional [Address ] = None
7171
7272
73+ class AnnotatedAddress (BaseModel ):
74+ """A Pydantic model whose fields carry Annotated metadata."""
75+
76+ city : Annotated [str , Field (description = "City name" )]
77+ zip_code : Annotated [str , Field (description = "US ZIP code" , pattern = r"^\d{5}$" )]
78+
79+
7380@pyd_dataclass
7481class Window :
7582 """A Pydantic dataclass for testing."""
@@ -703,6 +710,42 @@ def save(
703710 [{"$ref" : "#/$defs/Address" }, {"type" : "null" }],
704711 )
705712
713+ def test_optional_annotated_field_metadata (self ):
714+ """Test Optional[Annotated[T, Field(...)]] keeps metadata inside the anyOf."""
715+
716+ def forecast (
717+ days : Optional [
718+ Annotated [int , Field (description = "Number of days" , ge = 1 )]
719+ ] = None ,
720+ ) -> str :
721+ return "ok"
722+
723+ decl = build_function_declaration_with_json_schema (forecast )
724+ days_schema = decl .parameters_json_schema ["properties" ]["days" ]
725+
726+ self .assertIsNone (days_schema ["default" ])
727+ self .assertEqual (
728+ days_schema ["anyOf" ],
729+ [
730+ {"description" : "Number of days" , "minimum" : 1 , "type" : "integer" },
731+ {"type" : "null" },
732+ ],
733+ )
734+
735+ def test_nested_model_annotated_field_metadata (self ):
736+ """Test a nested model's Annotated field metadata reaches its $defs entry."""
737+
738+ def register (address : AnnotatedAddress ) -> str :
739+ return "ok"
740+
741+ decl = build_function_declaration_with_json_schema (register )
742+ address_def = decl .parameters_json_schema ["$defs" ]["AnnotatedAddress" ]
743+ props = address_def ["properties" ]
744+
745+ self .assertEqual (props ["city" ]["description" ], "City name" )
746+ self .assertEqual (props ["zip_code" ]["description" ], "US ZIP code" )
747+ self .assertEqual (props ["zip_code" ]["pattern" ], r"^\d{5}$" )
748+
706749 def test_annotated_nested_list_constraints (self ):
707750 """Test Annotated metadata on both a list and its item type."""
708751
0 commit comments