Skip to content

Commit cb49092

Browse files
google-genai-botcopybara-github
authored andcommitted
feat: Enable FunctionTools to be created for methods that return a Single.
PiperOrigin-RevId: 775889299
1 parent 94cadce commit cb49092

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ static FunctionDeclaration buildFunctionDeclaration(Method func, List<String> ig
7373
if (returnType instanceof ParameterizedType) {
7474
ParameterizedType parameterizedReturnType = (ParameterizedType) returnType;
7575
String returnTypeName = ((Class<?>) parameterizedReturnType.getRawType()).getName();
76-
if (returnTypeName.equals("io.reactivex.rxjava3.core.Maybe")) {
76+
if (returnTypeName.equals("io.reactivex.rxjava3.core.Maybe")
77+
|| returnTypeName.equals("io.reactivex.rxjava3.core.Single")) {
7778
returnType = parameterizedReturnType.getActualTypeArguments()[0];
7879
if (returnType instanceof ParameterizedType) {
7980
ParameterizedType maybeParameterizedType = (ParameterizedType) returnType;
@@ -86,7 +87,8 @@ static FunctionDeclaration buildFunctionDeclaration(Method func, List<String> ig
8687
}
8788
}
8889
throw new IllegalArgumentException(
89-
"Return type should be Map or Maybe<Map>, but it was " + realReturnType.getTypeName());
90+
"Return type should be Map or Maybe<Map> or Single<Map>, but it was "
91+
+ realReturnType.getTypeName());
9092
}
9193
return builder.build();
9294
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ private Maybe<Map<String, Object>> call(Map<String, Object> args, ToolContext to
197197
return Maybe.empty();
198198
} else if (result instanceof Maybe) {
199199
return (Maybe<Map<String, Object>>) result;
200+
} else if (result instanceof Single) {
201+
return ((Single<Map<String, Object>>) result).toMaybe();
200202
} else {
201203
return Maybe.just((Map<String, Object>) result);
202204
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.google.genai.types.FunctionDeclaration;
2727
import com.google.genai.types.Schema;
2828
import io.reactivex.rxjava3.core.Maybe;
29+
import io.reactivex.rxjava3.core.Single;
2930
import java.lang.reflect.Method;
3031
import java.util.HashMap;
3132
import java.util.List;
@@ -343,6 +344,24 @@ public void create_withMaybeStringReturnType() {
343344
() -> FunctionTool.create(Functions.class, "returnsMaybeString"));
344345
}
345346

347+
@Test
348+
public void create_withSingleMapReturnType() {
349+
FunctionTool tool = FunctionTool.create(Functions.class, "returnsSingleMap");
350+
351+
assertThat(tool).isNotNull();
352+
assertThat(tool.declaration().get().response())
353+
.hasValue(Schema.builder().type("OBJECT").build());
354+
}
355+
356+
@Test
357+
public void call_withSingleMapReturnType() throws Exception {
358+
FunctionTool tool = FunctionTool.create(Functions.class, "returnsSingleMap");
359+
360+
Map<String, Object> result = tool.runAsync(new HashMap<>(), null).blockingGet();
361+
362+
assertThat(result).containsExactly("key", "value");
363+
}
364+
346365
@Test
347366
public void call_nonStaticWithAllSupportedParameterTypes() throws Exception {
348367
Functions functions = new Functions();
@@ -463,6 +482,10 @@ public static Maybe<String> returnsMaybeString() {
463482
return Maybe.just("not supported");
464483
}
465484

485+
public static Single<Map<String, Object>> returnsSingleMap() {
486+
return Single.just(ImmutableMap.of("key", "value"));
487+
}
488+
466489
public void nonStaticVoidReturnWithoutSchema() {}
467490

468491
public ImmutableMap<String, Object> nonStaticReturnAllSupportedParametersAsMap(

0 commit comments

Comments
 (0)