From 4f89f2b5ff79933c5fde30e963ba6e5be042fac7 Mon Sep 17 00:00:00 2001 From: vbarda <vadym@langchain.dev> Date: Tue, 14 Jan 2025 16:37:05 -0500 Subject: [PATCH 1/2] langgraph: allow model names as strings in create_react_agent --- .../langgraph/prebuilt/chat_agent_executor.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/libs/langgraph/langgraph/prebuilt/chat_agent_executor.py b/libs/langgraph/langgraph/prebuilt/chat_agent_executor.py index 7172c8e10c..f44be2bb74 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,16 @@ class Agent,Tools otherClass # 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 init_chat_model + except ImportError: + raise ImportError( + "Please install langchain (`pip install langchain`) to use '<provider>:<model>' string syntax for `model` parameter." + ) + + model = init_chat_model(model) + tool_calling_enabled = len(tool_classes) > 0 if _should_bind_tools(model, tool_classes) and tool_calling_enabled: From dcd5218e472d5650a9cf35b1bc451d9bfac3e287 Mon Sep 17 00:00:00 2001 From: vbarda <vadym@langchain.dev> Date: Tue, 14 Jan 2025 17:17:17 -0500 Subject: [PATCH 2/2] lint --- libs/langgraph/langgraph/prebuilt/chat_agent_executor.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/libs/langgraph/langgraph/prebuilt/chat_agent_executor.py b/libs/langgraph/langgraph/prebuilt/chat_agent_executor.py index f44be2bb74..a140077136 100644 --- a/libs/langgraph/langgraph/prebuilt/chat_agent_executor.py +++ b/libs/langgraph/langgraph/prebuilt/chat_agent_executor.py @@ -597,13 +597,15 @@ class Agent,Tools otherClass if isinstance(model, str): try: - from langchain.chat_models import init_chat_model + 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 '<provider>:<model>' string syntax for `model` parameter." ) - model = init_chat_model(model) + model = cast(BaseChatModel, init_chat_model(model)) tool_calling_enabled = len(tool_classes) > 0