feat(genai): production-grade model-agnostic FallbackPolicy middleware#1
Closed
giterinhub wants to merge 2 commits into
Closed
feat(genai): production-grade model-agnostic FallbackPolicy middleware#1giterinhub wants to merge 2 commits into
giterinhub wants to merge 2 commits into
Conversation
…lemetry hook, and status code extraction - 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
Owner
Author
|
Superseded by upstream PR googleapis#2771 |
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().