Skip to content

Fix: Raises value error for invalid tool name instead of key error #280

Merged
wang-boyu merged 2 commits intomesa:mainfrom
DineshThumma9:fix/tool-name-value-error
Apr 11, 2026
Merged

Fix: Raises value error for invalid tool name instead of key error #280
wang-boyu merged 2 commits intomesa:mainfrom
DineshThumma9:fix/tool-name-value-error

Conversation

@DineshThumma9
Copy link
Copy Markdown
Contributor

@DineshThumma9 DineshThumma9 commented Mar 30, 2026

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:

  • unknown tool names
  • available tool names (sorted)

No behavior change for valid tool names.

Also updated test expectations in:

   test_tool_manager.py

Specifically, the nonexistent tool case now expects ValueError (with message match) instead of KeyError.

Testing

Executed targeted test suite for ToolManager:

source .venv/bin/activate
pytest tests/test_tools/test_tool_manager.py -q

Result:

All tests passed (28 passed)
Existing valid behavior is preserved
Nonexistent selected tool path now fails with explicit user-facing error as intended

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 30, 2026

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 252009d5-c253-4c8b-9b85-00f949f77a67

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ 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 and usage tips.

@DineshThumma9 DineshThumma9 changed the title raised value error for invalid tool name instead of key error Fix: Raises value error for invalid tool name instead of key error Mar 30, 2026
Copy link
Copy Markdown
Contributor

@BhoomiAgrawal12 BhoomiAgrawal12 left a comment

Choose a reason for hiding this comment

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

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",
)
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.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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}",
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.

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)}"
)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

available_tools is already initialized from sorted(self.tools.keys()), so the list is deterministic at the point it’s assigned

@wang-boyu wang-boyu added the bug Release notes label label Apr 11, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 11, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.55%. Comparing base (b58bdfa) to head (5f489be).
⚠️ Report is 3 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@wang-boyu
Copy link
Copy Markdown
Member

Thanks for the fix. I merged main into this PR branch to resolve some merge conflicts. Ready to merge now.

@wang-boyu wang-boyu merged commit b6ece59 into mesa:main Apr 11, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label tools

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Unknown selected tool names raise bare KeyError instead should raise value error with tool name

3 participants