Skip to content
Closed
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 @@ -340,7 +340,10 @@ private static Struct jsonToStruct(String json) {

Struct.Builder structBuilder = Struct.newBuilder();

if (rootNode.isArray()) {
if (rootNode.isTextual()) {
structBuilder.putFields("result", Value.newBuilder().setStringValue(json).build());
}
else if (rootNode.isArray()) {
// Handle JSON array
List<Value> values = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,38 @@ void jsonArrayToolCallingTest() {

}

@Test
void jsonTextToolCallingTest() {
// Test for the improved jsonToStruct method that handles JSON texts in tool
// calling

ToolCallingManager toolCallingManager = ToolCallingManager.builder()
.observationRegistry(ObservationRegistry.NOOP)
.build();

VertexAiGeminiChatModel chatModelWithTools = VertexAiGeminiChatModel.builder()
.vertexAI(vertexAiApi())
.toolCallingManager(toolCallingManager)
.defaultOptions(VertexAiGeminiChatOptions.builder()
.model(VertexAiGeminiChatModel.ChatModel.GEMINI_2_0_FLASH)
.temperature(0.1)
.build())
.build();

ChatClient chatClient = ChatClient.builder(chatModelWithTools).build();

// Create a prompt that will trigger the tool call with a specific request that
// should invoke the tool
String response = chatClient.prompt()
.tools(new CurrentTimeTools())
.user("Get the current time. Make sure to use the tool to get this information.")
.call()
.content();

assertThat(response).isNotEmpty();
assertThat(response).contains("2025-05-08T10:10:10+02:00[Europe/Berlin]");
}

/**
* Tool class that returns a JSON array to test the jsonToStruct method's ability to
* handle JSON arrays. This specifically tests the PR changes that improve the
Expand All @@ -378,6 +410,21 @@ public List<Map<String, String>> getScientists() {

}

/**
* Tool class that returns a String to test the jsonToStruct method's ability to
* handle JSON texts. This specifically tests the PR changes that improve the
* jsonToStruct method to handle JSON texts in addition to JSON objects and JSON
* arrays.
*/
public static class CurrentTimeTools {

@Tool(description = "Get the current date and time in the user's timezone")
String getCurrentDateTime() {
return "2025-05-08T10:10:10+02:00[Europe/Berlin]";
}

}

record ActorsFilmsRecord(String actor, List<String> movies) {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ 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 "Done";
return JsonParser.toJson("Done");
}
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 @@ -50,9 +50,9 @@ void convertWithNullReturnTypeShouldReturn() {
}

@Test
void convertVoidReturnTypeShouldReturnDone() {
void convertVoidReturnTypeShouldReturnDoneJson() {
String result = this.converter.convert(null, void.class);
assertThat(result).isEqualTo("Done");
assertThat(result).isEqualTo("\"Done\"");
}

@Test
Expand Down