Skip to content
Open
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
127 changes: 110 additions & 17 deletions core/src/main/java/com/google/adk/models/Gemini.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.primitives.Ints;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.genai.Client;
import com.google.genai.ResponseStream;
Expand All @@ -46,6 +47,8 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import okhttp3.OkHttpClient;
import org.jspecify.annotations.Nullable;
import org.slf4j.Logger;
Expand Down Expand Up @@ -440,6 +443,25 @@ protected CompletableFuture<GeminiLiveTransport> connectLiveTransport(
}

private static final class StreamingResponseAggregator {
/** The largest list index a streamed argument path may set, as in ADK Python. */
private static final int MAX_ARRAY_INDEX = 10_000;

/**
* A JSONPath segment, as in ADK Python: a list index such as {@code [0]}, a quoted key such as
* {@code ['a.b']} or {@code ["a.b"]}, or a key with an optional dot. Keys may hold escapes. The
* key loops are possessive: a greedy one recurses per character and overflows the stack.
*/
private static final Pattern JSON_PATH_SEGMENT =
Pattern.compile(
"\\[\\s*(\\d+)\\s*]"
+ "|\\['((?:[^'\\\\]|\\\\.)*+)'\\]"
+ "|\\[\"((?:[^\"\\\\]|\\\\.)*+)\"\\]"
+ "|\\.?((?:[^.\\[\\]\\\\]|\\\\.)++)");

/** A backslash escape in a JSONPath key: {@code \\uXXXX} or one escaped character. */
private static final Pattern JSON_PATH_ESCAPE =
Pattern.compile("\\\\u([0-9a-fA-F]{4})|\\\\(.)");

/** A whole number whose magnitude is at most 2^53 - 1 has an exact double; others may not. */
private static final double MAX_EXACT_DOUBLE_INTEGER = 9007199254740991d;

Expand Down Expand Up @@ -706,9 +728,15 @@ private static Number wholeNumberAsInteger(double value) {
*/
private @Nullable Object getValueByJsonPath(String jsonPath) {
Object current = currentFcArgs;
for (String key : splitJsonPath(jsonPath)) {
if (current instanceof Map<?, ?> map && map.containsKey(key)) {
for (Object segment : parseJsonPath(jsonPath)) {
if (segment instanceof String key
&& current instanceof Map<?, ?> map
&& map.containsKey(key)) {
current = map.get(key);
} else if (segment instanceof Integer index
&& current instanceof List<?> list
&& index < list.size()) {
current = list.get(index);
} else {
return null;
}
Expand All @@ -717,27 +745,92 @@ private static Number wholeNumberAsInteger(double value) {
}

/**
* Sets {@code value} at {@code jsonPath} in {@link #currentFcArgs}, creating maps as needed.
* Sets {@code value} at {@code jsonPath} in {@link #currentFcArgs}, creating maps and lists as
* needed and padding lists with nulls, as ADK Python's {@code _set_value_by_json_path} does. A
* path that runs into a value of the wrong type or an index above 10,000 is ignored.
*/
@SuppressWarnings("unchecked")
private void setValueByJsonPath(String jsonPath, Object value) {
String[] keys = splitJsonPath(jsonPath);
Map<String, Object> current = currentFcArgs;
for (int i = 0; i < keys.length - 1; i++) {
Object next = current.get(keys[i]);
if (!(next instanceof Map)) {
next = new LinkedHashMap<>();
current.put(keys[i], next);
private void setValueByJsonPath(String jsonPath, @Nullable Object value) {
List<Object> segments = parseJsonPath(jsonPath);
Object current = currentFcArgs;
for (int i = 0; i < segments.size(); i++) {
boolean last = i == segments.size() - 1;
Object child =
last
? value
: segments.get(i + 1) instanceof Integer
? new ArrayList<>()
: new LinkedHashMap<>();
if (segments.get(i) instanceof String key && current instanceof Map<?, ?>) {
Map<String, Object> map = (Map<String, Object>) current;
if (last || !map.containsKey(key)) {
map.put(key, child);
}
current = map.get(key);
} else if (segments.get(i) instanceof Integer index
&& current instanceof List<?>
&& index <= MAX_ARRAY_INDEX) {
List<Object> list = (List<Object>) current;
while (list.size() <= index) {
list.add(null);
}
if (last || list.get(index) == null) {
list.set(index, child);
}
current = list.get(index);
} else {
return;
}
current = (Map<String, Object>) next;
}
current.put(keys[keys.length - 1], value);
}

/** Splits a JSONPath such as {@code "$.location.city"} into its component keys. */
private static String[] splitJsonPath(String jsonPath) {
String path = jsonPath.startsWith("$.") ? jsonPath.substring(2) : jsonPath;
return path.split("\\.");
/** Splits a JSONPath such as {@code "$.items[0].name"} into map keys and list indices. */
private static List<Object> parseJsonPath(String jsonPath) {
String path =
jsonPath.startsWith("$.")
? jsonPath.substring(2)
: jsonPath.startsWith("$") ? jsonPath.substring(1) : jsonPath;
Matcher matcher = JSON_PATH_SEGMENT.matcher(path);
List<Object> segments = new ArrayList<>();
while (matcher.find()) {
if (matcher.group(1) != null) {
Integer index = Ints.tryParse(matcher.group(1));
// An index too large for an int is above MAX_ARRAY_INDEX anyway.
segments.add(index == null ? Integer.MAX_VALUE : index);
} else {
String key =
matcher.group(2) != null
? matcher.group(2)
: matcher.group(3) != null ? matcher.group(3) : matcher.group(4);
segments.add(unescapeJsonPathKey(key));
}
}
return segments;
}

/** Resolves backslash escapes in a JSONPath key the way ADK Python does. */
private static String unescapeJsonPathKey(String key) {
Matcher matcher = JSON_PATH_ESCAPE.matcher(key);
StringBuilder result = new StringBuilder();
while (matcher.find()) {
String replacement;
if (matcher.group(1) != null) {
replacement = String.valueOf((char) Integer.parseInt(matcher.group(1), 16));
} else {
replacement =
switch (matcher.group(2)) {
case "n" -> "\n";
case "r" -> "\r";
case "t" -> "\t";
case "b" -> "\b";
case "f" -> "\f";
default -> matcher.group(2);
};
}
matcher.appendReplacement(result, Matcher.quoteReplacement(replacement));
}
matcher.appendTail(result);
return result.toString();
}

/**
Expand Down
76 changes: 57 additions & 19 deletions core/src/main/java/com/google/adk/tools/SetModelResponseTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import com.google.common.collect.ImmutableMap;
import com.google.genai.types.FunctionDeclaration;
import com.google.genai.types.Schema;
import com.google.genai.types.Type;
import io.reactivex.rxjava3.core.Single;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -42,15 +44,46 @@ public class SetModelResponseTool extends BaseTool {
// Marker after which SchemaUtils appends the schema dump; stripped from feedback best-effort.
private static final String OUTPUT_SCHEMA_DUMP_MARKER = " does not match agent output schema: ";

private final Schema outputSchema;
// Function parameters must be an object, so a non-object output schema goes into this field.
private static final String RESPONSE_PARAMETER = "response";

private final boolean wrapsOutputSchema;
private final Schema parameters;

public SetModelResponseTool(Schema outputSchema) {
super(
NAME,
"Set your final response using the required output schema. "
+ "After using any other tools needed to complete the task, always call"
+ " set_model_response with your final answer in the specified schema format.");
this.outputSchema = outputSchema;
this.wrapsOutputSchema = !describesObject(outputSchema);
if (wrapsOutputSchema) {
this.parameters =
Schema.builder()
.type(Type.Known.OBJECT)
.properties(ImmutableMap.of(RESPONSE_PARAMETER, outputSchema))
.required(ImmutableList.of(RESPONSE_PARAMETER))
.build();
} else {
this.parameters =
outputSchema.type().isPresent()
? outputSchema
: outputSchema.toBuilder().type(Type.Known.OBJECT).build();
}
}

/**
* Returns whether {@code schema} is an object: typed {@code OBJECT}, or untyped and not only
* {@code items}.
*/
private static boolean describesObject(Schema schema) {
return schema
.type()
.map(type -> type.knownEnum() == Type.Known.OBJECT)
.orElse(
schema.items().isEmpty()
|| schema.properties().isPresent()
|| schema.required().isPresent());
}

@Override
Expand All @@ -59,30 +92,35 @@ public Optional<FunctionDeclaration> declaration() {
FunctionDeclaration.builder()
.name(name())
.description(description())
.parameters(outputSchema)
.parameters(parameters)
.build());
}

@Override
public Single<Map<String, Object>> runAsync(Map<String, Object> args, ToolContext toolContext) {
// Record validated responses on the event actions; return validation feedback so the model can
// retry.
return Single.fromCallable(
() -> {
try {
SchemaUtils.validateMapOnSchema(args, outputSchema, /* isInput= */ false);
} catch (IllegalArgumentException e) {
return ImmutableMap.of(
"error",
"Validation Error found:\n"
+ sanitizeValidationMessage(e.getMessage())
+ "\nRecall the set_model_response function correctly, fix the errors, and"
+ " call it again with all required fields using the correct types.");
}
Map<String, Object> validatedResponse = excludeNullFields(args, outputSchema);
toolContext.actions().setSetModelResponse(validatedResponse);
return validatedResponse;
});
return Single.fromCallable(() -> {
try {
SchemaUtils.validateMapOnSchema(args, parameters, /* isInput= */ false);
} catch (IllegalArgumentException e) {
return ImmutableMap.of(
"error",
"Validation Error found:\n"
+ sanitizeValidationMessage(e.getMessage())
+ "\nRecall the set_model_response function correctly, fix the errors, and"
+ " call it again with all required fields using the correct types.");
}
Map<String, Object> validatedResponse = excludeNullFields(args, parameters);
if (wrapsOutputSchema) {
Object response = validatedResponse.get(RESPONSE_PARAMETER);
toolContext.actions().setSetModelResponse(response);
// Like a function tool's non-map result; the value may be null.
return Collections.singletonMap("result", response);
}
toolContext.actions().setSetModelResponse(validatedResponse);
return validatedResponse;
});
}

/**
Expand Down
Loading
Loading