Skip to content

Make validate_tool_arguments raise exception instead of returning error string #1634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

albertvillanova
Copy link
Member

@albertvillanova albertvillanova commented Jul 29, 2025

Make validate_tool_arguments raise exception instead of returning error string, improving code design and following Python conventions.

Changes Made

  • Updated validate_tool_arguments: Now raises ValueError and TypeError directly instead of returning error message strings
  • Removed periods from error messages: Following Python best practices for exception messages
  • Updated docstring: Added comprehensive Raises section documenting all possible exceptions
  • Updated tests: Modified test_validate_tool_arguments and test_validate_tool_arguments_nullable to use pytest.raises() for exception testing
  • Simplified caller code: The caller in execute_tool_call now has cleaner exception handling with proper exception chaining using from e

Benefits

  1. Removes tight coupling: The validation function is no longer tightly coupled to its caller that needs to convert returned error strings into exceptions
  2. Pythonic approach: Uses standard Python exceptions (ValueError/TypeError) following established conventions
  3. Better error handling: Proper exception chaining preserves the original context with from e
  4. Cleaner separation of concerns: Validation logic is separated from error handling context
  5. Consistent with Python stdlib: Follows the same pattern used throughout the Python standard library

Exception Types

  • ValueError: For invalid argument values (missing required args, args not in schema, etc.)
  • TypeError: For type mismatches that cannot be converted

The caller catches these exceptions and reraises them as AgentToolCallError with proper context, maintaining the existing error handling flow while improving the internal design.

@albertvillanova albertvillanova merged commit 155814f into huggingface:main Jul 29, 2025
3 checks passed
@albertvillanova albertvillanova deleted the refactor-validate-tool-arguments branch July 29, 2025 13:13
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.

1 participant