-
Notifications
You must be signed in to change notification settings - Fork 152
fix: Allow injecting ObjectMapper in FunctionTool, default to ObjectMapper (re. #473) #487
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<Map<String, Object>> call(Map<String, Object> 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<Map<String, Object>> call(Map<String, Object> args, ToolContext to | |
} else if (result instanceof Maybe) { | ||
return ((Maybe<?>) result) | ||
.map( | ||
data -> | ||
OBJECT_MAPPER.convertValue(data, new TypeReference<Map<String, Object>>() {})); | ||
data -> objectMapper.convertValue(data, new TypeReference<Map<String, Object>>() {})); | ||
} else if (result instanceof Single) { | ||
return ((Single<?>) result) | ||
.map( | ||
data -> OBJECT_MAPPER.convertValue(data, new TypeReference<Map<String, Object>>() {})) | ||
.map(data -> objectMapper.convertValue(data, new TypeReference<Map<String, Object>>() {})) | ||
.toMaybe(); | ||
} else { | ||
return Maybe.just( | ||
OBJECT_MAPPER.convertValue(result, new TypeReference<Map<String, Object>>() {})); | ||
objectMapper.convertValue(result, new TypeReference<Map<String, Object>>() {})); | ||
} | ||
} | ||
|
||
|
@@ -291,7 +295,7 @@ public Flowable<Map<String, Object>> 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<Map<String, Object>> callLive( | |
} | ||
} | ||
|
||
private static List<Object> createList(List<Object> values, Class<?> type) { | ||
private List<Object> createList(List<Object> values, Class<?> type) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ==> #489 |
||
List<Object> list = new ArrayList<>(); | ||
// List of parameterized type is not supported. | ||
if (type == null) { | ||
|
@@ -321,13 +325,13 @@ private static List<Object> createList(List<Object> 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); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While the runtime argument conversion now uses the injected
objectMapper
, the function declaration schema is built usingFunctionCallingUtils
, which has its own static, non-configurableObjectMapper
. This can lead to inconsistencies if the injectedobjectMapper
has custom serializers, as the generated schema will not reflect them. This could cause mismatches between what the LLM expects and what the tool can handle at runtime. To ensure consistency,FunctionCallingUtils
should also be updated to use the sameObjectMapper
instance.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TBD...