Skip to content

TypeError: 'ClientResponse' object is not subscriptable when Google API returns 500 error with aiohttp backend #1897

@bardusco

Description

@bardusco

Bug Description

When using google-genai with the aiohttp backend and Google's API returns a 500 Internal Server Error, the SDK raises a TypeError: 'ClientResponse' object is not subscriptable instead of properly handling the error.

Environment

  • google-genai version: 1.56.0
  • langchain-google-genai version: 4.1.1
  • Python version: 3.11
  • aiohttp version: 3.12.15

Steps to Reproduce

  1. Install google-genai with aiohttp available in the environment
  2. Use ChatGoogleGenerativeAI from langchain-google-genai (or direct genai SDK)
  3. Make requests until Google API returns a transient 500 error

Expected Behavior

The SDK should raise google.genai.errors.ServerError with the 500 error details, allowing proper error handling and retry logic.

Actual Behavior

The SDK raises:

TypeError: 'ClientResponse' object is not subscriptable

Root Cause

The bug is in google/genai/_api_client.py around line 263 in the json property:

@property
def json(self) -> Any:
    if not self.response_stream[0]:  # Empty response
        return ''
    return self._load_json_from_response(self.response_stream[0])

When using the aiohttp backend, response_stream can be an aiohttp.ClientResponse object (not a list), which doesn't support subscript access with [0].

Full Stack Trace

google.genai.errors.ServerError: 500 INTERNAL. {'error': {'code': 500, 'message': 'Internal error encountered.', 'status': 'INTERNAL'}}

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/langchain_core/language_models/chat_models.py", line 664, in astream
    async for chunk in self._astream(
  File "/usr/local/lib/python3.11/site-packages/langchain_google_genai/chat_models.py", line 3202, in _astream
    async for chunk in stream:
  File "/usr/local/lib/python3.11/site-packages/google/genai/models.py", line 7175, in base_async_generator
    async for chunk in response:
  File "/usr/local/lib/python3.11/site-packages/google/genai/models.py", line 5918, in async_generator
    async for response in response_stream:
  File "/usr/local/lib/python3.11/site-packages/google/genai/_api_client.py", line 1459, in async_generator
    await errors.APIError.raise_error_async(
  File "/usr/local/lib/python3.11/site-packages/google/genai/errors.py", line 227, in raise_error_async
    raise ServerError(status_code, response_json, response)
google.genai.errors.ServerError: 500 INTERNAL. {'error': {'code': 500, 'message': 'Internal error encountered.', 'status': 'INTERNAL'}}

During handling of the above exception, another exception occurred:

  File "/usr/local/lib/python3.11/site-packages/langchain_core/language_models/chat_models.py", line 709, in astream
    generations_with_error_metadata = _generate_response_from_error(e)
  File "/usr/local/lib/python3.11/site-packages/langchain_core/language_models/chat_models.py", line 88, in _generate_response_from_error
    if hasattr(response, "json"):
  File "/usr/local/lib/python3.11/site-packages/google/genai/_api_client.py", line 263, in json
    if not self.response_stream[0]:  # Empty response
TypeError: 'ClientResponse' object is not subscriptable

Suggested Fix

Add a type check before accessing response_stream[0]:

@property
def json(self) -> Any:
    # Handle aiohttp.ClientResponse which doesn't support subscript access
    if hasattr(self.response_stream, 'content'):
        # This is aiohttp.ClientResponse, not a list
        return None  # or handle appropriately
    if not self.response_stream or not self.response_stream[0]:
        return ''
    return self._load_json_from_response(self.response_stream[0])

Impact

This bug prevents proper error handling when Google API has transient failures, causing:

  • Unexpected crashes instead of retryable errors
  • Difficulty implementing retry logic
  • Confusing error messages that hide the real issue (500 from Google)

Metadata

Metadata

Assignees

Labels

type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions