From 33e409e12b3153445247d99f5910cc6bfd387e8b Mon Sep 17 00:00:00 2001 From: Vadym Barda Date: Wed, 30 Apr 2025 13:56:58 -0400 Subject: [PATCH] docs(agents): add model fallbacks (#4478) --- docs/docs/agents/models.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/docs/docs/agents/models.md b/docs/docs/agents/models.md index 43c7fb45c..7c6e773c1 100644 --- a/docs/docs/agents/models.md +++ b/docs/docs/agents/models.md @@ -74,6 +74,41 @@ agent = create_react_agent( The example above uses `ChatAnthropic`, which is already supported by `init_chat_model`. This pattern is shown to illustrate how to manually instantiate a model not available through init_chat_model. +## Adding model fallbacks + +You can add a fallback to a different model or a different LLM provider using `model.with_fallbacks([...])`: + +=== "`init_chat_model`" + + ```python + from langchain.chat_models import init_chat_model + + model_with_fallbacks = ( + init_chat_model("anthropic:claude-3-5-haiku-latest") + # highlight-next-line + .with_fallbacks([ + init_chat_model("openai:gpt-4.1-mini"), + ]) + ) + ``` + +=== "`ChatModel`" + + ```python + from langchain_anthropic import ChatAnthropic + from langchain_openai import ChatOpenAI + + model_with_fallbacks = ( + ChatAnthropic(model="claude-3-5-haiku-latest") + # highlight-next-line + .with_fallbacks([ + ChatOpenAI(model="gpt-4.1-mini"), + ]) + ) + ``` + +See this [guide](https://python.langchain.com/docs/how_to/fallbacks/#fallback-to-better-model) for more information on model fallbacks. + ## Additional resources - [Model integration directory](https://python.langchain.com/docs/integrations/chat/)