Class ReflectAndRetryToolPlugin

java.lang.Object
com.google.adk.plugins.BasePlugin
com.google.adk.plugins.ReflectAndRetryToolPlugin
All Implemented Interfaces:
Plugin

@Experimental public final class ReflectAndRetryToolPlugin extends BasePlugin
Provides self-healing error recovery for tool failures.

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()));
 
  • Method Details

    • builder

      public static ReflectAndRetryToolPlugin.Builder 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

      public io.reactivex.rxjava3.core.Completable afterRunCallback(InvocationContext invocationContext)
      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.