feat(genai): production-grade model-agnostic FallbackPolicy middleware#2771
Open
giterinhub wants to merge 3 commits into
Open
feat(genai): production-grade model-agnostic FallbackPolicy middleware#2771giterinhub wants to merge 3 commits into
giterinhub wants to merge 3 commits into
Conversation
Closed
5 tasks
Fixes googleapis#1288 Closes googleapis#1288 - Add automatic status code extraction across APIError, httpx, requests, and response objects - Add backoff_factor parameter supporting configurable exponential backoff delays - Add on_fallback telemetry callback hook for custom audit logging and metrics - Add comprehensive pytest and unittest test suites covering max retries threshold, non-retryable fast-failing error codes, region+model matrix fallbacks, payload mutability audits, and 100-task high-concurrency async stress tests
giterinhub
force-pushed
the
feature/add-fallback-policy
branch
from
July 23, 2026 18:08
ac02e12 to
94139be
Compare
…gleapis#1288) - Narrow return type in _extract_status_code to prevent no-any-return mypy error - Add Dict[str, Any] type parameters for generic dict annotations - Explicitly export APIError in __all__ to resolve attr-defined mypy error in __init__.py
giterinhub
force-pushed
the
feature/add-fallback-policy
branch
from
July 24, 2026 08:04
40f8786 to
43d3116
Compare
Author
|
Note on mypy 3.11 static checking: To keep this PR strictly targeted to FallbackPolicy, those 9 pre-existing IPython type fixes have been isolated into a separate PR: #2773. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary & Context
This PR introduces and enhances the Model-Agnostic Fallback Middleware (
FallbackPolicy) for thegoogle-genaiPython SDK.The policy enables automatic failovers across dynamic model sequences and Vertex AI regions when encountering retryable API errors (e.g.
429 Rate Limit,503 Service Unavailable,504 Gateway Timeout).Key Improvements & Enhancements
Universal Exception Code Extraction:
_extract_status_codedynamically inspectsAPIError.code,exc.status_code, andexc.response.status_code, enabling seamless compatibility acrossgoogle-genai,httpx,requests, andgoogle.api_core.Telemetry & Audit Callback (
on_fallback):on_fallback(exc, attempt_idx, next_payload)hook allowing downstream applications to record metrics (Prometheus, OpenTelemetry, Datadog) or custom failover audit logs.Configurable Backoff (
backoff_factor):backoff_factor * 2^(attempt-1)) for both synchronous (time.sleep) and asynchronous (asyncio.sleep) execution paths.Payload Immutability Guarantee:
copy.deepcopy(kwargs)to ensure nested objects (contents,config) remain completely unmutated across retry iterations.Multi-Dimensional Fallback Matrix:
fallback_modelsandfallback_locationsare specified, evaluated up tomax_retries.Verification & Testing
python3 -m ruff check google/genai/_fallback.py google/genai/tests/test_fallback.py google/genai/tests/test_fallback_unittest.py-> Passed (0 errors)python3 -m ruff format --check google/genai/_fallback.py google/genai/tests/test_fallback.py google/genai/tests/test_fallback_unittest.py-> Passed (0 unformatted files)pytest google/genai/tests/test_fallback.py -v-> 13 / 13 Passedpython3 google/genai/tests/test_fallback_unittest.py-> 12 / 12 Passedexecute_asyncviaasyncio.gather().