Class ReflectAndRetryToolPlugin
- All Implemented Interfaces:
Plugin
This plugin intercepts tool failures, hands the model structured guidance for reflection and correction, and lets it retry up to a configurable limit. Failure counts are tracked per tool within a scope, so a success with one tool resets that tool's counter without forgiving another's.
A failure is an error a tool emits, such as Single.error, or an error the ReflectAndRetryToolPlugin.ResultErrorExtractor finds in a result. FunctionTool returns an exception thrown by its
method as a {"status": "error"} result, so such failures reach this plugin only through a
result error extractor. Calls to a tool name the agent does not have never reach this plugin.
Port of adk-python's ReflectAndRetryToolPlugin (
plugins/reflect_retry_tool_plugin.py).
Example:
Runner runner =
new InMemoryRunner(
agent,
APP_NAME,
ImmutableList.of(ReflectAndRetryToolPlugin.builder().maxRetries(3).build()));
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilder forReflectAndRetryToolPlugin.static interfaceFinds an error that a tool reported in its result instead of throwing.static enumDefines the lifecycle scope for tracking tool failure counts. -
Field Summary
Fields inherited from class com.google.adk.plugins.BasePlugin
name -
Method Summary
Modifier and TypeMethodDescriptionio.reactivex.rxjava3.core.CompletableafterRunCallback(InvocationContext invocationContext) Drops the ended invocation's failure counts.afterToolCallback(BaseTool tool, Map<String, Object> toolArgs, ToolContext toolContext, Map<String, Object> result) Resets the tool's failure count on success, or routes an error carried inside an otherwise successful result into the retry logic.builder()Returns a builder defaulting to three retries, throwing once they are exceeded, counted per invocation, with no result errors.io.reactivex.rxjava3.core.CompletableonRunErrorCallback(InvocationContext invocationContext, Throwable error) Drops the failed invocation's failure counts.onToolErrorCallback(BaseTool tool, Map<String, Object> toolArgs, ToolContext toolContext, Throwable error) Turns a thrown tool error into reflection guidance for the model.Methods inherited from class com.google.adk.plugins.BasePlugin
getNameMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.google.adk.plugins.Plugin
afterAgentCallback, afterModelCallback, beforeAgentCallback, beforeModelCallback, beforeRunCallback, beforeToolCallback, close, onEventCallback, onModelErrorCallback, onUserMessageCallback
-
Method Details
-
builder
Returns a builder defaulting to three retries, throwing once they are exceeded, counted per invocation, with no result errors. -
afterToolCallback
public io.reactivex.rxjava3.core.Maybe<Map<String,Object>> afterToolCallback(BaseTool tool, Map<String, Object> toolArgs, ToolContext toolContext, Map<String, Object> result) Resets the tool's failure count on success, or routes an error carried inside an otherwise successful result into the retry logic. A response this plugin produced is passed through.- Parameters:
tool- The tool instance that has just been executed.toolArgs- The original arguments that were passed to the tool.toolContext- The context specific to the tool execution.result- The dictionary returned by the tool invocation.- Returns:
- An optional Map to replace the original result from the tool. Returning Empty to use the original result.
-
afterRunCallback
Drops the ended invocation's failure counts.- Parameters:
invocationContext- The context for the entire invocation.
-
onRunErrorCallback
public io.reactivex.rxjava3.core.Completable onRunErrorCallback(InvocationContext invocationContext, Throwable error) Drops the failed invocation's failure counts.- Parameters:
invocationContext- The context for the entire invocation.error- The exception that was raised.
-
onToolErrorCallback
public io.reactivex.rxjava3.core.Maybe<Map<String,Object>> onToolErrorCallback(BaseTool tool, Map<String, Object> toolArgs, ToolContext toolContext, Throwable error) Turns a thrown tool error into reflection guidance for the model.- Parameters:
tool- The tool instance that encountered an error.toolArgs- The arguments that were passed to the tool.toolContext- The context specific to the tool execution.error- The exception that was raised during tool execution.- Returns:
- An optional Map to be used as the tool response instead of propagating the error. Returning Empty to allow the original error to be raised.
-