Skip to content

Issue 3924 Fix MethodToolCallback enum argument handling #3933

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MethodToolCallback does not handle invalid arg gracefully
1 participant