From 20401916b6be6c54b5900611db5ad58a78f886b3 Mon Sep 17 00:00:00 2001 From: Danilo Bardusco Date: Tue, 30 Dec 2025 17:02:24 -0300 Subject: [PATCH] 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: https://github.com/googleapis/python-genai/issues/1897 --- google/genai/_api_client.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/google/genai/_api_client.py b/google/genai/_api_client.py index 7710c4487..9a43621d7 100644 --- a/google/genai/_api_client.py +++ b/google/genai/_api_client.py @@ -260,7 +260,13 @@ async def __anext__(self) -> Any: @property def json(self) -> Any: - if not self.response_stream[0]: # Empty response + # Handle case where response_stream is not a list (e.g., aiohttp.ClientResponse) + # This can happen when the API returns an error and the response object + # is passed directly instead of being wrapped in a list. + # See: https://github.com/googleapis/python-genai/issues/1897 + if not isinstance(self.response_stream, list): + return None + if not self.response_stream or not self.response_stream[0]: # Empty response return '' return self._load_json_from_response(self.response_stream[0])