Skip to content

Conversation

@bardusco
Copy link

Description

When using the aiohttp backend and Google API returns a 500 Internal Server Error, the HttpResponse.json property raises TypeError: 'ClientResponse' object is not subscriptable instead of allowing proper error handling.

This happens because response_stream can be an aiohttp.ClientResponse object (not a list) when an error occurs, and the code attempts to access response_stream[0] without checking the type first.

Root Cause

In google/genai/_api_client.py, the json property assumes response_stream is always a list:

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

The Fix

Add a type check before attempting list access:

@property
def json(self) -> Any:
    if not isinstance(self.response_stream, list):
        return None
    if not self.response_stream or not self.response_stream[0]:
        return ''
    return self._load_json_from_response(self.response_stream[0])

Impact

Without this fix:

  • The original ServerError: 500 INTERNAL is masked by TypeError
  • Retry logic cannot identify the error as transient
  • Error handling and recovery is broken

With this fix:

  • The ServerError is raised correctly
  • Retry logic can identify transient errors and retry
  • Proper error messages are logged

Testing

This fix has been validated in production for 24+ hours:

Before the fix (Dec 30, 2025 until 18:40 UTC-3):

  • Multiple TypeError: 'ClientResponse' object is not subscriptable errors
  • Messages lost due to masked errors
  • Retry logic failing

After the fix (Dec 30, 2025 19:00 UTC-3 - Dec 31, 2025 16:00 UTC-3):

  • Zero TypeError: 'ClientResponse' object is not subscriptable errors
  • 50+ transient errors correctly identified and retried
  • Zero message handler failures
  • All retries successful

Related Issue

Fixes #1897

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: googleapis#1897
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant