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 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 BasePlugin
getNameMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Plugin
afterAgentCallback, afterModelCallback, beforeAgentCallback, beforeModelCallback, beforeRunCallback, beforeToolCallback, close, onEventCallback, onModelErrorCallback, onUserMessageCallbackModifier and TypeMethodDescriptiondefault io.reactivex.rxjava3.core.Maybe<com.google.genai.types.Content> afterAgentCallback(BaseAgent agent, CallbackContext callbackContext) Callback executed after an agent's primary logic has completed.default io.reactivex.rxjava3.core.Maybe<LlmResponse> afterModelCallback(CallbackContext callbackContext, LlmResponse llmResponse) Callback executed after a response is received from the model.default io.reactivex.rxjava3.core.Maybe<com.google.genai.types.Content> beforeAgentCallback(BaseAgent agent, CallbackContext callbackContext) Callback executed before an agent's primary logic is invoked.default io.reactivex.rxjava3.core.Maybe<LlmResponse> beforeModelCallback(CallbackContext callbackContext, LlmRequest.Builder llmRequest) Callback executed before a request is sent to the model.default io.reactivex.rxjava3.core.Maybe<com.google.genai.types.Content> beforeRunCallback(InvocationContext invocationContext) Callback executed before the ADK runner runs.beforeToolCallback(BaseTool tool, Map<String, Object> toolArgs, ToolContext toolContext) Callback executed before a tool is called.default io.reactivex.rxjava3.core.Completableclose()Method executed when the runner is closed.default io.reactivex.rxjava3.core.Maybe<Event> onEventCallback(InvocationContext invocationContext, Event event) Callback executed after an event is yielded from runner.default io.reactivex.rxjava3.core.Maybe<LlmResponse> onModelErrorCallback(CallbackContext callbackContext, LlmRequest.Builder llmRequest, Throwable error) Callback executed when a model call encounters an error.default io.reactivex.rxjava3.core.Maybe<com.google.genai.types.Content> onUserMessageCallback(InvocationContext invocationContext, com.google.genai.types.Content userMessage) Callback executed when a user message is received before an invocation starts.
-
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.
-