sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

[BUG] Gemini: structured_output() raises the raw ClientError on a 429 instead of ModelThrottledException

Open chrikrah opened this issue 3 weeks ago • 1 comments

Checks

  • [x] I have updated to the lastest minor and patch version of Strands
  • [x] I have checked the documentation and this is not expected behavior
  • [x] I have searched ./issues and there are no duplicates of my issue

SDK Language

Python

Strands Version

main at 3d32c22 (after python/v1.64.0)

Language Runtime Version

Python 3.12

Operating System

macOS

Installation Method

git clone

Steps to Reproduce

GeminiModel.structured_output() calls generate_content with no except, so the 429 mapping that stream() got in #4524 never runs there.

import asyncio, unittest.mock as m
import pydantic
from google import genai
from strands.models.gemini import GeminiModel

class Out(pydantic.BaseModel):
    x: int

async def run(code, status, path):
    cls = genai.errors.ClientError if code < 500 else genai.errors.ServerError
    error = cls(code, {"error": {"code": code, "message": "busy", "status": status}})
    model = GeminiModel(model_id="gemini-2.5-flash")
    client = m.MagicMock()
    client.aio.models.generate_content = m.AsyncMock(side_effect=error)
    client.aio.models.generate_content_stream = m.AsyncMock(side_effect=error)
    msgs = [{"role": "user", "content": [{"text": "hi"}]}]
    with m.patch.object(model, "_get_client", return_value=client):
        try:
            gen = model.stream(msgs) if path == "stream" else model.structured_output(Out, msgs)
            async for _ in gen:
                pass
        except Exception as x:
            return type(x).__name__

for code, status in [(429, "RESOURCE_EXHAUSTED"), (503, "UNAVAILABLE")]:
    for path in ["stream", "structured_output"]:
        print(code, path, asyncio.run(run(code, status, path)))
$ python -I repro.py    # google-genai 2.34.0, strands.__file__ from the 3d32c22 export
429 stream ModelThrottledException
429 structured_output ClientError
503 stream ServerError
503 structured_output ServerError
# not run: tests/strands/models/test_gemini.py (the export lacked the moto dev dependency)

Expected Behavior

The Gemini provider page promises a ModelThrottledException when quota limits are exceeded ("Rate Limiting and Safety Issues" in model-providers/google.mdx), and the same page documents agent.structured_output() on Gemini. OpenAIModel.structured_output() maps throttling too (openai.py:890).

Actual Behavior

The raw genai.errors.ClientError reaches the caller, so a ModelThrottledException handler misses a Gemini 429.

Possible Solution

Moving the except block from stream() into one helper that both methods call means #5947's 503 change covers structured_output() too. Besides the deprecated Agent.structured_output_async(), LLMDecisionModel (decisions/llm_decision_model.py:88) and the routing classifier (models/routing/classifier_strategy.py:93) call it, so a throttled Gemini decision or routing call also surfaces as a raw ClientError.

Related Issues

#4524 (429 in stream()), #5916 and #5947 (503 in stream()), #149.

I can open that PR this week, with a test for each method.

chrikrah avatar Nov 09 '26 21:11 chrikrah

I can take this. I will map Gemini structured_output() 429s to ModelThrottledException the same way stream() already does, and add a test.

Ayush701-code-zm avatar Nov 10 '26 03:11 Ayush701-code-zm