Skip to content

Commit 7c9b3ac

Browse files
committed
Merge branch 'fix/provider-strategy-response-format-support' of https://github.com/RohanDisa/langchain-google into fix/provider-strategy-response-format-support
2 parents 71a7239 + ceff510 commit 7c9b3ac

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

libs/genai/tests/unit_tests/test_chat_models.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from unittest.mock import ANY, AsyncMock, Mock, patch
1111

1212
import google.ai.generativelanguage as glm
13-
import proto # type: ignore[import-untyped]
1413
import pytest
1514
from google.ai.generativelanguage_v1beta.types import (
1615
Candidate,
@@ -2962,17 +2961,16 @@ def _convert_proto_to_dict(obj: Any) -> Any:
29622961
"""Recursively convert proto objects to dicts for comparison."""
29632962
if isinstance(obj, dict):
29642963
return {k: _convert_proto_to_dict(v) for k, v in obj.items()}
2965-
elif isinstance(obj, (list, tuple)):
2964+
if isinstance(obj, (list, tuple)):
29662965
return [_convert_proto_to_dict(item) for item in obj]
2967-
elif hasattr(obj, "__class__") and "proto" in str(type(obj)):
2966+
if hasattr(obj, "__class__") and "proto" in str(type(obj)):
29682967
# Try to convert proto object to dict
29692968
try:
29702969
if hasattr(obj, "__iter__") and not isinstance(obj, str):
29712970
converted = dict(obj)
29722971
# Recursively convert nested proto objects
29732972
return {k: _convert_proto_to_dict(v) for k, v in converted.items()}
2974-
else:
2975-
return obj
2973+
return obj
29762974
except (TypeError, ValueError):
29772975
return obj
29782976
return obj
@@ -3006,9 +3004,12 @@ def test_response_format_provider_strategy() -> None:
30063004
},
30073005
}
30083006

3009-
gen_config = llm._prepare_params(stop=None, response_format=response_format)
3007+
gen_config = llm._prepare_params(
3008+
stop=None, response_format=response_format
3009+
)
30103010

3011-
# response_json_schema may be converted to proto object, so convert to dict for comparison
3011+
# response_json_schema may be converted to proto object, so convert to
3012+
# dict for comparison
30123013
schema = _convert_proto_to_dict(gen_config.response_json_schema)
30133014
assert schema == schema_dict
30143015
assert gen_config.response_mime_type == "application/json"
@@ -3025,13 +3026,14 @@ def test_response_format_provider_strategy() -> None:
30253026
response_format=response_format,
30263027
response_json_schema=different_schema,
30273028
)
3028-
3029-
# response_json_schema may be converted to proto object, so convert to dict for comparison
3029+
3030+
# response_json_schema may be converted to proto object, so convert to
3031+
# dict for comparison
30303032
schema_2 = _convert_proto_to_dict(gen_config_2.response_json_schema)
30313033
assert schema_2 == different_schema
30323034
assert gen_config_2.response_mime_type == "application/json"
30333035

3034-
3036+
30353037
old_schema = {
30363038
"type": "object",
30373039
"properties": {"old_field": {"type": "string"}},
@@ -3044,7 +3046,8 @@ def test_response_format_provider_strategy() -> None:
30443046
response_format=response_format,
30453047
)
30463048

3047-
# response_json_schema may be converted to proto object, so convert to dict for comparison
3049+
# response_json_schema may be converted to proto object, so convert to
3050+
# dict for comparison
30483051
schema_3 = _convert_proto_to_dict(gen_config_3.response_json_schema)
30493052
assert schema_3 == schema_dict
30503053
assert gen_config_3.response_mime_type == "application/json"
@@ -3056,7 +3059,8 @@ def test_response_format_provider_strategy() -> None:
30563059
response_schema=schema_dict,
30573060
)
30583061
# Should fall back to response_schema
3059-
# response_json_schema may be converted to proto object, so convert to dict for comparison
3062+
# response_json_schema may be converted to proto object, so convert to
3063+
# dict for comparison
30603064
schema_4 = _convert_proto_to_dict(gen_config_4.response_json_schema)
30613065
assert schema_4 == schema_dict
30623066
assert gen_config_4.response_mime_type == "application/json"

0 commit comments

Comments
 (0)