From 4b7ec256e9929ba856a01db47f4961fc265fa504 Mon Sep 17 00:00:00 2001 From: vbarda Date: Mon, 28 Apr 2025 11:07:09 -0400 Subject: [PATCH] prebuilt: switch to executing parallel tool calls via Send by default --- libs/prebuilt/langgraph/prebuilt/chat_agent_executor.py | 9 ++++++++- libs/prebuilt/tests/test_react_agent.py | 8 ++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/libs/prebuilt/langgraph/prebuilt/chat_agent_executor.py b/libs/prebuilt/langgraph/prebuilt/chat_agent_executor.py index 667e9961a..6b3d72480 100644 --- a/libs/prebuilt/langgraph/prebuilt/chat_agent_executor.py +++ b/libs/prebuilt/langgraph/prebuilt/chat_agent_executor.py @@ -277,7 +277,7 @@ def create_react_agent( interrupt_before: Optional[list[str]] = None, interrupt_after: Optional[list[str]] = None, debug: bool = False, - version: Literal["v1", "v2"] = "v1", + version: Literal["v1", "v2"] = "v2", name: Optional[str] = None, ) -> CompiledGraph: """Creates a graph that works with a chat model that utilizes tool calling. @@ -930,6 +930,13 @@ def create_react_agent( break if m.name in should_return_direct: return END + + # handle a case of parallel tool calls where + # the tool w/ `return_direct` was executed in a different `Send` + if isinstance(m, AIMessage) and m.tool_calls: + if any(call["name"] in should_return_direct for call in m.tool_calls): + return END + return entrypoint if should_return_direct: diff --git a/libs/prebuilt/tests/test_react_agent.py b/libs/prebuilt/tests/test_react_agent.py index eea4444e9..d2d6960d3 100644 --- a/libs/prebuilt/tests/test_react_agent.py +++ b/libs/prebuilt/tests/test_react_agent.py @@ -1078,7 +1078,9 @@ async def test_return_direct(version: str) -> None: ), ] model = FakeToolCallingModel(tool_calls=[second_tool_call, []]) - agent = create_react_agent(model, [tool_return_direct, tool_normal]) + agent = create_react_agent( + model, [tool_return_direct, tool_normal], version=version + ) result = agent.invoke( {"messages": [HumanMessage(content="Test normal", id="hum1")]} ) @@ -1107,7 +1109,9 @@ async def test_return_direct(version: str) -> None: ), ] model = FakeToolCallingModel(tool_calls=[both_tool_calls, []]) - agent = create_react_agent(model, [tool_return_direct, tool_normal]) + agent = create_react_agent( + model, [tool_return_direct, tool_normal], version=version + ) result = agent.invoke({"messages": [HumanMessage(content="Test both", id="hum2")]}) assert result["messages"] == [ HumanMessage(content="Test both", id="hum2"),