Skip to content

Fixed GH-3466: Fixed the logic error in the validateToolContextSupport method of MethodToolCallback #3478

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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ public String call(String toolInput, @Nullable ToolContext toolContext) {
private void validateToolContextSupport(@Nullable ToolContext toolContext) {
var isNonEmptyToolContextProvided = toolContext != null && !CollectionUtils.isEmpty(toolContext.getContext());
var isToolContextAcceptedByMethod = Stream.of(this.toolMethod.getParameterTypes())
.anyMatch(type -> ClassUtils.isAssignable(type, ToolContext.class));
if (isToolContextAcceptedByMethod && !isNonEmptyToolContextProvided) {
.anyMatch(type -> ClassUtils.isAssignable(ToolContext.class, type));
if (isNonEmptyToolContextProvided && !isToolContextAcceptedByMethod) {
throw new IllegalArgumentException("ToolContext is required by the method as an argument");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@

import org.junit.jupiter.api.Test;

import org.springframework.ai.chat.model.ToolContext;
import org.springframework.ai.tool.definition.DefaultToolDefinition;
import org.springframework.ai.tool.definition.ToolDefinition;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

/**
* Tests for {@link MethodToolCallback} with generic types.
Expand Down Expand Up @@ -137,6 +139,76 @@ void testNestedGenericType() throws Exception {
assertThat(result).isEqualTo("2 maps processed: [{a=1, b=2}, {c=3, d=4}]");
}

@Test
void testToolContextType() throws Exception {
// Create a test object with a method that takes a List<Map<String, Integer>>
TestGenericClass testObject = new TestGenericClass();
Method method = TestGenericClass.class.getMethod("processStringListInToolContext", ToolContext.class);

// Create a tool definition
ToolDefinition toolDefinition = DefaultToolDefinition.builder()
.name("processToolContext")
.description("Process tool context")
.inputSchema("{}")
.build();

// Create a MethodToolCallback
MethodToolCallback callback = MethodToolCallback.builder()
.toolDefinition(toolDefinition)
.toolMethod(method)
.toolObject(testObject)
.build();

// Create an empty JSON input
String toolInput = """
{}
""";

// Create a toolContext
ToolContext toolContext = new ToolContext(Map.of("foo", "bar"));

// Call the tool
String result = callback.call(toolInput, toolContext);

// Verify the result
assertThat(result).isEqualTo("1 entries processed {foo=bar}");
}

@Test
void testToolContextTypeWithNonToolContextArgs() throws Exception {
// Create a test object with a method that takes a List<String>
TestGenericClass testObject = new TestGenericClass();
Method method = TestGenericClass.class.getMethod("processStringList", List.class);

// Create a tool definition
ToolDefinition toolDefinition = DefaultToolDefinition.builder()
.name("processStringList")
.description("Process a list of strings")
.inputSchema("{}")
.build();

// Create a MethodToolCallback
MethodToolCallback callback = MethodToolCallback.builder()
.toolDefinition(toolDefinition)
.toolMethod(method)
.toolObject(testObject)
.build();

// Create a JSON input with a list of strings
String toolInput = """
{
"strings": ["one", "two", "three"]
}
""";

// Create a toolContext
ToolContext toolContext = new ToolContext(Map.of("foo", "bar"));

// Call the tool and verify
assertThatThrownBy(() -> callback.call(toolInput, toolContext)).isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("ToolContext is required by the method as an argument");
}

/**
* Test class with methods that use generic types.
*/
Expand All @@ -154,6 +226,11 @@ public String processListOfMaps(List<Map<String, Integer>> listOfMaps) {
return listOfMaps.size() + " maps processed: " + listOfMaps;
}

public String processStringListInToolContext(ToolContext toolContext) {
Map<String, Object> context = toolContext.getContext();
return context.size() + " entries processed " + context;
}

}

}