From 8ab206043c25909f24090e7cebdcb3647bf70b2b Mon Sep 17 00:00:00 2001 From: Bagatur <22008038+baskaryan@users.noreply.github.com> Date: Tue, 7 Jan 2025 13:47:31 -0500 Subject: [PATCH] langgraph[patch]: fix create_react_agent inspectability (#2948) Co-authored-by: vbarda --- libs/langgraph/langgraph/prebuilt/chat_agent_executor.py | 8 ++++---- libs/langgraph/tests/test_prebuilt.py | 7 +++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/libs/langgraph/langgraph/prebuilt/chat_agent_executor.py b/libs/langgraph/langgraph/prebuilt/chat_agent_executor.py index da70f9e87..1b4797b80 100644 --- a/libs/langgraph/langgraph/prebuilt/chat_agent_executor.py +++ b/libs/langgraph/langgraph/prebuilt/chat_agent_executor.py @@ -554,6 +554,10 @@ def create_react_agent( ) model_runnable = preprocessor | model + # If any of the tools are configured to return_directly after running, + # our graph needs to check if these were called + should_return_direct = {t.name for t in tool_classes if t.return_direct} + # Define the function that calls the model def call_model(state: AgentState, config: RunnableConfig) -> AgentState: _validate_chat_history(state["messages"]) @@ -673,10 +677,6 @@ def create_react_agent( should_continue, ) - # If any of the tools are configured to return_directly after running, - # our graph needs to check if these were called - should_return_direct = {t.name for t in tool_classes if t.return_direct} - def route_tool_responses(state: AgentState) -> Literal["agent", "__end__"]: for m in reversed(state["messages"]): if not isinstance(m, ToolMessage): diff --git a/libs/langgraph/tests/test_prebuilt.py b/libs/langgraph/tests/test_prebuilt.py index ea44c4f0e..3186cac9e 100644 --- a/libs/langgraph/tests/test_prebuilt.py +++ b/libs/langgraph/tests/test_prebuilt.py @@ -1,4 +1,5 @@ import dataclasses +import inspect import json from functools import partial from typing import ( @@ -2040,3 +2041,9 @@ def test__get_state_args() -> None: return 0.0 assert _get_state_args(foo) == {"a": None, "b": "bar"} + + +def test_inspect_react() -> None: + model = FakeToolCallingModel(tool_calls=[]) + agent = create_react_agent(model, []) + inspect.getclosurevars(agent.nodes["agent"].bound.func)