Skip to content

Commit b62a9b5

Browse files
fix(langchain_v1): removed unsed functions in tool_call_limit middleware (#33735)
These functions seem unused and can be removed.
1 parent 76dd656 commit b62a9b5

File tree

2 files changed

+1
-126
lines changed

2 files changed

+1
-126
lines changed

libs/langchain_v1/langchain/agents/middleware/tool_call_limit.py

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from typing import TYPE_CHECKING, Annotated, Any, Literal
66

7-
from langchain_core.messages import AIMessage, AnyMessage, HumanMessage
7+
from langchain_core.messages import AIMessage
88
from langgraph.channels.untracked_value import UntrackedValue
99
from typing_extensions import NotRequired
1010

@@ -33,53 +33,6 @@ class ToolCallLimitState(AgentState):
3333
run_tool_call_count: NotRequired[Annotated[dict[str, int], UntrackedValue, PrivateStateAttr]]
3434

3535

36-
def _count_tool_calls_in_messages(messages: list[AnyMessage], tool_name: str | None = None) -> int:
37-
"""Count tool calls in a list of messages.
38-
39-
Args:
40-
messages: List of messages to count tool calls in.
41-
tool_name: If specified, only count calls to this specific tool.
42-
If `None`, count all tool calls.
43-
44-
Returns:
45-
The total number of tool calls (optionally filtered by tool_name).
46-
"""
47-
count = 0
48-
for message in messages:
49-
if isinstance(message, AIMessage) and message.tool_calls:
50-
if tool_name is None:
51-
# Count all tool calls
52-
count += len(message.tool_calls)
53-
else:
54-
# Count only calls to the specified tool
55-
count += sum(1 for tc in message.tool_calls if tc["name"] == tool_name)
56-
return count
57-
58-
59-
def _get_run_messages(messages: list[AnyMessage]) -> list[AnyMessage]:
60-
"""Get messages from the current run (after the last HumanMessage).
61-
62-
Args:
63-
messages: Full list of messages.
64-
65-
Returns:
66-
Messages from the current run (after last HumanMessage).
67-
"""
68-
# Find the last HumanMessage
69-
last_human_index = -1
70-
for i in range(len(messages) - 1, -1, -1):
71-
if isinstance(messages[i], HumanMessage):
72-
last_human_index = i
73-
break
74-
75-
# If no HumanMessage found, return all messages
76-
if last_human_index == -1:
77-
return messages
78-
79-
# Return messages after the last HumanMessage
80-
return messages[last_human_index + 1 :]
81-
82-
8336
def _build_tool_limit_exceeded_message(
8437
thread_count: int,
8538
run_count: int,

libs/langchain_v1/tests/unit_tests/agents/test_tool_call_limit.py

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -9,88 +9,10 @@
99
from langchain.agents.middleware.tool_call_limit import (
1010
ToolCallLimitExceededError,
1111
ToolCallLimitMiddleware,
12-
_count_tool_calls_in_messages,
13-
_get_run_messages,
1412
)
1513
from tests.unit_tests.agents.test_middleware_agent import FakeToolCallingModel
1614

1715

18-
def test_count_tool_calls_in_messages():
19-
"""Test counting tool calls in messages."""
20-
# Test with no messages
21-
assert _count_tool_calls_in_messages([]) == 0
22-
23-
# Test with messages but no tool calls
24-
messages = [
25-
HumanMessage("Hello"),
26-
AIMessage("Hi there"),
27-
]
28-
assert _count_tool_calls_in_messages(messages) == 0
29-
30-
# Test with tool calls
31-
messages = [
32-
HumanMessage("Search for something"),
33-
AIMessage(
34-
"Searching...",
35-
tool_calls=[
36-
{"name": "search", "args": {"query": "test"}, "id": "1"},
37-
{"name": "calculator", "args": {"expression": "1+1"}, "id": "2"},
38-
],
39-
),
40-
ToolMessage("Result 1", tool_call_id="1"),
41-
ToolMessage("Result 2", tool_call_id="2"),
42-
AIMessage(
43-
"More searching...",
44-
tool_calls=[
45-
{"name": "search", "args": {"query": "another"}, "id": "3"},
46-
],
47-
),
48-
]
49-
# Total: 3 tool calls (2 from first AI message, 1 from second)
50-
assert _count_tool_calls_in_messages(messages) == 3
51-
52-
# Test filtering by tool name
53-
assert _count_tool_calls_in_messages(messages, tool_name="search") == 2
54-
assert _count_tool_calls_in_messages(messages, tool_name="calculator") == 1
55-
assert _count_tool_calls_in_messages(messages, tool_name="nonexistent") == 0
56-
57-
58-
def test_get_run_messages():
59-
"""Test extracting run messages after last HumanMessage."""
60-
# Test with no messages
61-
assert _get_run_messages([]) == []
62-
63-
# Test with no HumanMessage
64-
messages = [
65-
AIMessage("Hello"),
66-
AIMessage("World"),
67-
]
68-
assert _get_run_messages(messages) == messages
69-
70-
# Test with HumanMessage at the end
71-
messages = [
72-
AIMessage("Previous"),
73-
HumanMessage("New question"),
74-
]
75-
assert _get_run_messages(messages) == []
76-
77-
# Test with messages after HumanMessage
78-
messages = [
79-
AIMessage("Previous"),
80-
ToolMessage("Previous result", tool_call_id="0"),
81-
HumanMessage("New question"),
82-
AIMessage(
83-
"Response",
84-
tool_calls=[{"name": "search", "args": {}, "id": "1"}],
85-
),
86-
ToolMessage("Result", tool_call_id="1"),
87-
]
88-
run_messages = _get_run_messages(messages)
89-
assert len(run_messages) == 2
90-
assert isinstance(run_messages[0], AIMessage)
91-
assert isinstance(run_messages[1], ToolMessage)
92-
93-
9416
def test_middleware_initialization_validation():
9517
"""Test that middleware initialization validates parameters correctly."""
9618
# Test that at least one limit must be specified

0 commit comments

Comments
 (0)