Skip to content

Tools MethodToolCallback throwing an IllegalArgumentException #2714

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
yuchaozhou opened this issue Apr 12, 2025 · 0 comments
Open

Tools MethodToolCallback throwing an IllegalArgumentException #2714

yuchaozhou opened this issue Apr 12, 2025 · 0 comments

Comments

@yuchaozhou
Copy link

Bug description
In the latest version of the Spring AI framework (m7), the validateToolContextSupport method has a bug introduced during refactoring. Specifically, the variable handling in the if condition is incorrect, causing an IllegalArgumentException to be thrown erroneously when ToolContext is non-null and its context is non-empty. This behavior worked correctly in version m6, but the refactoring in m7 introduced this issue.

Below is a code comparison:

m6 version:

private void validateToolContextSupport(@Nullable ToolContext toolContext) {
    var isToolContextRequired = toolContext != null && !CollectionUtils.isEmpty(toolContext.getContext());
    var isToolContextAcceptedByMethod = Stream.of(toolMethod.getParameterTypes())
        .anyMatch(type -> ClassUtils.isAssignable(type, ToolContext.class));
    if (isToolContextRequired && !isToolContextAcceptedByMethod) {
        throw new IllegalArgumentException("ToolContext is not supported by the method as an argument");
    }
}

m7 version:

	private void validateToolContextSupport(@Nullable ToolContext toolContext) {
		var isNonEmptyToolContextProvided = toolContext != null && !CollectionUtils.isEmpty(toolContext.getContext());
		var isToolContextAcceptedByMethod = Stream.of(toolMethod.getParameterTypes())
			.anyMatch(type -> ClassUtils.isAssignable(type, ToolContext.class));
		if (isToolContextAcceptedByMethod && !isNonEmptyToolContextProvided) {
			throw new IllegalArgumentException("ToolContext is required by the method as an argument");
		}
	}

Although the code appears identical, in m7, the evaluation of isToolContextAcceptedByMethod seems to misbehave, likely due to incorrect variable scoping or context handling during refactoring, causing the exception to be thrown unexpectedly.

Environment

  • Spring AI version: spring-ai-M7
  • Java version: [Java 17]

Steps to reproduce
Using the Tools tool will cause this issue

Expected behavior
In version m7, the validateToolContextSupport method should behave as it did in m6, throwing an IllegalArgumentException only when ToolContext is non-null, its context is non-empty, and the method’s parameters do not support the ToolContext type. Currently, m7 incorrectly throws the exception, leading to functional issues.

Minimal Complete Reproducible example
Using Tools

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

No branches or pull requests

1 participant