Skip to content

fix: retry Gemini server errors and fall back to other models (#250) - #270

Open
mecampbellsoup wants to merge 1 commit into
bradautomates:mainfrom
mecampbellsoup:mc/250-gemini-retry-fallback
Open

mecampbellsoup wants to merge 1 commit into
bradautomates:mainfrom
mecampbellsoup:mc/250-gemini-retry-fallback

Conversation

@mecampbellsoup

Copy link
Copy Markdown

Fixes #250.

A 503 "high demand" from the default Gemini model fails the whole run on the first try, even after the upload has finished and while older models are answering normally.

What changes

  • Retry: a server error (5xx) is retried on the same model and the same upload or URL, after 5s and then 15s. Each attempt is logged to stderr. WATCH_GEMINI_RETRIES sets the count (default 2, 0 disables).
  • Fallback: when a model stays overloaded, or returns 429 (out of quota), the models in WATCH_GEMINI_FALLBACK_MODELS are asked in order (default gemini-3.5-flash,gemini-2.5-flash; empty disables).
  • Nothing silent: the report's Engine line names the model that answered, and a new Model fallback line lists every model that failed. When all models fail, the error lists each one with its status.
  • Unchanged: auth, rejected (4xx) and network errors still stop at once, and the engine still never falls back to --engine local.

ask() is untouched. The new ask_with_recovery() wraps it, and failures now raise a GeminiError (a SystemExit subclass that carries its category), so existing callers and tests behave as before.

How it was checked

With a free-tier key on 2026-11-10, a two-token text request got HTTP 503 from gemini-3.7-flash, gemini-3.8-flash and gemini-flash-latest on every try (3/3 each). On the Interactions API's agentic path that the skill uses, gemini-3.5-flash and gemini-2.5-flash answered 2/2 each, while gemini-3.7-flash returned 503 in under half a second. That matches the table in the #250 comment.

The real run of this branch, on a 3:22 CNBC clip, with default settings:

[watch] asking gemini-3.7-flash…
[watch] gemini-3.7-flash: HTTP 503 (attempt 1/3); retrying in 5s…
[watch] gemini-3.7-flash: HTTP 503 (attempt 2/3); retrying in 15s…
[watch] gemini-3.7-flash: HTTP 503; asking gemini-3.5-flash instead…

- **Engine:** gemini-3.5-flash (agentic)
- **Model fallback:** answered by gemini-3.5-flash after gemini-3.7-flash (HTTP 503, 3 attempts) failed

pytest -q: 262 passed. The new tests cover retry with backoff on the same upload, fallback once retries are spent, 429 moving on without a retry, auth/rejected/network errors stopping at once, every model failing, the config settings, and the report line.

🤖 Generated with Claude Code

https://claude.ai/code/session_01JzgQzQPSrKcadVCMTgMyse

…tomates#250)

A 503 "high demand" on the default model failed the whole run, even
though the upload was done and an older model was answering. Retry 5xx
on the same upload with backoff, then ask WATCH_GEMINI_FALLBACK_MODELS
in order; the report names the model that answered and the ones that
failed. Auth, rejected and network errors still stop at once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JzgQzQPSrKcadVCMTgMyse
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.

Gemini engine: repeated HTTP 503 on local videos; no bounded retry after upload

1 participant