retry-fallback

command
v1.11.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 24, 2026 License: Apache-2.0 Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

This sample demonstrates composing the Retry and Fallback middlewares in a single Generate call to build a resilient model pipeline.

The composition order passed to ai.WithUse is outer-to-inner: the first middleware wraps the second, which wraps the actual model call. Here:

ai.WithUse(&middleware.Retry{...}, &middleware.Fallback{...})

expands to Retry { Fallback { model } } at call time:

  • The primary model is invoked first.
  • If it fails with a fallback-eligible status (UNAVAILABLE, NOT_FOUND, DEADLINE_EXCEEDED, INTERNAL, ...), Fallback forwards the request to the next model in its list and keeps trying until one succeeds or the list is exhausted.
  • If the whole Fallback cascade still returns a retryable error, Retry sleeps with exponential backoff and runs the cascade again, up to MaxRetries times.

To make the fallback path visibly fire on a fresh run, the primary model is deliberately set to a non-existent model id — Google AI returns a NOT_FOUND, Fallback catches it, and the real model answers. If you switch the primary to a valid model, the sample still works; Fallback simply never triggers.

To run:

go run .

In another terminal, trigger the flow (the response will be produced by the fallback model since the primary is intentionally invalid):

curl -X POST http://localhost:8080/resilientFlow \
  -H "Content-Type: application/json" \
  -d '{"data": "quantum computing"}'

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL