Skip to content

Fix ToolUsage bare raise for non-dict arguments#6457

Open
initiallyqq wants to merge 2 commits into
crewAIInc:mainfrom
initiallyqq:fix-tool-usage-bare-raise
Open

Fix ToolUsage bare raise for non-dict arguments#6457
initiallyqq wants to merge 2 commits into
crewAIInc:mainfrom
initiallyqq:fix-tool-usage-bare-raise

Conversation

@initiallyqq

Copy link
Copy Markdown

Summary

  • Replace the bare raise in ToolUsage._original_tool_calling with ToolUsageError for non-dict arguments.
  • Add a regression test for the raise_error=True path.

Fixes #6430

Tests

  • ./.venv/Scripts/python.exe -m pytest lib/crewai/tests/tools/test_tool_usage.py::test_original_tool_calling_raises_tool_usage_error_for_non_dict_arguments -q
  • ./.venv/Scripts/ruff.exe check lib/crewai/src/crewai/tools/tool_usage.py --select PLE0704

Note: the full test_tool_usage.py file has 5 existing failures on Windows because pytest-recording disables socket creation for the event bus; the new regression test passes.

@coderabbitai

coderabbitai Bot commented Jul 4, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: a561c530-81f1-4d3d-90fb-b4b69788cdeb

📥 Commits

Reviewing files that changed from the base of the PR and between 7bdd13d and c048958.

📒 Files selected for processing (1)
  • lib/crewai/src/crewai/tools/tool_usage.py

📝 Walkthrough

Walkthrough

Fixed the non-dict tool-argument path in ToolUsage._original_tool_calling to raise ToolUsageError with the localized message instead of a bare raise, and added a regression test covering that case.

Changes

ToolUsageError fix

Layer / File(s) Summary
Raise ToolUsageError instead of bare raise
lib/crewai/src/crewai/tools/tool_usage.py, lib/crewai/tests/tools/test_tool_usage.py
Builds a shared localized tool_arguments_error, returns it on validation failure when raise_error=False, raises it for non-dict arguments when raise_error=True, and adds a test that patches _validate_tool_input to return a list and asserts ToolUsageError is raised.

Related Issues: #6430

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main fix in ToolUsage for non-dict arguments.
Description check ✅ Passed The description is directly about the same ToolUsage bug fix and regression test.
Linked Issues check ✅ Passed The changes address #6430 by replacing the bare raise path with ToolUsageError and adding a regression test.
Out of Scope Changes check ✅ Passed The code changes and test stay within the reported bug fix scope.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@initiallyqq initiallyqq changed the title fix-tool-usage-bare-raise Fix ToolUsage bare raise for non-dict arguments Jul 4, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
lib/crewai/src/crewai/tools/tool_usage.py (1)

838-859: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Fix confirmed correct; minor duplication remains.

The fix at Line 853 correctly replaces the bare raise (which would have hit RuntimeError: No active exception to reraise since this branch is outside the except block) with an explicit ToolUsageError. This matches the localized tool_arguments_error message used elsewhere in this file.

Minor note: the same ToolUsageError(f"{I18N_DEFAULT.errors('tool_arguments_error')}") construction is now duplicated three times in this function (lines 849, 853, 854). Consider hoisting it into a local variable for readability.

♻️ Optional simplification
         if not isinstance(arguments, dict):
+            error = ToolUsageError(f"{I18N_DEFAULT.errors('tool_arguments_error')}")
             if raise_error:
-                raise ToolUsageError(f"{I18N_DEFAULT.errors('tool_arguments_error')}")
-            return ToolUsageError(f"{I18N_DEFAULT.errors('tool_arguments_error')}")
+                raise error
+            return error
🤖 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/src/crewai/tools/tool_usage.py` around lines 838 - 859, The
`_original_tool_calling` method now has the same
`ToolUsageError(f"{I18N_DEFAULT.errors('tool_arguments_error')}")` repeated in
multiple branches, which adds unnecessary duplication. Refactor this method by
defining the localized tool-arguments error once inside `_original_tool_calling`
and reuse it in both the `except` path and the `not isinstance(arguments, dict)`
path, while keeping the explicit `ToolUsageError` behavior in `tool_usage.py`.
🤖 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/src/crewai/tools/tool_usage.py`:
- Around line 838-859: The `_original_tool_calling` method now has the same
`ToolUsageError(f"{I18N_DEFAULT.errors('tool_arguments_error')}")` repeated in
multiple branches, which adds unnecessary duplication. Refactor this method by
defining the localized tool-arguments error once inside `_original_tool_calling`
and reuse it in both the `except` path and the `not isinstance(arguments, dict)`
path, while keeping the explicit `ToolUsageError` behavior in `tool_usage.py`.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 6a129971-2ddb-46e0-838d-722ea40d93dc

📥 Commits

Reviewing files that changed from the base of the PR and between 2b90117 and 7bdd13d.

📒 Files selected for processing (2)
  • lib/crewai/src/crewai/tools/tool_usage.py
  • lib/crewai/tests/tools/test_tool_usage.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Bare raise in ToolUsage._original_tool_calling causes RuntimeError instead of ToolUsageError

1 participant