From 837fe59e243aa51a7ac361c3cb4cc53d8e542c68 Mon Sep 17 00:00:00 2001 From: Sydney Runkle <54324534+sydney-runkle@users.noreply.github.com> Date: Fri, 23 May 2025 13:58:53 -0400 Subject: [PATCH] prebuilt: support provider builtin tools in `create_react_agent` (#4800) --- docs/docs/agents/tools.md | 16 +++++++++++++++- .../langgraph/prebuilt/chat_agent_executor.py | 15 ++++++++++----- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/docs/docs/agents/tools.md b/docs/docs/agents/tools.md index 8ca986e05..353a9b71e 100644 --- a/docs/docs/agents/tools.md +++ b/docs/docs/agents/tools.md @@ -280,7 +280,21 @@ LangGraph allows access to short-term and long-term memory from tools. See [Memo ## Prebuilt tools -LangChain supports a wide range of prebuilt tool integrations for interacting with APIs, databases, file systems, web data, and more. These tools extend the functionality of agents and enable rapid development. +You can use prebuilt tools from model providers by passing a dictionary with tool specs to the `tools` parameter of `create_react_agent`. For example, to use the `web_search_preview` tool from OpenAI: + +```python +from langgraph.prebuilt import create_react_agent + +agent = create_react_agent( + model="openai:gpt-4o-mini", + tools=[{"type": "web_search_preview"}] +) +response = agent.invoke( + {"messages": ["What was a positive news story from today?"]} +) +``` + +Additionally, LangChain supports a wide range of prebuilt tool integrations for interacting with APIs, databases, file systems, web data, and more. These tools extend the functionality of agents and enable rapid development. You can browse the full list of available integrations in the [LangChain integrations directory](https://python.langchain.com/docs/integrations/tools/). diff --git a/libs/prebuilt/langgraph/prebuilt/chat_agent_executor.py b/libs/prebuilt/langgraph/prebuilt/chat_agent_executor.py index 87dd71e25..668280783 100644 --- a/libs/prebuilt/langgraph/prebuilt/chat_agent_executor.py +++ b/libs/prebuilt/langgraph/prebuilt/chat_agent_executor.py @@ -240,7 +240,7 @@ def _validate_chat_history( def create_react_agent( model: Union[str, LanguageModelLike], - tools: Union[Sequence[Union[BaseTool, Callable]], ToolNode], + tools: Union[Sequence[Union[BaseTool, Callable, dict[str, Any]]], ToolNode], *, prompt: Optional[Prompt] = None, response_format: Optional[ @@ -420,12 +420,13 @@ def create_react_agent( else AgentState ) + llm_builtin_tools: list[dict] = [] if isinstance(tools, ToolNode): tool_classes = list(tools.tools_by_name.values()) tool_node = tools else: - tool_node = ToolNode(tools) - # get the tool functions wrapped in a tool class from the ToolNode + llm_builtin_tools = [t for t in tools if isinstance(t, dict)] + tool_node = ToolNode([t for t in tools if not isinstance(t, dict)]) tool_classes = list(tool_node.tools_by_name.values()) if isinstance(model, str): @@ -442,8 +443,12 @@ def create_react_agent( tool_calling_enabled = len(tool_classes) > 0 - if _should_bind_tools(model, tool_classes) and tool_calling_enabled: - model = cast(BaseChatModel, model).bind_tools(tool_classes) + if ( + _should_bind_tools(model, tool_classes) + and len(tool_classes) > 0 + or (len(llm_builtin_tools) > 0) + ): + model = cast(BaseChatModel, model).bind_tools(tool_classes + llm_builtin_tools) # type: ignore[operator] model_runnable = _get_prompt_runnable(prompt) | model