Fix: Raises value error for invalid tool name instead of key error #280
Fix: Raises value error for invalid tool name instead of key error #280
Conversation
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ 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.
It's a good fix @DineshThumma9 , however I have two suggestions please have a look.
| "Unknown tool name(s): " | ||
| f"{invalid_tools}. Available tools: {available_tools}", | ||
| color="red", | ||
| ) |
There was a problem hiding this comment.
Using style() inside a ValueError isn’t ideal here as exception messages should generally stay as plain strings since they’re often displayed programmatically. Styling can introduce ANSI codes into str(e), which makes debugging and assertions messy.
I’d suggest removing style() and keeping the message simple as it’s already clear and readable without formatting?
There was a problem hiding this comment.
I kept style() here to match the existing error-message formatting pattern used in tool_manager.py, so ValueError messages are displayed consistently across this module
| raise ValueError( | ||
| style( | ||
| "Unknown tool name(s): " | ||
| f"{invalid_tools}. Available tools: {available_tools}", |
There was a problem hiding this comment.
It might be helpful to make the error message deterministic and slightly more debug-friendly by using sorted(self.tools) so the available tools list is consistent across runs?
Something like:
raise ValueError(
f"Unknown tool name(s): {invalid_tools}. Available tools: {sorted(self.tools)}"
)There was a problem hiding this comment.
available_tools is already initialized from sorted(self.tools.keys()), so the list is deterministic at the point it’s assigned
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #280 +/- ##
==========================================
+ Coverage 91.52% 91.55% +0.02%
==========================================
Files 19 19
Lines 1641 1645 +4
==========================================
+ Hits 1502 1506 +4
Misses 139 139 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Thanks for the fix. I merged |
Note
Use this template for bug fixes only. For enhancements/new features, use the feature template and get maintainer approval in an issue/discussion before opening a PR.
Summary
ToolManager.get_all_tools_schema() currently raises a raw KeyError when selected_tools contains an unknown tool name. This leaks internal dictionary behavior to users and gives little guidance on how to fix the input.
This PR improves error handling by raising a clear ValueError that includes both invalid tool names and the list of available registered tools.
Bug / Issue
Fixes : #279
Context:
Method: ToolManager.get_all_tools_schema(selected_tools=...)
Expected : actionable validation error for unknown tool names
Actual : low-level KeyError from dictionary lookup
Impact:
Harder debugging for users configuring selected_tools
Poor DX in a core API path used by reasoning modules and custom tool pipelines
Implementation
Updated get_all_tools_schema() in tool_manager.py to:
1 .Validate all selected_tools names against self.tools
2 . Collect unknown tool names
3 . Raise ValueError with:
No behavior change for valid tool names.
Also updated test expectations in:
Specifically, the nonexistent tool case now expects ValueError (with message match) instead of KeyError.
Testing
Executed targeted test suite for ToolManager:
Result:
All tests passed (28 passed)
Existing valid behavior is preserved
Nonexistent selected tool path now fails with explicit user-facing error as intended