Skip to content

Commit 9d00424

Browse files
Ark-kuncopybara-github
authored andcommitted
fix: GenAI - Fixed GAPIC-renamed keys (keys with trailing underscore) in structures returned by the to_dict methods.
When calling `to_dict` on objects that contain schema (e.g. `GenerationConfig`), correct keys are now used (e.g. `type` instead of `type_`). PiperOrigin-RevId: 696384806
1 parent 9a30c31 commit 9d00424

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

tests/unit/vertexai/test_generative_models.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -178,47 +178,47 @@
178178
},
179179
}
180180
_RENAMING_EXPECTED_SCHEMA = {
181-
"type_": "OBJECT",
181+
"type": "OBJECT",
182182
"min_properties": "1", # NB: int64 is converted to string
183183
"max_properties": "3",
184184
"properties": {
185185
"names": {
186-
"type_": "ARRAY",
186+
"type": "ARRAY",
187187
"min_items": "2",
188188
"max_items": "4",
189189
"items": {
190-
"type_": "STRING",
190+
"type": "STRING",
191191
"min_length": "3",
192192
"max_length": "5",
193193
},
194194
},
195195
"date": {
196196
"any_of": [
197197
{
198-
"type_": "STRING",
199-
"format_": "date",
198+
"type": "STRING",
199+
"format": "date",
200200
},
201201
{
202202
"any_of": [
203203
{
204-
"type_": "INTEGER",
204+
"type": "INTEGER",
205205
"minimum": 20241001,
206206
},
207207
],
208208
},
209209
],
210210
},
211211
"ordered": {
212-
"type_": "OBJECT",
212+
"type": "OBJECT",
213213
"properties": {
214-
"a": {"type_": "STRING"},
215-
"b": {"type_": "INTEGER"},
214+
"a": {"type": "STRING"},
215+
"b": {"type": "INTEGER"},
216216
"c": {
217-
"type_": "OBJECT",
217+
"type": "OBJECT",
218218
"properties": {
219-
"x": {"type_": "STRING"},
220-
"y": {"type_": "NUMBER"},
221-
"z": {"type_": "INTEGER"},
219+
"x": {"type": "STRING"},
220+
"y": {"type": "NUMBER"},
221+
"z": {"type": "INTEGER"},
222222
},
223223
"property_ordering": ["z", "y", "x"], # explicit order kept
224224
},

vertexai/generative_models/_generative_models.py

+25-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
"""Classes for working with generative models."""
1616
# pylint: disable=bad-continuation, line-too-long, protected-access
1717

18-
from collections.abc import Mapping
1918
import copy
2019
import io
2120
import json
@@ -30,6 +29,7 @@
3029
Iterable,
3130
List,
3231
Literal,
32+
Mapping,
3333
Optional,
3434
Sequence,
3535
Type,
@@ -2953,11 +2953,19 @@ def _append_gapic_part(
29532953

29542954
def _proto_to_dict(message) -> Dict[str, Any]:
29552955
"""Converts a proto-plus protobuf message to a dictionary."""
2956-
return type(message).to_dict(
2956+
# The best way to convert proto to dict is not trivial.
2957+
# Ideally, we want original keys in snake_case.
2958+
# The preserving_proto_field_name flag controls key names, but states have issues:
2959+
# `False` leads to keys using camelCase instead of snake_case.
2960+
# `True` leads to keys using snake_case, but has renamed names like `type_`.
2961+
# We needs to fix this issue using _fix_renamed_proto_dict_keys_in_place.
2962+
result = type(message).to_dict(
29572963
message,
29582964
including_default_value_fields=False,
29592965
use_integers_for_enums=False,
29602966
)
2967+
_fix_renamed_proto_dict_keys_in_place(result)
2968+
return result
29612969

29622970

29632971
def _dict_to_proto(message_type: Type[T], message_dict: Dict[str, Any]) -> T:
@@ -2974,6 +2982,21 @@ def _dict_to_pretty_string(d: dict) -> str:
29742982
return json.dumps(d, indent=2)
29752983

29762984

2985+
def _fix_renamed_proto_dict_keys_in_place(d: Mapping[str, Any]):
2986+
"""Fixes proto dict keys in place."""
2987+
for key, value in list(d.items()):
2988+
if key.endswith("_"):
2989+
new_key = key.rstrip("_")
2990+
del d[key]
2991+
d[new_key] = value
2992+
if isinstance(value, Mapping):
2993+
_fix_renamed_proto_dict_keys_in_place(value)
2994+
if isinstance(value, Sequence) and not isinstance(value, str):
2995+
for item in value:
2996+
if isinstance(item, Mapping):
2997+
_fix_renamed_proto_dict_keys_in_place(item)
2998+
2999+
29773000
_FORMAT_TO_MIME_TYPE = {
29783001
"png": "image/png",
29793002
"jpeg": "image/jpeg",

0 commit comments

Comments
 (0)