ModelFallbackMiddleware(
self,
first_model: str | BaseChatModel,
*additional_models: str | BaseChatModelStart the shell session and run startup commands.
| Name | Type | Description |
|---|---|---|
first_model* | str | BaseChatModel | |
*additional_models | str | BaseChatModel | Default: () |
| Name | Type |
|---|---|
| first_model | str | BaseChatModel |
Automatic fallback to alternative models on errors.
Retries failed model calls with alternative models in sequence until
success or all models exhausted. Primary model specified in create_agent.
Example:
from langchain.agents.middleware.model_fallback import ModelFallbackMiddleware
from langchain.agents import create_agent
fallback = ModelFallbackMiddleware(
"openai:gpt-4o-mini", # Try first on error
"anthropic:claude-sonnet-4-5-20250929", # Then this
)
agent = create_agent(
model="openai:gpt-4o", # Primary model
middleware=[fallback],
)
# If primary fails: tries gpt-4o-mini, then claude-sonnet-4-5-20250929
result = await agent.invoke({"messages": [HumanMessage("Hello")]})First fallback model (string name or instance).
Additional fallbacks in order.
Try fallback models in sequence on errors.
Try fallback models in sequence on errors (async version).