Consider the current implementation of validateToolContextSupport compared from 1.0.0 and 1.0.0-M6
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) {
throw new IllegalArgumentException("ToolContext is required by the method as an argument");
}
}
M6
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");
}
}
-
Both have the same serious bug also refered in #3355 , it seems that the writer does not know the semantics of ClassUtils.isAssignable(left, right) => right is assinable to left, so left is the "super" type. All this code has the arguments in incorrect order.
-
More, in the case of a toolContext not being provided, the change in the validation logic now make it fail due to error 1 M6 : No context => isToolContextRequired = false
=> isToolContextRequired && !isToolContextAcceptedByMethod = false => no exception 1.0.0 : No context => isNonEmptyToolContextProvided = false + isNonEmptyToolContextProvided = true (because of error 1) => isToolContextAcceptedByMethod && !isNonEmptyToolContextProvided = true => exception is thrown !
Comment From: sunyuhan1998
Hi @jcgouveia , I checked the code and I agree with you and pushed a PR to try to fix it :https://github.com/spring-projects/spring-ai/pull/3478, can you help to review this PR?thanks.