Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/crewai/src/crewai/tools/tool_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ def _original_tool_calling(

if not isinstance(arguments, dict):
if raise_error:
raise
raise ToolUsageError(f"{I18N_DEFAULT.errors('tool_arguments_error')}")
return ToolUsageError(f"{I18N_DEFAULT.errors('tool_arguments_error')}")

return ToolCalling(
Expand Down
26 changes: 25 additions & 1 deletion lib/crewai/tests/tools/test_tool_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
)
from crewai.tools import BaseTool
from crewai.tools.tool_calling import ToolCalling
from crewai.tools.tool_usage import ToolUsage
from crewai.tools.tool_usage import ToolUsage, ToolUsageError
from crewai.utilities.i18n import I18N_DEFAULT
from crewai.utilities.tool_utils import execute_tool_and_check_finality
from pydantic import BaseModel, Field
import pytest
Expand Down Expand Up @@ -903,3 +904,26 @@ def on_finished(source, event):
assert len(finished_events) == 0, (
"ToolUsageFinishedEvent should NOT be emitted after ToolUsageErrorEvent"
)


def test_original_tool_calling_raises_tool_usage_error_for_non_dict_arguments():
"""Regression test for #6430: non-dict arguments must raise ToolUsageError,
not a bare `raise` (which would raise RuntimeError: No active exception)."""
tool_usage = ToolUsage(
tools_handler=MagicMock(),
tools=[RandomNumberTool()],
task=MagicMock(),
function_calling_llm=MagicMock(),
agent=example_agent,
action=MagicMock(tool="Random Number Generator", tool_input="[]"),
)

with patch.object(tool_usage, "_validate_tool_input", return_value=["not", "a", "dict"]):
with pytest.raises(ToolUsageError) as e_info:
tool_usage._original_tool_calling("irrelevant", raise_error=True)

assert str(e_info.value) == I18N_DEFAULT.errors("tool_arguments_error")

result = tool_usage._original_tool_calling("irrelevant", raise_error=False)
assert isinstance(result, ToolUsageError)
assert str(result) == I18N_DEFAULT.errors("tool_arguments_error")