fix(tools): raise ToolUsageError instead of bare raise for non-dict arguments#6499
fix(tools): raise ToolUsageError instead of bare raise for non-dict arguments#6499nolanchic wants to merge 1 commit into
Conversation
…rguments _original_tool_calling used a bare outside any active except block when the parsed arguments were not a dict. With raise_error=True this raised rather than the intended ToolUsageError. Raise the explicit exception instead. Fixes crewAIInc#6430
📝 WalkthroughWalkthroughFixed a bare ChangesBare raise fix and regression tests
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
lib/crewai/tests/tools/test_tool_usage.py (1)
562-587: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winConsider asserting the error message in the
raise_error=Truetest.The PR objectives mention verifying "corrected exception type and message," but
pytest.raises(ToolUsageError)only checks the type. Adding amatch=parameter would also confirm the i18ntool_arguments_errormessage is surfaced, strengthening the regression test against future changes that might raise a differentToolUsageError.♻️ Proposed fix to assert the error message
- with pytest.raises(ToolUsageError): + with pytest.raises(ToolUsageError, match="not a valid key, value dictionary"): tool_usage._original_tool_calling("tool_string", raise_error=True)🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@lib/crewai/tests/tools/test_tool_usage.py` around lines 562 - 587, The regression test for ToolUsage._original_tool_calling currently only checks that ToolUsageError is raised; update the assertion in test_original_tool_calling_raises_tool_usage_error_for_non_dict_arguments to also verify the exception message with pytest.raises(..., match=...) so it confirms the i18n tool_arguments_error text is emitted when raise_error=True. Keep the existing mock setup and patching around _select_tool and _validate_tool_input, and strengthen the test so it validates both the exception type and message.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@lib/crewai/tests/tools/test_tool_usage.py`:
- Around line 562-587: The regression test for ToolUsage._original_tool_calling
currently only checks that ToolUsageError is raised; update the assertion in
test_original_tool_calling_raises_tool_usage_error_for_non_dict_arguments to
also verify the exception message with pytest.raises(..., match=...) so it
confirms the i18n tool_arguments_error text is emitted when raise_error=True.
Keep the existing mock setup and patching around _select_tool and
_validate_tool_input, and strengthen the test so it validates both the exception
type and message.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: bec184db-fdeb-49c6-8049-6766e0a9a424
📒 Files selected for processing (2)
lib/crewai/src/crewai/tools/tool_usage.pylib/crewai/tests/tools/test_tool_usage.py
Description
ToolUsage._original_tool_calling()used a bareraiseoutside any activeexceptblock when the parsed tool arguments were not adict:A bare
raisere-raises the exception currently being handled, but on this path the precedingtry/exceptcompleted normally, so no exception is active. Result: withraise_error=Truethe call raisedRuntimeError: No active exception to reraiseinstead of the intendedToolUsageError, masking the real tool-arguments problem.Fix: raise the explicit
ToolUsageError, matching the message already used by theraise_error=Falsebranch on the next line.Why the bug was latent
_validate_tool_input()currently always returns adictor raises, so the non-dict branch was effectively dead code in normal use. The bug surfaces whenever that contract is relaxed — e.g. subclasses overriding_validate_tool_input, monkeypatching in tests, or future changes to the validation pipeline. Making the branch raise the correct exception keeps the failure mode meaningful instead of an opaqueRuntimeError.Test plan
test_original_tool_calling_raises_tool_usage_error_for_non_dict_arguments— fails withRuntimeErroron the old code, passes withToolUsageErrorafter the fix.test_original_tool_calling_returns_tool_usage_error_for_non_dict_without_raisecovering theraise_error=Falsepath.uv run pytest lib/crewai/tests/tools/→ 135 passed.uv run ruff check lib/→ clean.uv run mypy lib/crewai/src/crewai/tools/tool_usage.py→ clean (strict).Fixes #6430.