Conversation
Signed-off-by: Cui-xf <cui_xf293@qq.com>
| final var imgB64 = Base64.getEncoder().encodeToString(buf.toByteArray()); | ||
| return JsonParser.toJson(Map.of("mimeType", "image/png", "data", imgB64)); | ||
| } | ||
| else if (result instanceof String stringResult) { |
There was a problem hiding this comment.
the current assumption is that if is not a rendered image, we assume it is json (also a string) in this current PR, the json conversion in the else statement will never be accessed. Can you explain more of the surrounding use case that motivated you to make this change? adding to DefaultToolCallResultConverterTests?
There was a problem hiding this comment.
Also wondering why JsonParser.toJson(result); doesn't use Type returnType
There was a problem hiding this comment.
the current assumption is that if is not a rendered image, we assume it is json (also a string) in this current PR, the json conversion in the else statement will never be accessed. Can you explain more of the surrounding use case that motivated you to make this change? adding to
DefaultToolCallResultConverterTests?
Hey, thanks for the review!
I noticed that when I use the org.springframework.ai.tool.annotation.Tool annotation and the method returns a String, the result gets escaped (see screenshot below). I’m not really sure if this is the expected behavior, or if there’s any doc about it.
If this is actually, I will to close the PR.
Thanks!
|
@Cui-xf this is an interesting case: Currently the Here is an example how the DefaultToolCallResultConverter behvaves: public class Main {
private static final ObjectMapper OBJECT_MAPPER = JsonMapper.builder().build();
public static void main(String[] args) {
var value1 = "Foo Bar";
System.out.println("Value 1: " + value1);
System.out.println(String.format("- raw value: %s, is JSON: %s", value1, isValidJSONJackson(value1)));
System.out.println(String.format("- toJson(value): %s, is JSON: %s", toJson(value1),
isValidJSONJackson(toJson(value1))));
var value2 = "{\"hello\": \"world\"}";
System.out.println("Value 2: " + value2);
System.out.println(String.format("- raw value: %s, is JSON? %s", value1, isValidJSONJackson(value2)));
System.out.println(String.format("- toJson(value): %s, is JSON? %s", toJson(value2),
isValidJSONJackson(toJson(value2))));
}
public static boolean isValidJSONJackson(String jsonString) {
try {
OBJECT_MAPPER.readTree(jsonString);
return true;
} catch (Exception e) {
return false;
}
}
public static String toJson(@Nullable Object object) {
try {
return OBJECT_MAPPER.writeValueAsString(object);
}
catch (JsonProcessingException ex) {
throw new IllegalStateException("Conversion from Object to JSON failed", ex);
}
}
}Would generate: Before we try to change the default behavior perhaps you can try to implement a custom ToolCallResultConverter implementation? |
|
@Cui-xf Closing this PR as the custom ToolResultConverter is the potential way to fix this issue. Thanks for the contribution! |
Signed-off-by: Cui-xf cui_xf293@qq.com
Thank you for taking time to contribute this pull request!
You might have already read the [contributor guide][1], but as a reminder, please make sure to:
mainbranch and squash your commits