mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-26 08:38:15 +00:00
Fix duplicate tool execution in structured_tool.invoke()
- Remove duplicate function call on line 285 that caused tools to execute twice - Add comprehensive test to prevent regression - Fixes issue #3489 Co-Authored-By: João <joao@crewai.com>
This commit is contained in:
@@ -282,8 +282,6 @@ class CrewStructuredTool:
|
|||||||
except Exception:
|
except Exception:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
result = self.func(**parsed_args, **kwargs)
|
|
||||||
|
|
||||||
if asyncio.iscoroutine(result):
|
if asyncio.iscoroutine(result):
|
||||||
return asyncio.run(result)
|
return asyncio.run(result)
|
||||||
|
|
||||||
|
|||||||
@@ -144,6 +144,32 @@ def test_default_values_in_schema():
|
|||||||
)
|
)
|
||||||
assert result == "test custom 42"
|
assert result == "test custom 42"
|
||||||
|
|
||||||
|
|
||||||
|
def test_tool_not_executed_twice():
|
||||||
|
"""Test that tool function is only executed once per invoke call (bug #3489)"""
|
||||||
|
call_count = 0
|
||||||
|
|
||||||
|
def counting_func(param: str) -> str:
|
||||||
|
"""Function that counts how many times it's called."""
|
||||||
|
nonlocal call_count
|
||||||
|
call_count += 1
|
||||||
|
return f"Called {call_count} times with {param}"
|
||||||
|
|
||||||
|
tool = CrewStructuredTool.from_function(
|
||||||
|
func=counting_func, name="counting_tool", description="Counts calls"
|
||||||
|
)
|
||||||
|
|
||||||
|
call_count = 0
|
||||||
|
|
||||||
|
result = tool.invoke({"param": "test"})
|
||||||
|
|
||||||
|
assert call_count == 1, f"Expected function to be called once, but was called {call_count} times"
|
||||||
|
assert result == "Called 1 times with test"
|
||||||
|
|
||||||
|
result = tool.invoke({"param": "test2"})
|
||||||
|
assert call_count == 2, f"Expected function to be called twice total, but was called {call_count} times"
|
||||||
|
assert result == "Called 2 times with test2"
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def custom_tool_decorator():
|
def custom_tool_decorator():
|
||||||
from crewai.tools import tool
|
from crewai.tools import tool
|
||||||
@@ -227,4 +253,4 @@ def test_async_tool_using_decorator_within_flow(custom_tool_decorator):
|
|||||||
|
|
||||||
flow = StructuredExampleFlow()
|
flow = StructuredExampleFlow()
|
||||||
result = flow.kickoff()
|
result = flow.kickoff()
|
||||||
assert result.raw == "Hello World from Custom Tool"
|
assert result.raw == "Hello World from Custom Tool"
|
||||||
|
|||||||
Reference in New Issue
Block a user