Skip to content

Conversation

@wilocu
Copy link
Contributor

@wilocu wilocu commented Jul 28, 2025

Summary

Fixes MethodToolCallback not handling invalid enum arguments gracefully by converting
IllegalArgumentException to ToolExecutionException.

Closes #3924

Changes Made

  • Exception Handling: Added try-catch block around buildMethodArguments() in
    MethodToolCallback.call()
  • Exception Conversion: Convert IllegalArgumentException to ToolExecutionException with
    proper context
  • Logging: Added debug logging for argument conversion failures following existing patterns
  • Test Coverage: Added comprehensive unit tests for both invalid and valid enum scenarios

Technical Details

  • Root Cause: IllegalArgumentException was thrown during enum conversion in
    JsonParser.toTypedObject() but not caught by MethodToolCallback
  • Solution: Wrap argument building phase with exception handling at lines 108-115 in
    MethodToolCallback.java
  • Integration: Maintains compatibility with existing ToolExecutionExceptionProcessor pipeline

Test Results

  • ✅ New unit tests pass (invalid enum throws ToolExecutionException)
  • ✅ Existing functionality preserved (valid enum values work normally)
  • ✅ No regressions in MethodToolCallbackProviderTests and DefaultToolCallingManagerTests

Files Modified

  • spring-ai-model/src/main/java/org/springframework/ai/tool/method/MethodToolCallback.java
  • spring-ai-model/src/test/java/org/springframework/ai/tool/method/MethodToolCallbackTests.java
    (new file)

This change allows LLMs that provide invalid enum values to receive graceful error responses
instead of unhandled exceptions.
Signed-off-by: Mattia Pasetto [email protected]

Please make sure to double check and let me know if anything is wrong, this is my first contribution.

Signed-off-by: Mattia Pasetto <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MethodToolCallback does not handle invalid arg gracefully

2 participants