Skip to content

Fix string return conversion in DefaultToolCallResultConverter #3756

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 @@ -22,6 +22,7 @@
import java.lang.reflect.Type;
import java.util.Base64;
import java.util.Map;
import java.util.Optional;

import javax.imageio.ImageIO;

Expand All @@ -35,6 +36,7 @@
* A default implementation of {@link ToolCallResultConverter}.
*
* @author Thomas Vitale
* @author Jemin Huh
* @since 1.0.0
*/
public final class DefaultToolCallResultConverter implements ToolCallResultConverter {
Expand All @@ -45,7 +47,11 @@ public final class DefaultToolCallResultConverter implements ToolCallResultConve
public String convert(@Nullable Object result, @Nullable Type returnType) {
if (returnType == Void.TYPE) {
logger.debug("The tool has no return type. Converting to conventional response.");
return JsonParser.toJson("Done");
return "Done";
}
if (returnType instanceof Class<?> cls && cls.equals(String.class)) {
logger.debug("Tool return type is String. Returning result as is.");
return Optional.ofNullable((String) result).orElse("null");
}
if (result instanceof RenderedImage) {
final var buf = new ByteArrayOutputStream(1024 * 4);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
* Unit tests for {@link DefaultToolCallResultConverter}.
*
* @author Thomas Vitale
* @author Jemin Huh
*/
class DefaultToolCallResultConverterTests {

Expand All @@ -52,13 +53,13 @@ void convertWithNullReturnTypeShouldReturn() {
@Test
void convertVoidReturnTypeShouldReturnDoneJson() {
String result = this.converter.convert(null, void.class);
assertThat(result).isEqualTo("\"Done\"");
assertThat(result).isEqualTo("Done");
}

@Test
void convertStringReturnTypeShouldReturnJson() {
String result = this.converter.convert("test", String.class);
assertThat(result).isEqualTo("\"test\"");
assertThat(result).isEqualTo("test");
}

@Test
Expand Down