[BUG] GeminiModel: 429 not retried when error.status is "Too Many Requests" instead of RESOURCE_EXHAUSTED
Checks
- I have updated to the lastest minor and patch version of Strands
- I have checked the documentation and this is not expected behavior
- I have searched ./issues and there are no duplicates of my issue
SDK Language
Python
Strands Version
1.57.0
Language Runtime Version
Python 3.11.12
Operating System
Linux (Debian, Docker)
Installation Method
pip
Steps to Reproduce
- Install:
pip install 'strands-agents[gemini]==1.57.0'(resolves google-genai 2.24.0) - Save and run the script below. It feeds
GeminiModel.streamthe twoClientErrors google-genai builds for a Vertex AI 429: one from a JSON response body, one from a non-JSON body (theContentTypeErrorfallback ingoogle/genai/errors.pyraise_for_async_response).
import asyncio
import json
from unittest.mock import AsyncMock, MagicMock
from google.genai import errors
from strands.models.gemini import GeminiModel
from strands.types.exceptions import ModelThrottledException
BODY = {
"error": {
"code": 429,
"message": "Resource exhausted. Please try again later.",
"status": "RESOURCE_EXHAUSTED",
}
}
# What google-genai builds for a 429 with a JSON body vs. a non-JSON body.
CASES = {
"json body": errors.ClientError(429, BODY),
"non-json body": errors.ClientError(
429, {"message": json.dumps(BODY), "status": "Too Many Requests"}
),
}
async def classify(error: errors.ClientError) -> str:
model = GeminiModel(model_id="gemini-2.5-flash")
client = MagicMock()
client.aio.models.generate_content_stream = AsyncMock(side_effect=error)
model._get_client = lambda: client
try:
async for _ in model.stream([{"role": "user", "content": [{"text": "hi"}]}]):
pass
except ModelThrottledException:
return "ModelThrottledException (retried)"
except errors.ClientError as raised:
return f"ClientError (NOT retried), code={raised.code} status={raised.status!r}"
return "no error"
for name, error in CASES.items():
print(f"{name}: {asyncio.run(classify(error))}")- See output:
json body: ModelThrottledException (retried)
non-json body: ClientError (NOT retried), code=429 status='Too Many Requests'Expected Behavior
Every 429 from Gemini raises ModelThrottledException, so the event loop's retry strategy backs off and retries, whatever the response body format.
Actual Behavior
When Vertex AI returns a 429 with a non-JSON body, google-genai falls back to {'message': <raw text>, 'status': response.reason}, so error.status == "Too Many Requests" (while error.code == 429). GeminiModel.stream only matches error.status against "RESOURCE_EXHAUSTED" | "UNAVAILABLE", so the error falls through to case _: raise error. There is no retry, and the agent invocation fails on the first rate limit.
Additional Context
We see this in production on Vertex AI (gemini-3.1-flash-lite, streaming, aiohttp transport) under concurrent load. The same endpoint returns both body formats: in one trace, the first 429 on a request was JSON and was retried with backoff, and a later 429 on the same request was non-JSON and failed immediately. Across a bulk-load burst, 93 of 95 failed 429s were never retried.
Verbatim error from production:
ClientError('429 Too Many Requests. {\'message\': \'{\n "error": {\n "code": 429,\n "message": "Resource exhausted. Please try again later. ...",\n "status": "RESOURCE_EXHAUSTED"\n }\n}\n\', \'status\': \'Too Many Requests\'}')The real RESOURCE_EXHAUSTED survives only inside error.message as a string.
Related gap: genai.errors.ServerError (e.g. 500 INTERNAL, 503 UNAVAILABLE) is not a ClientError, so GeminiModel never retries it either. Transient Vertex 5xx errors were about 19% of our failures.
Possible Solution
Classify on the HTTP code, which google-genai populates on every path, as well as the status string:
except genai.errors.ClientError as error:
if error.code == 429 or error.status in ("RESOURCE_EXHAUSTED", "UNAVAILABLE"):
raise ModelThrottledException(error.message or str(error)) from error
...Related Issues
Follow-up to #3226 / #3228. #3228 moved classification from json.loads(error.message) to ClientError.status, which fixed plain-text messages whose status still parsed. Its review called out this remaining case (a non-JSON HTTP body makes status the reason phrase "Too Many Requests") and suggested error.code in (429, 503) as a follow-up, but left it out of scope.
Source: strands-agents/harness-sdk