From b18d266f2c530e2d56154205edfaf393a9d75b77 Mon Sep 17 00:00:00 2001 From: Vadym Barda Date: Wed, 15 Jan 2025 09:43:37 -0500 Subject: [PATCH] langgraph: allow model names as strings in create_react_agent (#3031) ```python agent = create_react_agent("anthropic:claude-3-5-sonnet-latest", [add]) agent.invoke({"messages": [("user", "what's 3 + 5")]}) ``` --- .../langgraph/prebuilt/chat_agent_executor.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/libs/langgraph/langgraph/prebuilt/chat_agent_executor.py b/libs/langgraph/langgraph/prebuilt/chat_agent_executor.py index 7172c8e10..a14007713 100644 --- a/libs/langgraph/langgraph/prebuilt/chat_agent_executor.py +++ b/libs/langgraph/langgraph/prebuilt/chat_agent_executor.py @@ -223,7 +223,7 @@ def _validate_chat_history( @deprecated_parameter("messages_modifier", "0.1.9", "state_modifier", removal="0.3.0") def create_react_agent( - model: LanguageModelLike, + model: Union[str, LanguageModelLike], tools: Union[ToolExecutor, Sequence[BaseTool], ToolNode], *, state_schema: Optional[StateSchemaType] = None, @@ -595,6 +595,18 @@ def create_react_agent( # get the tool functions wrapped in a tool class from the ToolNode tool_classes = list(tool_node.tools_by_name.values()) + if isinstance(model, str): + try: + from langchain.chat_models import ( # type: ignore[import-not-found] + init_chat_model, + ) + except ImportError: + raise ImportError( + "Please install langchain (`pip install langchain`) to use ':' string syntax for `model` parameter." + ) + + model = cast(BaseChatModel, init_chat_model(model)) + tool_calling_enabled = len(tool_classes) > 0 if _should_bind_tools(model, tool_classes) and tool_calling_enabled: