From f628dc955520bc53adf520a4193fe9e85634611b Mon Sep 17 00:00:00 2001 From: Michael Vorburger Date: Tue, 7 Oct 2025 21:08:18 +0200 Subject: [PATCH] fix: Allow injecting ObjectMapper in FunctionTool, default to ObjectMapper (re. #473) --- .../com/google/adk/tools/FunctionTool.java | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/core/src/main/java/com/google/adk/tools/FunctionTool.java b/core/src/main/java/com/google/adk/tools/FunctionTool.java index 3037ef40..95be3bf0 100644 --- a/core/src/main/java/com/google/adk/tools/FunctionTool.java +++ b/core/src/main/java/com/google/adk/tools/FunctionTool.java @@ -18,7 +18,7 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; +import com.google.adk.JsonBaseModel; import com.google.adk.agents.InvocationContext; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; @@ -42,13 +42,13 @@ /** FunctionTool implements a customized function calling tool. */ public class FunctionTool extends BaseTool { - private static final ObjectMapper OBJECT_MAPPER = - new ObjectMapper().registerModule(new Jdk8Module()); + private static final Logger logger = LoggerFactory.getLogger(FunctionTool.class); @Nullable private final Object instance; private final Method func; private final FunctionDeclaration funcDeclaration; + private final ObjectMapper objectMapper; public static FunctionTool create(Object instance, Method func) { if (!areParametersAnnotatedWithSchema(func) && wasCompiledWithDefaultParameterNames(func)) { @@ -123,6 +123,11 @@ private static boolean wasCompiledWithDefaultParameterNames(Method func) { } protected FunctionTool(@Nullable Object instance, Method func, boolean isLongRunning) { + this(instance, func, isLongRunning, JsonBaseModel.getMapper()); + } + + protected FunctionTool( + @Nullable Object instance, Method func, boolean isLongRunning, ObjectMapper objectMapper) { super( func.isAnnotationPresent(Annotations.Schema.class) && !func.getAnnotation(Annotations.Schema.class).name().isEmpty() @@ -144,6 +149,7 @@ protected FunctionTool(@Nullable Object instance, Method func, boolean isLongRun this.funcDeclaration = FunctionCallingUtils.buildFunctionDeclaration( this.func, ImmutableList.of("toolContext", "inputStream")); + this.objectMapper = objectMapper; } @Override @@ -225,7 +231,7 @@ private Maybe> call(Map args, ToolContext to continue; } } else if (argValue instanceof Map) { - arguments[i] = OBJECT_MAPPER.convertValue(argValue, paramType); + arguments[i] = objectMapper.convertValue(argValue, paramType); continue; } arguments[i] = castValue(argValue, paramType); @@ -236,16 +242,14 @@ private Maybe> call(Map args, ToolContext to } else if (result instanceof Maybe) { return ((Maybe) result) .map( - data -> - OBJECT_MAPPER.convertValue(data, new TypeReference>() {})); + data -> objectMapper.convertValue(data, new TypeReference>() {})); } else if (result instanceof Single) { return ((Single) result) - .map( - data -> OBJECT_MAPPER.convertValue(data, new TypeReference>() {})) + .map(data -> objectMapper.convertValue(data, new TypeReference>() {})) .toMaybe(); } else { return Maybe.just( - OBJECT_MAPPER.convertValue(result, new TypeReference>() {})); + objectMapper.convertValue(result, new TypeReference>() {})); } } @@ -291,7 +295,7 @@ public Flowable> callLive( continue; } } else if (argValue instanceof Map) { - arguments[i] = OBJECT_MAPPER.convertValue(argValue, paramType); + arguments[i] = objectMapper.convertValue(argValue, paramType); continue; } arguments[i] = castValue(argValue, paramType); @@ -305,7 +309,7 @@ public Flowable> callLive( } } - private static List createList(List values, Class type) { + private List createList(List values, Class type) { List list = new ArrayList<>(); // List of parameterized type is not supported. if (type == null) { @@ -321,13 +325,13 @@ private static List createList(List values, Class type) { || cls == String.class) { list.add(castValue(value, cls)); } else { - list.add(OBJECT_MAPPER.convertValue(value, type)); + list.add(objectMapper.convertValue(value, type)); } } return list; } - private static Object castValue(Object value, Class type) { + private Object castValue(Object value, Class type) { if (type.equals(Integer.class) || type.equals(int.class)) { if (value instanceof Integer) { return value; @@ -372,6 +376,6 @@ private static Object castValue(Object value, Class type) { return value; } } - return OBJECT_MAPPER.convertValue(value, type); + return objectMapper.convertValue(value, type); } }