@@ -78,6 +78,12 @@ class CountryInfoWithAnyOf(pydantic.BaseModel):
7878 restaurants_per_capita : Union [int , float ]
7979
8080
81+ class _Score (pydantic .BaseModel ):
82+ """A generic score container."""
83+
84+ point : int
85+
86+
8187@pytest .fixture
8288def client (use_vertex ):
8389 if use_vertex :
@@ -693,3 +699,120 @@ def test_t_schema_sets_property_ordering_for_schema_type(client):
693699
694700 transformed_schema = _transformers .t_schema (client , schema )
695701 assert transformed_schema .property_ordering == ['name' , 'population' ]
702+
703+
704+ @pytest .mark .parametrize ('use_vertex' , [True , False ])
705+ def test_t_schema_keeps_field_descriptions_on_shared_nested_model (client ):
706+ """Each field that references a shared model keeps its own description."""
707+
708+ class Review (pydantic .BaseModel ):
709+ clarity : _Score = pydantic .Field (description = 'Rate clarity only.' )
710+ accuracy : _Score = pydantic .Field (description = 'Rate factual accuracy only.' )
711+
712+ schema = _transformers .t_schema (client , Review )
713+
714+ assert schema .properties ['clarity' ].description == 'Rate clarity only.'
715+ assert (
716+ schema .properties ['accuracy' ].description == 'Rate factual accuracy only.'
717+ )
718+ assert schema .properties ['clarity' ].properties ['point' ].type == 'INTEGER'
719+ assert schema .properties ['accuracy' ].properties ['point' ].type == 'INTEGER'
720+
721+
722+ @pytest .mark .parametrize ('use_vertex' , [True , False ])
723+ def test_t_schema_keeps_field_description_on_optional_nested_model (client ):
724+ """An Optional nested-model field keeps its description when flattened."""
725+
726+ class Review (pydantic .BaseModel ):
727+ clarity : Union [_Score , None ] = pydantic .Field (
728+ default = None , description = 'Rate clarity only.'
729+ )
730+
731+ schema = _transformers .t_schema (client , Review )
732+
733+ assert schema .properties ['clarity' ].description == 'Rate clarity only.'
734+ assert schema .properties ['clarity' ].nullable
735+
736+
737+ @pytest .mark .parametrize ('use_vertex' , [True , False ])
738+ def test_t_schema_keeps_field_title_on_nested_model (client ):
739+ """A nested-model field keeps its own title instead of the model name."""
740+
741+ class Review (pydantic .BaseModel ):
742+ clarity : _Score = pydantic .Field (title = 'Clarity' )
743+
744+ schema = _transformers .t_schema (client , Review )
745+
746+ assert schema .properties ['clarity' ].title == 'Clarity'
747+
748+
749+ @pytest .mark .parametrize ('use_vertex' , [True , False ])
750+ def test_t_schema_uses_model_docstring_without_field_description (client ):
751+ """Without field annotations, the model docstring and name still apply."""
752+
753+ class Review (pydantic .BaseModel ):
754+ clarity : _Score
755+
756+ schema = _transformers .t_schema (client , Review )
757+
758+ assert schema .properties ['clarity' ].description == (
759+ 'A generic score container.'
760+ )
761+ assert schema .properties ['clarity' ].title == '_Score'
762+
763+
764+ @pytest .mark .parametrize ('use_vertex' , [True , False ])
765+ def test_t_schema_ignores_unsupported_siblings_of_ref (client ):
766+ """Unsupported '$ref' siblings are dropped while the description is kept."""
767+
768+ class Review (pydantic .BaseModel ):
769+ clarity : _Score = pydantic .Field (
770+ description = 'Rate clarity only.' , examples = [{'point' : 3 }]
771+ )
772+
773+ schema = _transformers .t_schema (client , Review )
774+
775+ assert schema .properties ['clarity' ].description == 'Rate clarity only.'
776+
777+
778+ @pytest .mark .parametrize ('use_vertex' , [True , False ])
779+ def test_process_schema_keeps_ref_sibling_description (client ):
780+ """Each '$ref' property gets its own copy carrying its own description."""
781+ schema = {
782+ 'type' : 'OBJECT' ,
783+ 'properties' : {
784+ 'clarity' : {
785+ '$ref' : '#/$defs/Score' ,
786+ 'description' : 'Rate clarity only.' ,
787+ },
788+ 'accuracy' : {
789+ '$ref' : '#/$defs/Score' ,
790+ 'description' : 'Rate factual accuracy only.' ,
791+ },
792+ },
793+ '$defs' : {
794+ 'Score' : {
795+ 'type' : 'OBJECT' ,
796+ 'description' : 'A generic score container.' ,
797+ 'properties' : {'point' : {'type' : 'INTEGER' }},
798+ }
799+ },
800+ }
801+
802+ _transformers .process_schema (schema , client , order_properties = False )
803+
804+ assert schema == {
805+ 'type' : 'OBJECT' ,
806+ 'properties' : {
807+ 'clarity' : {
808+ 'type' : 'OBJECT' ,
809+ 'description' : 'Rate clarity only.' ,
810+ 'properties' : {'point' : {'type' : 'INTEGER' }},
811+ },
812+ 'accuracy' : {
813+ 'type' : 'OBJECT' ,
814+ 'description' : 'Rate factual accuracy only.' ,
815+ 'properties' : {'point' : {'type' : 'INTEGER' }},
816+ },
817+ },
818+ }
0 commit comments