The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
fix(langchain): frame custom HITL rejection reasons (#39773)
Sydney Runkle0eb47127d254
diff --git a/libs/langchain_v1/langchain/agents/middleware/human_in_the_loop.py b/libs/langchain_v1/langchain/agents/middleware/human_in_the_loop.pyindex 5edf99e1c5..c5973be035 100644--- a/libs/langchain_v1/langchain/agents/middleware/human_in_the_loop.py+++ b/libs/langchain_v1/langchain/agents/middleware/human_in_the_loop.py@@ -101,10 +101,11 @@ class RejectDecision(TypedDict):"""The type of response when a human rejects the action."""message: NotRequired[str]- """The message sent to the model explaining why the action was rejected.+ """The human-provided reason for rejecting the action.- If omitted, the model is told that the tool was not executed and should not- retry the same tool call unless the user asks for it.+ The reason is framed as a user rejection when sent to the model. If omitted,+ the model is told that the tool was not executed and should not retry the same+ tool call unless the user asks for it."""@@ -334,10 +335,15 @@ class HumanInTheLoopMiddleware(AgentMiddleware[StateT, ContextT, ResponseT]):None,)if decision["type"] == "reject" and "reject" in allowed_decisions:- content = decision.get("message") or (- f"User rejected the tool call for `{tool_call['name']}` with id {tool_call['id']}. "- "The tool was not executed. Do not retry this tool call unless the user "- "explicitly requests it."+ reason = decision.get("message")+ content = (+ f"User rejected the tool call for `{tool_call['name']}` with reason: {reason}"+ if reason+ else (+ f"User rejected the tool call for `{tool_call['name']}` with id "+ f"{tool_call['id']}. The tool was not executed. Do not retry this tool "+ "call unless the user explicitly requests it."+ ))tool_message = ToolMessage(content=content,diff --git a/libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_human_in_the_loop.py b/libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_human_in_the_loop.pyindex f36c508fa1..d940ef507d 100644--- a/libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_human_in_the_loop.py+++ b/libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_human_in_the_loop.py@@ -149,8 +149,8 @@ def test_human_in_the_loop_middleware_single_tool_edit() -> None:assert result["messages"][0].tool_calls[0]["id"] == "1" # ID should be preserved-def test_human_in_the_loop_middleware_single_tool_response() -> None:- """Test HumanInTheLoopMiddleware with single tool response with custom message."""+def test_human_in_the_loop_middleware_single_tool_rejection_reason() -> None:+ """Test a custom rejection reason retains its human-provided context."""middleware = HumanInTheLoopMiddleware(interrupt_on={"test_tool": {"allowed_decisions": ["approve", "edit", "reject"]}})@@ -173,7 +173,9 @@ def test_human_in_the_loop_middleware_single_tool_response() -> None:assert len(result["messages"]) == 2assert isinstance(result["messages"][0], AIMessage)assert isinstance(result["messages"][1], ToolMessage)- assert result["messages"][1].content == "Custom response message"+ assert result["messages"][1].content == (+ "User rejected the tool call for `test_tool` with reason: Custom response message"+ )assert result["messages"][1].name == "test_tool"assert result["messages"][1].tool_call_id == "1"@@ -458,7 +460,10 @@ def test_human_in_the_loop_middleware_multiple_tools_mixed_responses() -> None:# Second message should be the tool message for the rejected tool calltool_message = result["messages"][1]assert isinstance(tool_message, ToolMessage)- assert tool_message.content == "User rejected this tool call"+ assert tool_message.content == (+ "User rejected the tool call for `get_temperature` with reason: "+ "User rejected this tool call"+ )assert tool_message.name == "get_temperature"@@ -1014,7 +1019,9 @@ def test_human_in_the_loop_middleware_preserves_order_with_rejections() -> None:# Check rejection tool messagetool_message = result["messages"][1]assert isinstance(tool_message, ToolMessage)- assert tool_message.content == "Rejected tool B"+ assert tool_message.content == (+ "User rejected the tool call for `tool_b` with reason: Rejected tool B"+ )assert tool_message.tool_call_id == "id_b"