-
Notifications
You must be signed in to change notification settings - Fork 19.4k
feat(langchain): prevent dangling tool calls in HITL middleware #33489
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nhuang-lc
wants to merge
1
commit into
master
Choose a base branch
from
nh/dangling-tool-calls
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
libs/langchain_v1/tests/unit_tests/agents/middleware/test_hitl.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| from langchain.agents.middleware.human_in_the_loop import HumanInTheLoopMiddleware | ||
| from langchain_core.messages import ( | ||
| SystemMessage, | ||
| HumanMessage, | ||
| AIMessage, | ||
| ToolMessage, | ||
| ToolCall, | ||
| RemoveMessage, | ||
| ) | ||
| from langgraph.graph.message import add_messages | ||
|
|
||
|
|
||
| class TestHumanInTheLoopMiddlewareBeforeModel: | ||
| """Test HumanInTheLoopMiddleware before_model behavior.""" | ||
|
|
||
| def test_first_message(self) -> None: | ||
| input_messages = [ | ||
| SystemMessage(content="You are a helpful assistant.", id="1"), | ||
| HumanMessage(content="Hello, how are you?", id="2"), | ||
| ] | ||
| middleware = HumanInTheLoopMiddleware(interrupt_on={}) | ||
| state_update = middleware.before_agent({"messages": input_messages}, None) | ||
| assert state_update is not None | ||
| assert len(state_update["messages"]) == 3 | ||
| assert state_update["messages"][0].type == "remove" | ||
| assert state_update["messages"][1].type == "system" | ||
| assert state_update["messages"][1].content == "You are a helpful assistant." | ||
| assert state_update["messages"][2].type == "human" | ||
| assert state_update["messages"][2].content == "Hello, how are you?" | ||
| assert state_update["messages"][2].id == "2" | ||
|
|
||
| def test_missing_tool_call(self) -> None: | ||
| input_messages = [ | ||
| SystemMessage(content="You are a helpful assistant.", id="1"), | ||
| HumanMessage(content="Hello, how are you?", id="2"), | ||
| AIMessage( | ||
| content="I'm doing well, thank you!", | ||
| tool_calls=[ | ||
| ToolCall(id="123", name="get_events_for_days", args={"date_str": "2025-01-01"}) | ||
| ], | ||
| id="3", | ||
| ), | ||
| HumanMessage(content="What is the weather in Tokyo?", id="4"), | ||
| ] | ||
| middleware = HumanInTheLoopMiddleware(interrupt_on={}) | ||
| state_update = middleware.before_agent({"messages": input_messages}, None) | ||
| assert state_update is not None | ||
| assert len(state_update["messages"]) == 6 | ||
| assert state_update["messages"][0].type == "remove" | ||
| assert state_update["messages"][1] == input_messages[0] | ||
| assert state_update["messages"][2] == input_messages[1] | ||
| assert state_update["messages"][3] == input_messages[2] | ||
| assert state_update["messages"][4].type == "tool" | ||
| assert state_update["messages"][4].tool_call_id == "123" | ||
| assert state_update["messages"][4].name == "get_events_for_days" | ||
| assert state_update["messages"][5] == input_messages[3] | ||
| updated_messages = add_messages(input_messages, state_update["messages"]) | ||
| assert len(updated_messages) == 5 | ||
| assert updated_messages[0] == input_messages[0] | ||
| assert updated_messages[1] == input_messages[1] | ||
| assert updated_messages[2] == input_messages[2] | ||
| assert updated_messages[3].type == "tool" | ||
| assert updated_messages[3].tool_call_id == "123" | ||
| assert updated_messages[3].name == "get_events_for_days" | ||
| assert updated_messages[4] == input_messages[3] | ||
|
|
||
| def test_no_missing_tool_calls(self) -> None: | ||
| input_messages = [ | ||
| SystemMessage(content="You are a helpful assistant.", id="1"), | ||
| HumanMessage(content="Hello, how are you?", id="2"), | ||
| AIMessage( | ||
| content="I'm doing well, thank you!", | ||
| tool_calls=[ | ||
| ToolCall(id="123", name="get_events_for_days", args={"date_str": "2025-01-01"}) | ||
| ], | ||
| id="3", | ||
| ), | ||
| ToolMessage(content="I have no events for that date.", tool_call_id="123", id="4"), | ||
| HumanMessage(content="What is the weather in Tokyo?", id="5"), | ||
| ] | ||
| middleware = HumanInTheLoopMiddleware(interrupt_on={}) | ||
| state_update = middleware.before_agent({"messages": input_messages}, None) | ||
| assert state_update is not None | ||
| assert len(state_update["messages"]) == 6 | ||
| assert state_update["messages"][0].type == "remove" | ||
| assert state_update["messages"][1:] == input_messages | ||
| updated_messages = add_messages(input_messages, state_update["messages"]) | ||
| assert len(updated_messages) == 5 | ||
| assert updated_messages == input_messages | ||
|
|
||
| def test_two_missing_tool_calls(self) -> None: | ||
| input_messages = [ | ||
| SystemMessage(content="You are a helpful assistant.", id="1"), | ||
| HumanMessage(content="Hello, how are you?", id="2"), | ||
| AIMessage( | ||
| content="I'm doing well, thank you!", | ||
| tool_calls=[ | ||
| ToolCall(id="123", name="get_events_for_days", args={"date_str": "2025-01-01"}) | ||
| ], | ||
| id="3", | ||
| ), | ||
| HumanMessage(content="What is the weather in Tokyo?", id="4"), | ||
| AIMessage( | ||
| content="I'm doing well, thank you!", | ||
| tool_calls=[ | ||
| ToolCall(id="456", name="get_events_for_days", args={"date_str": "2025-01-01"}) | ||
| ], | ||
| id="5", | ||
| ), | ||
| HumanMessage(content="What is the weather in Tokyo?", id="6"), | ||
| ] | ||
| middleware = HumanInTheLoopMiddleware(interrupt_on={}) | ||
| state_update = middleware.before_agent({"messages": input_messages}, None) | ||
| assert state_update is not None | ||
| assert len(state_update["messages"]) == 9 | ||
| assert state_update["messages"][0].type == "remove" | ||
| assert state_update["messages"][1] == input_messages[0] | ||
| assert state_update["messages"][2] == input_messages[1] | ||
| assert state_update["messages"][3] == input_messages[2] | ||
| assert state_update["messages"][4].type == "tool" | ||
| assert state_update["messages"][4].tool_call_id == "123" | ||
| assert state_update["messages"][4].name == "get_events_for_days" | ||
| assert state_update["messages"][5] == input_messages[3] | ||
| assert state_update["messages"][6] == input_messages[4] | ||
| assert state_update["messages"][7].type == "tool" | ||
| assert state_update["messages"][7].tool_call_id == "456" | ||
| assert state_update["messages"][7].name == "get_events_for_days" | ||
| assert state_update["messages"][8] == input_messages[5] | ||
| updated_messages = add_messages(input_messages, state_update["messages"]) | ||
| assert len(updated_messages) == 8 | ||
| assert updated_messages[0] == input_messages[0] | ||
| assert updated_messages[1] == input_messages[1] | ||
| assert updated_messages[2] == input_messages[2] | ||
| assert updated_messages[3].type == "tool" | ||
| assert updated_messages[3].tool_call_id == "123" | ||
| assert updated_messages[3].name == "get_events_for_days" | ||
| assert updated_messages[4] == input_messages[3] | ||
| assert updated_messages[5] == input_messages[4] | ||
| assert updated_messages[6].type == "tool" | ||
| assert updated_messages[6].tool_call_id == "456" | ||
| assert updated_messages[6].name == "get_events_for_days" | ||
| assert updated_messages[7] == input_messages[5] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we want to only check the last
AIMessage? I have a helper for finding the last AI message + any tool messages after the fact