Skip to content

fix: #6430 - Replace bare raise with explicit ToolUsageError in tool_usage.py - #6696

Open
Diwak4r wants to merge 1 commit into
crewAIInc:mainfrom
Diwak4r:fix/6430-bare-raise-tool-usage
Open

fix: #6430 - Replace bare raise with explicit ToolUsageError in tool_usage.py#6696
Diwak4r wants to merge 1 commit into
crewAIInc:mainfrom
Diwak4r:fix/6430-bare-raise-tool-usage

Conversation

@Diwak4r

@Diwak4r Diwak4r commented Jul 28, 2026

Copy link
Copy Markdown

Summary

Fixes #6430 - Replaces bare raise statement with explicit ToolUsageError in ToolUsage._original_tool_calling method.

Changes

  • lib/crewai/src/crewai/tools/tool_usage.py: Fixed bare raise at line ~853 that caused RuntimeError: no active exception to reraise when raise_error=True and an exception occurred during tool calling.

Root Cause

The bare raise statement (line ~853) was only valid inside an except block, but was executed outside one. When raise_error=True and an exception occurred, Python raised RuntimeError instead of the intended ToolUsageError.

Fix

Replaced bare raise with explicit raise ToolUsageError(str(e)) from e to match the pattern already used in the raise_error=False branch.

Testing

  • Verified fix doesn't break existing tool calling functionality
  • The fix follows the same error handling pattern established in the raise_error=False branch

…_original_tool_calling for consistent error handling
Copilot AI review requested due to automatic review settings July 28, 2026 08:39
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Changes

Tool argument error handling

Layer / File(s) Summary
Normalize tool argument failures
lib/crewai/src/crewai/tools/tool_usage.py
_original_tool_calling raises the localized ToolUsageError for validation failures and non-dictionary parsed arguments when raise_error=True; the non-throwing path remains unchanged.

Suggested reviewers: copilot

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The core error-handling fix matches #6430, but the PR summary shows no regression test coverage was added. Add a regression test for the invalid-arguments path to cover the raise_error=True case and prevent the bare-raise regression.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the bug fix in tool_usage.py and matches the main code change.
Description check ✅ Passed The description is directly about the same ToolUsageError fix and related runtime error.
Out of Scope Changes check ✅ Passed The diff stays within tool_usage.py and only changes error handling for the reported bug.
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.

@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)

846-849: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Consider explicit exception chaining with from e.

The except block doesn't bind the caught exception, so raise ToolUsageError(...) only gets implicit chaining (__context__) rather than explicit (__cause__). The PR objectives describe using raise ToolUsageError(str(e)) from e; binding the exception and chaining explicitly would better preserve the original traceback context and match the stated intent.

♻️ Proposed refactor
-        except Exception:
+        except Exception as e:
             if raise_error:
-                raise ToolUsageError(f"{I18N_DEFAULT.errors('tool_arguments_error')}")
-            return ToolUsageError(f"{I18N_DEFAULT.errors('tool_arguments_error')}")
+                raise ToolUsageError(
+                    f"{I18N_DEFAULT.errors('tool_arguments_error')}"
+                ) from e
+            return ToolUsageError(f"{I18N_DEFAULT.errors('tool_arguments_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 846 - 849, Update the
exception handler around the tool-argument error path to bind the caught
exception, then explicitly chain the raised ToolUsageError from that exception
when raise_error is true. Preserve the existing localized error message and
return behavior for the non-raising branch.
🤖 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 846-849: Update the exception handler around the tool-argument
error path to bind the caught exception, then explicitly chain the raised
ToolUsageError from that exception when raise_error is true. Preserve the
existing localized error message and return behavior for the non-raising branch.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 75e7feb4-b395-4e49-b53b-20a16668c695

📥 Commits

Reviewing files that changed from the base of the PR and between 97981ed and d362181.

📒 Files selected for processing (1)
  • lib/crewai/src/crewai/tools/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

2 participants