The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
fix(langchain_v1): removed unsed functions in tool_call_limit middleware (#33735)
Christian Bromannb62a9b57f346
diff --git a/libs/langchain_v1/langchain/agents/middleware/tool_call_limit.py b/libs/langchain_v1/langchain/agents/middleware/tool_call_limit.pyindex dca66e9682..4dc7b5cf53 100644--- a/libs/langchain_v1/langchain/agents/middleware/tool_call_limit.py+++ b/libs/langchain_v1/langchain/agents/middleware/tool_call_limit.py@@ -4,7 +4,7 @@ from __future__ import annotationsfrom typing import TYPE_CHECKING, Annotated, Any, Literal-from langchain_core.messages import AIMessage, AnyMessage, HumanMessage+from langchain_core.messages import AIMessagefrom langgraph.channels.untracked_value import UntrackedValuefrom typing_extensions import NotRequired@@ -33,53 +33,6 @@ class ToolCallLimitState(AgentState):run_tool_call_count: NotRequired[Annotated[dict[str, int], UntrackedValue, PrivateStateAttr]]-def _count_tool_calls_in_messages(messages: list[AnyMessage], tool_name: str | None = None) -> int:- """Count tool calls in a list of messages.-- Args:- messages: List of messages to count tool calls in.- tool_name: If specified, only count calls to this specific tool.- If `None`, count all tool calls.-- Returns:- The total number of tool calls (optionally filtered by tool_name).- """- count = 0- for message in messages:- if isinstance(message, AIMessage) and message.tool_calls:- if tool_name is None:- # Count all tool calls- count += len(message.tool_calls)- else:- # Count only calls to the specified tool- count += sum(1 for tc in message.tool_calls if tc["name"] == tool_name)- return count---def _get_run_messages(messages: list[AnyMessage]) -> list[AnyMessage]:- """Get messages from the current run (after the last HumanMessage).-- Args:- messages: Full list of messages.-- Returns:- Messages from the current run (after last HumanMessage).- """- # Find the last HumanMessage- last_human_index = -1- for i in range(len(messages) - 1, -1, -1):- if isinstance(messages[i], HumanMessage):- last_human_index = i- break-- # If no HumanMessage found, return all messages- if last_human_index == -1:- return messages-- # Return messages after the last HumanMessage- return messages[last_human_index + 1 :]--def _build_tool_limit_exceeded_message(thread_count: int,run_count: int,diff --git a/libs/langchain_v1/tests/unit_tests/agents/test_tool_call_limit.py b/libs/langchain_v1/tests/unit_tests/agents/test_tool_call_limit.pyindex e283aad527..8498926a30 100644--- a/libs/langchain_v1/tests/unit_tests/agents/test_tool_call_limit.py+++ b/libs/langchain_v1/tests/unit_tests/agents/test_tool_call_limit.py@@ -9,88 +9,10 @@ from langchain.agents.factory import create_agentfrom langchain.agents.middleware.tool_call_limit import (ToolCallLimitExceededError,ToolCallLimitMiddleware,- _count_tool_calls_in_messages,- _get_run_messages,)from tests.unit_tests.agents.test_middleware_agent import FakeToolCallingModel-def test_count_tool_calls_in_messages():- """Test counting tool calls in messages."""- # Test with no messages- assert _count_tool_calls_in_messages([]) == 0-- # Test with messages but no tool calls- messages = [- HumanMessage("Hello"),- AIMessage("Hi there"),- ]- assert _count_tool_calls_in_messages(messages) == 0-- # Test with tool calls- messages = [- HumanMessage("Search for something"),- AIMessage(- "Searching...",- tool_calls=[- {"name": "search", "args": {"query": "test"}, "id": "1"},- {"name": "calculator", "args": {"expression": "1+1"}, "id": "2"},- ],- ),- ToolMessage("Result 1", tool_call_id="1"),- ToolMessage("Result 2", tool_call_id="2"),- AIMessage(- "More searching...",- tool_calls=[- {"name": "search", "args": {"query": "another"}, "id": "3"},- ],- ),- ]- # Total: 3 tool calls (2 from first AI message, 1 from second)- assert _count_tool_calls_in_messages(messages) == 3-- # Test filtering by tool name- assert _count_tool_calls_in_messages(messages, tool_name="search") == 2- assert _count_tool_calls_in_messages(messages, tool_name="calculator") == 1- assert _count_tool_calls_in_messages(messages, tool_name="nonexistent") == 0---def test_get_run_messages():- """Test extracting run messages after last HumanMessage."""- # Test with no messages- assert _get_run_messages([]) == []-- # Test with no HumanMessage- messages = [- AIMessage("Hello"),- AIMessage("World"),- ]- assert _get_run_messages(messages) == messages-- # Test with HumanMessage at the end- messages = [- AIMessage("Previous"),- HumanMessage("New question"),- ]- assert _get_run_messages(messages) == []-- # Test with messages after HumanMessage- messages = [- AIMessage("Previous"),- ToolMessage("Previous result", tool_call_id="0"),- HumanMessage("New question"),- AIMessage(- "Response",- tool_calls=[{"name": "search", "args": {}, "id": "1"}],- ),- ToolMessage("Result", tool_call_id="1"),- ]- run_messages = _get_run_messages(messages)- assert len(run_messages) == 2- assert isinstance(run_messages[0], AIMessage)- assert isinstance(run_messages[1], ToolMessage)--def test_middleware_initialization_validation():"""Test that middleware initialization validates parameters correctly."""# Test that at least one limit must be specified