Skip to content

Commit 4c1abcc

Browse files
google-genai-botcopybara-github
authored andcommitted
fix: Don't crash parsing responses with JSON objects in Any-typed fields
PiperOrigin-RevId: 1003203859
1 parent 6cd176e commit 4c1abcc

2 files changed

Lines changed: 119 additions & 6 deletions

File tree

‎google/genai/_common.py‎

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,30 @@ def _is_struct_type(annotation: type) -> bool:
326326
return key_type is str and value_type is typing.Any
327327

328328

329+
def _as_base_model_type(
330+
annotation: Any,
331+
) -> Optional[typing.Type[pydantic.BaseModel]]:
332+
"""Returns the BaseModel subclass an annotation refers to, or None.
333+
334+
`Annotated[Model, ...]` refers to `Model`: the metadata does not change the
335+
fields to prune against.
336+
337+
Args:
338+
annotation: A field type annotation, e.g. `Model`, `Annotated[Model, ...]`,
339+
`Any` or `str`.
340+
341+
Returns:
342+
The BaseModel subclass, or None if the annotation is not a model.
343+
"""
344+
while typing.get_origin(annotation) is typing.Annotated:
345+
annotation = typing.get_args(annotation)[0]
346+
if isinstance(annotation, type) and issubclass(
347+
annotation, pydantic.BaseModel
348+
):
349+
return annotation
350+
return None
351+
352+
329353
def _remove_extra_fields(model: Any, response: dict[str, object]) -> None:
330354
"""Removes extra fields from the response that are not in the model.
331355
@@ -361,18 +385,25 @@ def _remove_extra_fields(model: Any, response: dict[str, object]) -> None:
361385
if typing.get_origin(annotation) is Union:
362386
annotation = typing.get_args(annotation)[0]
363387

364-
# if dict, assume BaseModel but also check that field type is not dict
365-
# example: FunctionCall.args
366-
if isinstance(value, dict) and typing.get_origin(annotation) is not dict:
367-
_remove_extra_fields(annotation, value)
388+
# Only recurse into dicts that map to a BaseModel. Fields typed `Any` (or
389+
# `list[Any]`) carry free-form JSON, e.g. PredictResponse.predictions, and
390+
# have no fields to prune against.
391+
model_type = _as_base_model_type(annotation)
392+
if isinstance(value, dict) and model_type is not None:
393+
_remove_extra_fields(model_type, value)
368394
elif isinstance(value, list):
369395
if _is_struct_type(annotation):
370396
continue
371397

398+
item_type_args = typing.get_args(annotation)
399+
item_type = _as_base_model_type(
400+
item_type_args[0] if item_type_args else None
401+
)
402+
if item_type is None:
403+
continue
372404
for item in value:
373-
# assume a list of dict is list of BaseModel
374405
if isinstance(item, dict):
375-
_remove_extra_fields(typing.get_args(annotation)[0], item)
406+
_remove_extra_fields(item_type, item)
376407

377408

378409
T = typing.TypeVar('T', bound='BaseModel')

‎google/genai/tests/common/test_common.py‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,3 +979,85 @@ def test_from_response_resolves_forward_references():
979979

980980
assert result.items is not None
981981
assert result.items[0].value == "a"
982+
983+
984+
class _PrunedNestedModel(_common.BaseModel):
985+
name: Optional[str] = None
986+
987+
988+
class _FreeFormJsonModel(_common.BaseModel):
989+
# Mirrors PredictResponse: model servers return arbitrary JSON here.
990+
predictions: Optional[List[typing.Any]] = None
991+
metadata: Optional[typing.Any] = None
992+
nested: Optional[_PrunedNestedModel] = None
993+
nested_list: Optional[List[_PrunedNestedModel]] = None
994+
995+
996+
@pytest.mark.parametrize(
997+
"response,field,expected",
998+
[
999+
pytest.param(
1000+
{"predictions": [{"content": "a", "score": 0.9}]},
1001+
"predictions",
1002+
[{"content": "a", "score": 0.9}],
1003+
id="dicts_in_list_of_any",
1004+
),
1005+
pytest.param(
1006+
{"predictions": ["a", {"content": "b"}, [0.1, 0.2]]},
1007+
"predictions",
1008+
["a", {"content": "b"}, [0.1, 0.2]],
1009+
id="mixed_items_in_list_of_any",
1010+
),
1011+
pytest.param(
1012+
{"metadata": {"tokens": 3, "extraField": True}},
1013+
"metadata",
1014+
{"tokens": 3, "extraField": True},
1015+
id="dict_in_any",
1016+
),
1017+
],
1018+
)
1019+
def test_from_response_keeps_free_form_json(response, field, expected):
1020+
"""Dicts in `Any`-typed fields are free-form JSON and are kept verbatim."""
1021+
result = _FreeFormJsonModel._from_response(response=response, kwargs={})
1022+
1023+
assert getattr(result, field) == expected
1024+
1025+
1026+
def test_from_response_still_prunes_nested_models_next_to_any_fields():
1027+
"""Pruning still recurses into BaseModel fields alongside `Any` fields."""
1028+
result = _FreeFormJsonModel._from_response(
1029+
response={
1030+
"predictions": [{"content": "a"}],
1031+
"nested": {"name": "n", "extraField": 1},
1032+
"nestedList": [{"name": "m", "extraField": 2}],
1033+
"unknownTopLevel": 3,
1034+
},
1035+
kwargs={},
1036+
)
1037+
1038+
assert result.predictions == [{"content": "a"}]
1039+
assert result.nested.name == "n"
1040+
assert "extra_field" not in result.nested.model_dump()
1041+
assert result.nested_list[0].name == "m"
1042+
assert "unknown_top_level" not in result.model_dump()
1043+
1044+
1045+
class _AnnotatedNestedModel(_common.BaseModel):
1046+
nested: Optional[typing.Annotated[_PrunedNestedModel, "metadata"]] = None
1047+
nested_list: Optional[
1048+
List[typing.Annotated[_PrunedNestedModel, "metadata"]]
1049+
] = None
1050+
1051+
1052+
def test_from_response_prunes_annotated_nested_models():
1053+
"""`Annotated[Model, ...]` fields are pruned like `Model` fields."""
1054+
result = _AnnotatedNestedModel._from_response(
1055+
response={
1056+
"nested": {"name": "n", "extraField": 1},
1057+
"nestedList": [{"name": "m", "extraField": 2}],
1058+
},
1059+
kwargs={},
1060+
)
1061+
1062+
assert result.nested.name == "n"
1063+
assert result.nested_list[0].name == "m"

0 commit comments

Comments
 (0)