Skip to content

fix(tools): replace bare raise with ToolUsageError in _original_tool_calling#6431

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

fix(tools): replace bare raise with ToolUsageError in _original_tool_calling#6431
molinto wants to merge 2 commits into
crewAIInc:mainfrom
molinto:fix/bare-raise-tool-usage

Conversation

@molinto

@molinto molinto commented Jul 2, 2026

Copy link
Copy Markdown

Fixes #6430

Problem

ToolUsage._original_tool_calling() contains a bare raise outside any except block:

if not isinstance(arguments, dict):
    if raise_error:
        raise   # <- no active exception here
    return ToolUsageError(f"{I18N_DEFAULT.errors('tool_arguments_error')}")

A bare raise re-raises the exception currently being handled. On this path no exception is active (the preceding try/except completed normally), so if _validate_tool_input ever returns a non-dict, Python raises RuntimeError: No active exception to re-raise instead of the intended tool-arguments error.

Today _validate_tool_input always returns a dict or raises, so the branch is a latent landmine rather than a hot path — but _tool_calling() calls this with raise_error=True and relies on catching a meaningful exception to trigger its fallback, so any future change or subclass of _validate_tool_input would surface this as a confusing RuntimeError.

ruff check --select PLE0704 also flags this line. (It flags one more bare raise in crewai/agent/core.py:660; that one is only ever reached from within an exception context via _handle_execution_error, so it behaves correctly at runtime — left untouched here.)

Fix

Raise ToolUsageError (an Exception subclass) with the same i18n message the non-raising branch returns, keeping both branches consistent.

Tests

Added a regression test in lib/crewai/tests/tools/test_tool_usage.py that forces _validate_tool_input to return a non-dict and asserts ToolUsageError is raised (previously: RuntimeError). Verified failing-without / passing-with the fix. ruff clean on the touched file.

Note for maintainers

This contribution was made with AI assistance. Per CONTRIBUTING.md the llm-generated label is required — I don't have triage permission to set it, so could a maintainer please add it?

@coderabbitai

coderabbitai Bot commented Jul 2, 2026

Copy link
Copy Markdown

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: fd77308b-1df8-476f-a453-127e0dd3e202

📥 Commits

Reviewing files that changed from the base of the PR and between 559a9c6 and bd79e34.

📒 Files selected for processing (3)
  • fix-bare-raise-tool-usage.patch
  • lib/crewai/src/crewai/tools/tool_usage.py
  • lib/crewai/tests/tools/test_tool_usage.py
👮 Files not reviewed due to content moderation or server errors (3)
  • lib/crewai/tests/tools/test_tool_usage.py
  • lib/crewai/src/crewai/tools/tool_usage.py
  • fix-bare-raise-tool-usage.patch

📝 Walkthrough

[!WARNING]

Walkthrough skipped

File diffs could not be summarized.

✨ 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