Skip to content

Commit 70f65bd

Browse files
jaycee-licopybara-github
authored andcommitted
chore: Remove no-op converters
PiperOrigin-RevId: 810488317
1 parent 0fa183c commit 70f65bd

17 files changed

+1364
-7696
lines changed

google/genai/_common.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import enum
2222
import functools
2323
import logging
24+
import re
2425
import typing
2526
from typing import Any, Callable, FrozenSet, Optional, Union, get_args, get_origin
2627
import uuid
@@ -142,25 +143,38 @@ def get_value_by_path(data: Any, keys: list[str]) -> Any:
142143
return data
143144

144145

145-
def convert_to_dict(obj: object) -> Any:
146+
def maybe_snake_to_camel(snake_str: str, convert: bool = True) -> str:
147+
"""Converts a snake_case string to CamelCase, if convert is True."""
148+
if not convert:
149+
return snake_str
150+
return re.sub(r'_([a-zA-Z])', lambda match: match.group(1).upper(), snake_str)
151+
152+
153+
def convert_to_dict(obj: object, convert_keys: bool = False) -> Any:
146154
"""Recursively converts a given object to a dictionary.
147155
148156
If the object is a Pydantic model, it uses the model's `model_dump()` method.
149157
150158
Args:
151159
obj: The object to convert.
160+
convert_keys: Whether to convert the keys from snake case to camel case.
152161
153162
Returns:
154163
A dictionary representation of the object, a list of objects if a list is
155164
passed, or the object itself if it is not a dictionary, list, or Pydantic
156165
model.
157166
"""
158167
if isinstance(obj, pydantic.BaseModel):
159-
return obj.model_dump(exclude_none=True)
168+
return convert_to_dict(obj.model_dump(exclude_none=True), convert_keys)
160169
elif isinstance(obj, dict):
161-
return {key: convert_to_dict(value) for key, value in obj.items()}
170+
return {
171+
maybe_snake_to_camel(key, convert_keys): convert_to_dict(
172+
value, convert_keys
173+
)
174+
for key, value in obj.items()
175+
}
162176
elif isinstance(obj, list):
163-
return [convert_to_dict(item) for item in obj]
177+
return [convert_to_dict(item, convert_keys) for item in obj]
164178
else:
165179
return obj
166180

@@ -642,3 +656,4 @@ def recursive_dict_update(
642656
target_dict[key] = value
643657
else:
644658
target_dict[key] = value
659+

0 commit comments

Comments
 (0)