Class FallbackMiddleware
- All Implemented Interfaces:
GenerationMiddleware
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.
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionname()Returns the middleware's unique identifier.Returns a fresh instance for eachgenerate()call, enabling per-invocation state.wrapGenerate(ActionContext ctx, GenerateParams params, GenerateNext next) Wraps each iteration of the generate tool loop.Methods inherited from class com.google.genkit.ai.middleware.BaseGenerationMiddleware
tools, wrapModel, wrapTool
-
Constructor Details
-
FallbackMiddleware
-
-
Method Details
-
name
Description copied from interface:GenerationMiddlewareReturns the middleware's unique identifier. -
newInstance
Description copied from interface:GenerationMiddlewareReturns a fresh instance for eachgenerate()call, enabling per-invocation state.Stable state (e.g., API keys, configuration) should be preserved. Per-request state (e.g., counters) should be reset.
-
wrapGenerate
public ModelResponse wrapGenerate(ActionContext ctx, GenerateParams params, GenerateNext next) throws GenkitException Description copied from interface:GenerationMiddlewareWraps each iteration of the generate tool loop.- Specified by:
wrapGeneratein interfaceGenerationMiddleware- Overrides:
wrapGeneratein classBaseGenerationMiddleware- Parameters:
ctx- the action contextparams- the generate parameters including the current request and iterationnext- the next function in the chain- Returns:
- the model response
- Throws:
GenkitException- if processing fails
-