Skip to content

Commit 872b573

Browse files
Mateusz Krawieccopybara-github
authored andcommitted
feat: support non-map return values returned from Function Tools as in https://google.github.io/adk-docs/tools-custom/function-tools/#return-type
PiperOrigin-RevId: 838719814
1 parent e6755a2 commit 872b573

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

core/src/main/java/com/google/adk/tools/FunctionTool.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,15 @@ private Maybe<Map<String, Object>> call(Map<String, Object> args, ToolContext to
238238
data -> OBJECT_MAPPER.convertValue(data, new TypeReference<Map<String, Object>>() {}))
239239
.toMaybe();
240240
} else {
241-
return Maybe.just(
242-
OBJECT_MAPPER.convertValue(result, new TypeReference<Map<String, Object>>() {}));
241+
try {
242+
return Maybe.just(
243+
OBJECT_MAPPER.convertValue(result, new TypeReference<Map<String, Object>>() {}));
244+
} catch (IllegalArgumentException e) {
245+
// Conversion to map failed, in this case we follow
246+
// https://google.github.io/adk-docs/tools-custom/function-tools/#return-type and return
247+
// the { "result": $result }
248+
return Maybe.just(ImmutableMap.of("result", result));
249+
}
243250
}
244251
}
245252

core/src/test/java/com/google/adk/tools/FunctionToolTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,15 @@ public void call_withPojoParamWithFields() throws Exception {
328328
assertThat(result).containsExactly("field1", "abc", "field2", 123);
329329
}
330330

331+
@Test
332+
public void call_withBooleanReturnValue_returnsMapWithResult() throws Exception {
333+
FunctionTool tool = FunctionTool.create(Functions.class, "returnsBoolean");
334+
335+
Map<String, Object> result = tool.runAsync(ImmutableMap.of(), null).blockingGet();
336+
337+
assertThat(result).containsExactly("result", true);
338+
}
339+
331340
@Test
332341
public void call_withPojoParamWithGettersAndSetters() throws Exception {
333342
FunctionTool tool = FunctionTool.create(Functions.class, "pojoParamWithGettersAndSetters");
@@ -894,6 +903,10 @@ public static Single<Map<String, Object>> returnsSingleMap() {
894903
return Single.just(ImmutableMap.of("key", "value"));
895904
}
896905

906+
public static Boolean returnsBoolean() {
907+
return true;
908+
}
909+
897910
public static PojoWithGettersAndSetters returnsPojo() {
898911
PojoWithGettersAndSetters pojo = new PojoWithGettersAndSetters();
899912
pojo.setField1("abc");

0 commit comments

Comments
 (0)