Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions google/genai/_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,15 @@ def __init__(
self._retry = tenacity.Retrying(**retry_kwargs)
self._async_retry = tenacity.AsyncRetrying(**retry_kwargs)

def convert_uri_to_string(self, obj: Union[dict[str, object], bytes, object]) -> Any:
if isinstance(obj, dict):
return {k: self.convert_uri_to_string(v) for k, v in obj.items()}
elif isinstance(obj, list):
return [self.convert_uri_to_string(i) for i in obj]
elif hasattr(obj, "__str__") and type(obj).__name__ == "AnyUrl":
return str(obj)
return obj

async def _get_aiohttp_session(self) -> 'aiohttp.ClientSession':
"""Returns the aiohttp client session."""
if self._aiohttp_session is None or self._aiohttp_session.closed:
Expand Down Expand Up @@ -1095,11 +1104,11 @@ def _request_once(
http_request.headers['x-goog-user-project'] = (
self._credentials.quota_project_id
)
data = json.dumps(http_request.data) if http_request.data else None
data = json.dumps(self.convert_uri_to_string(http_request.data)) if http_request.data else None
else:
if http_request.data:
if not isinstance(http_request.data, bytes):
data = json.dumps(http_request.data) if http_request.data else None
data = json.dumps(self.convert_uri_to_string(http_request.data)) if http_request.data else None
else:
data = http_request.data

Expand Down Expand Up @@ -1163,11 +1172,11 @@ async def _async_request_once(
http_request.headers['x-goog-user-project'] = (
self._credentials.quota_project_id
)
data = json.dumps(http_request.data) if http_request.data else None
data = json.dumps(self.convert_uri_to_string(http_request.data)) if http_request.data else None
else:
if http_request.data:
if not isinstance(http_request.data, bytes):
data = json.dumps(http_request.data) if http_request.data else None
data = json.dumps(self.convert_uri_to_string(http_request.data)) if http_request.data else None
else:
data = http_request.data

Expand Down