Skip to content

feat(genai): production-grade model-agnostic FallbackPolicy middleware#1

Closed
giterinhub wants to merge 2 commits into
mainfrom
feature/add-fallback-policy
Closed

feat(genai): production-grade model-agnostic FallbackPolicy middleware#1
giterinhub wants to merge 2 commits into
mainfrom
feature/add-fallback-policy

Conversation

@giterinhub

Copy link
Copy Markdown
Owner

Summary & Context

This PR introduces and enhances the Model-Agnostic Fallback Middleware (FallbackPolicy) for the google-genai Python 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

  1. Universal Exception Code Extraction:

    • _extract_status_code dynamically inspects APIError.code, exc.status_code, and exc.response.status_code, enabling seamless compatibility across google-genai, httpx, requests, and google.api_core.
  2. Telemetry & Audit Callback (on_fallback):

    • Accepts an optional on_fallback(exc, attempt_idx, next_payload) hook allowing downstream applications to record metrics (Prometheus, OpenTelemetry, Datadog) or custom failover audit logs.
  3. Configurable Backoff (backoff_factor):

    • Supports exponential backoff delays (backoff_factor * 2^(attempt-1)) for both synchronous (time.sleep) and asynchronous (asyncio.sleep) execution paths.
  4. Payload Immutability Guarantee:

    • Each attempt uses copy.deepcopy(kwargs) to ensure nested objects (contents, config) remain completely unmutated across retry iterations.
  5. Multi-Dimensional Fallback Matrix:

    • Sequential matrix evaluation when both fallback_models and fallback_locations are specified, evaluated up to max_retries.

Verification & Testing

  • Ruff Linting: python3 -m ruff check google/genai/_fallback.py google/genai/tests/test_fallback.py google/genai/tests/test_fallback_unittest.py -> Passed (0 errors)
  • Ruff Formatting: 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 Suite: pytest google/genai/tests/test_fallback.py -v -> 13 / 13 Passed
  • Unittest Suite: python3 google/genai/tests/test_fallback_unittest.py -> 12 / 12 Passed
  • Concurrency Stress Test: 100 concurrent async tasks executed through execute_async via asyncio.gather().

…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

Copy link
Copy Markdown
Owner Author

Superseded by upstream PR googleapis#2771

@giterinhub giterinhub closed this Jul 23, 2026
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.

1 participant