fix(gemini): include 503 UNAVAILABLE in fallback retry condition

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Vadym Samoilenko 2026-05-08 12:38:26 +01:00
parent e2391e2603
commit 796cd85a1d

View file

@ -54,7 +54,7 @@ class GeminiService:
self.prompts_dir = Path(__file__).parent.parent / "prompts"
async def _generate(self, contents: Any, config: Any = None) -> tuple[Any, str]:
"""Call generate_content, falling back on 429/quota errors. Returns (response, model_used)."""
"""Call generate_content, falling back on 429/503 transient errors. Returns (response, model_used)."""
for model in [self.model_name, *self._fallback_models]:
try:
kw: dict[str, Any] = {"model": model, "contents": contents}
@ -62,12 +62,12 @@ class GeminiService:
kw["config"] = config
response = await asyncio.to_thread(client.models.generate_content, **kw)
if model != self.model_name:
logger.warning(f"Used fallback model {model!r} (primary quota exceeded)")
logger.warning(f"Used fallback model {model!r} (primary unavailable)")
return response, model
except Exception as exc:
msg = str(exc)
if "429" in msg or "RESOURCE_EXHAUSTED" in msg:
logger.warning(f"Model {model!r} quota exceeded, trying next fallback")
if "429" in msg or "RESOURCE_EXHAUSTED" in msg or "503" in msg or "UNAVAILABLE" in msg:
logger.warning(f"Model {model!r} unavailable, trying next fallback")
last_exc: Exception = exc
continue
raise