Skip to content

fix: bare raise in ToolUsage._original_tool_calling causes RuntimeError instead of ToolUsageError#6455

Open
rafaelipuente wants to merge 2 commits into
crewAIInc:mainfrom
rafaelipuente:fix/toolusage-bare-raise
Open

fix: bare raise in ToolUsage._original_tool_calling causes RuntimeError instead of ToolUsageError#6455
rafaelipuente wants to merge 2 commits into
crewAIInc:mainfrom
rafaelipuente:fix/toolusage-bare-raise

Conversation

@rafaelipuente

Copy link
Copy Markdown

Description

Fixes #6430.

ToolUsage._original_tool_calling() contained a bare raise in the not isinstance(arguments, dict) branch. A bare raise re-raises the exception currently being handled, but on this path no exception is active (the preceding try/except completed normally). If that branch were ever hit with raise_error=True, Python would crash with RuntimeError: No active exception to re-raise instead of the intended tool-arguments error.

This matters because _tool_calling() calls this method with raise_error=True and relies on catching a meaningful exception to trigger its function-calling/retry fallback.

Fix

Replace the bare raise with an explicit raise ToolUsageError(...) using the same tool_arguments_error message the branch already returns when raise_error=False, keeping error messaging consistent.

Testing

  • Added a regression test that patches _validate_tool_input to return a non-dict and asserts _original_tool_calling(..., raise_error=True) raises ToolUsageError (not RuntimeError), and that the non-raise path returns a ToolUsageError instance.
  • uv run pytest lib/crewai/tests/tools/test_tool_usage.py passes (29 tests).
  • ruff check --select PLE0704 no longer flags the file; ruff lint/format clean on changed files.

(Note: authored with the assistance of an AI coding agent. Please apply the llm-generated label.)

Made with Cursor

@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: 81ce23b1-c226-4faa-b2de-e23ccfa4acd5

📥 Commits

Reviewing files that changed from the base of the PR and between 2b90117 and 271e545.

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

📝 Walkthrough

Walkthrough

A bare raise statement in ToolUsage._original_tool_calling's non-dict-arguments branch was replaced with an explicit raise ToolUsageError(...) using the localized error message. A corresponding regression test was added to verify the new exception behavior.

Changes

ToolUsageError Fix

Layer / File(s) Summary
Raise ToolUsageError instead of bare raise
lib/crewai/src/crewai/tools/tool_usage.py
When raise_error=True and validated arguments are non-dict, the code now raises ToolUsageError with the localized "tool_arguments_error" message instead of re-raising a nonexistent active exception.
Regression test
lib/crewai/tests/tools/test_tool_usage.py
Imports ToolUsageError and adds a test patching _validate_tool_input/_select_tool to confirm _original_tool_calling raises ToolUsageError with raise_error=True and returns a ToolUsageError instance otherwise.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the bug fix in ToolUsage._original_tool_calling and matches the main change.
Description check ✅ Passed The description is directly related to the changeset and explains the same fix and regression test.
Linked Issues check ✅ Passed The PR fixes the bare raise, raises ToolUsageError with the expected message, and adds regression coverage as requested [#6430].
Out of Scope Changes check ✅ Passed The changes stay narrowly focused on the reported bug and its regression test, with no clear unrelated additions.
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.

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