Class FallbackMiddleware

java.lang.Object
com.google.genkit.ai.middleware.BaseGenerationMiddleware
com.google.genkit.plugins.middleware.FallbackMiddleware
All Implemented Interfaces:
GenerationMiddleware

public class FallbackMiddleware extends BaseGenerationMiddleware
Falls back to alternate models when the primary model fails.

Wraps the wrapGenerate hook: if generation with the primary model (the one on the incoming request) throws, each model in FallbackMiddleware.Options.models is tried in order by re-running the generate iteration with the request's model swapped. The first success is returned; if all fallbacks fail, the original error is rethrown.

Implemented at the generate level (rather than wrapModel) because switching model requires re-resolving the model action, which the core generate loop already does from options.model. As a consequence a failing iteration re-runs its tool calls under the fallback model; put side-effect-free tools before this middleware if that matters.

Mirrors the JS fallback middleware in @genkit-ai/middleware and the Go Fallback middleware.