Skip to content

Commit 006042d

Browse files
yinghsienwucopybara-github
authored andcommitted
Copybara import of the project:
-- 2040191 by Danilo Bardusco <danilo@bardusco.com>: fix: handle non-list response_stream in HttpResponse.json property When using the aiohttp backend and Google API returns a 500 error, the response_stream can be an aiohttp.ClientResponse object instead of a list. This causes 'TypeError: ClientResponse object is not subscriptable' when trying to access response_stream[0]. This fix adds a type check before attempting list access, returning None for non-list response_stream values to allow proper error handling to continue. Fixes: #1897 COPYBARA_INTEGRATE_REVIEW=#1903 from taubot-ai:fix-aiohttp-clientresponse-subscript-error f164187 PiperOrigin-RevId: 874784958
1 parent ecfa90c commit 006042d

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

google/genai/_api_client.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,13 @@ async def __anext__(self) -> Any:
267267

268268
@property
269269
def json(self) -> Any:
270-
if not self.response_stream[0]: # Empty response
270+
# Handle case where response_stream is not a list (e.g., aiohttp.ClientResponse)
271+
# This can happen when the API returns an error and the response object
272+
# is passed directly instead of being wrapped in a list.
273+
# See: https://github.com/googleapis/python-genai/issues/1897
274+
if not isinstance(self.response_stream, list):
275+
return None
276+
if not self.response_stream or not self.response_stream[0]: # Empty response
271277
return ''
272278
return self._load_json_from_response(self.response_stream[0])
273279

0 commit comments

Comments
 (0)