Skip to content

Commit a591d5c

Browse files
authored
fix(tools): correct args validation in langchain tool wrapper (#761)
Fixed logic error in MelleaTool.from_langchain() where the condition `if args is not None or len(args) != 0:` was always True, causing false positive warnings on every tool call. Changed to `if args:` which correctly checks for non-empty tuples, since *args is always a tuple (never None) in Python. Added test_from_langchain_args_handling() to verify: - No warning when called with kwargs only (normal case) - Warning logged when positional args passed (edge case) Closes: #760 Signed-off-by: Mark Sturdevant <mark.sturdevant@ibm.com>
1 parent 989021a commit a591d5c

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

mellea/backends/tools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def from_langchain(cls, tool: Any) -> "MelleaTool":
9595

9696
def parameter_remapper(*args, **kwargs):
9797
"""Langchain tools expect their first argument to be 'tool_input'."""
98-
if args is not None or len(args) != 0:
98+
if args:
9999
# This shouldn't happen. Our ModelToolCall.call_func actually passes in everything as kwargs.
100100
FancyLogger.get_logger().warning(
101101
f"ignoring unexpected args while calling langchain tool ({tool_name}): ({args})"

test/backends/test_mellea_tool.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,30 @@ def test_from_langchain():
9999
assert t.run(input=2) == "2"
100100

101101

102+
def test_from_langchain_args_handling(caplog):
103+
"""Test that langchain tools handle args correctly.
104+
105+
Verifies that:
106+
1. Tools work correctly when called with kwargs only (normal case)
107+
2. A warning is logged if positional args are passed (shouldn't happen)
108+
"""
109+
t = MelleaTool.from_langchain(langchain_tool)
110+
111+
# Normal case: kwargs only (no warning expected)
112+
caplog.clear()
113+
result = t.run(input=5)
114+
assert result == "5"
115+
assert "ignoring unexpected args" not in caplog.text
116+
117+
# Edge case: positional args passed (warning expected)
118+
# This shouldn't happen in practice since ModelToolCall.call_func uses **kwargs
119+
caplog.clear()
120+
result = t.run(42, input=5) # Pass both positional and keyword args
121+
assert result == "5" # Should still work, using kwargs
122+
assert "ignoring unexpected args" in caplog.text
123+
assert "langchain_tool" in caplog.text
124+
125+
102126
@pytest.mark.qualitative
103127
@pytest.mark.ollama
104128
@pytest.mark.llm

0 commit comments

Comments
 (0)