Skip to content

Commit 76c7018

Browse files
sunyuhan1998ilayaperumalg
authored andcommitted
fix: validateToolContextSupport method to use right order in ClassUtils.isAssignable
Signed-off-by: Sun Yuhan <[email protected]>
1 parent 66324a6 commit 76c7018

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

spring-ai-model/src/main/java/org/springframework/ai/tool/method/MethodToolCallback.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public String call(String toolInput, @Nullable ToolContext toolContext) {
118118
private void validateToolContextSupport(@Nullable ToolContext toolContext) {
119119
var isNonEmptyToolContextProvided = toolContext != null && !CollectionUtils.isEmpty(toolContext.getContext());
120120
var isToolContextAcceptedByMethod = Stream.of(this.toolMethod.getParameterTypes())
121-
.anyMatch(type -> ClassUtils.isAssignable(type, ToolContext.class));
121+
.anyMatch(type -> ClassUtils.isAssignable(ToolContext.class, type));
122122
if (isToolContextAcceptedByMethod && !isNonEmptyToolContextProvided) {
123123
throw new IllegalArgumentException("ToolContext is required by the method as an argument");
124124
}

spring-ai-model/src/test/java/org/springframework/ai/tool/method/MethodToolCallbackGenericTypesTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import org.junit.jupiter.api.Test;
2424

25+
import org.springframework.ai.chat.model.ToolContext;
2526
import org.springframework.ai.tool.definition.DefaultToolDefinition;
2627
import org.springframework.ai.tool.definition.ToolDefinition;
2728

@@ -137,6 +138,41 @@ void testNestedGenericType() throws Exception {
137138
assertThat(result).isEqualTo("2 maps processed: [{a=1, b=2}, {c=3, d=4}]");
138139
}
139140

141+
@Test
142+
void testToolContextType() throws Exception {
143+
// Create a test object with a method that takes a List<Map<String, Integer>>
144+
TestGenericClass testObject = new TestGenericClass();
145+
Method method = TestGenericClass.class.getMethod("processStringListInToolContext", ToolContext.class);
146+
147+
// Create a tool definition
148+
ToolDefinition toolDefinition = DefaultToolDefinition.builder()
149+
.name("processToolContext")
150+
.description("Process tool context")
151+
.inputSchema("{}")
152+
.build();
153+
154+
// Create a MethodToolCallback
155+
MethodToolCallback callback = MethodToolCallback.builder()
156+
.toolDefinition(toolDefinition)
157+
.toolMethod(method)
158+
.toolObject(testObject)
159+
.build();
160+
161+
// Create an empty JSON input
162+
String toolInput = """
163+
{}
164+
""";
165+
166+
// Create a toolContext
167+
ToolContext toolContext = new ToolContext(Map.of("foo", "bar"));
168+
169+
// Call the tool
170+
String result = callback.call(toolInput, toolContext);
171+
172+
// Verify the result
173+
assertThat(result).isEqualTo("1 entries processed {foo=bar}");
174+
}
175+
140176
/**
141177
* Test class with methods that use generic types.
142178
*/
@@ -154,6 +190,11 @@ public String processListOfMaps(List<Map<String, Integer>> listOfMaps) {
154190
return listOfMaps.size() + " maps processed: " + listOfMaps;
155191
}
156192

193+
public String processStringListInToolContext(ToolContext toolContext) {
194+
Map<String, Object> context = toolContext.getContext();
195+
return context.size() + " entries processed " + context;
196+
}
197+
157198
}
158199

159200
}

0 commit comments

Comments
 (0)