Skip to content

Commit

Permalink
langgraph: allow model names as strings in create_react_agent
Browse files Browse the repository at this point in the history
  • Loading branch information
vbarda committed Jan 14, 2025
1 parent 54bdba2 commit 4f89f2b
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion libs/langgraph/langgraph/prebuilt/chat_agent_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 4f89f2b

Please sign in to comment.