GeezerVison is showing adk.dev — ReflectAndRetryModelPlugin | ADK for TypeScript: API Reference - v2.5.0
- ReflectAndRetryModelPlugin
Class ReflectAndRetryModelPlugin
Provides self-healing, concurrent-safe error recovery for model failures.
This plugin intercepts model failures (such as MALFORMED_FUNCTION_CALL),
provides structured guidance to the LLM for reflection and correction, and
retries the turn up to a configurable limit.
Exampleimport {ReflectAndRetryModelPlugin, TrackingScope} from '@google/adk';
const modelRetryPlugin = new ReflectAndRetryModelPlugin({ maxRetries: 3, trackingScope: TrackingScope.INVOCATION, });
Constructors
constructor
-
Returns ReflectAndRetryModelPlugin
Properties
ReadonlymaxRetries
maxRetries: number
Readonlyname
name: string
ReadonlyonModelErrors
onModelErrors: FinishReason[]
ReadonlythrowExceptionIfRetryExceeded
throwExceptionIfRetryExceeded: boolean
Methods
adkHandleModelError
-
adkHandleModelError( __namedParameters: { errorDetails?: string; errorType?: string; finishReason?: string; responseType?: string; retryCount?: number; },): { reflection_guidance: string }
Internal framework reflection tool handler.
Parameters
- __namedParameters: {
errorDetails?: string; errorType?: string; finishReason?: string; responseType?: string; retryCount?: number; }
Returns { reflection_guidance: string }
afterAgentCallback
-
afterAgentCallback( _params: { agent: BaseAgent; callbackContext: Context },): Promise<Content | undefined>
Callback executed after an agent's primary logic has completed.
This callback can be used to inspect, log, or append an additional event
after the agent's execution completes.
Parameters
- _params: { agent: BaseAgent; callbackContext: Context }
-
The agent that has just run.
-
callbackContext: Context
The context for the agent invocation.
Returns Promise<Content | undefined>An optional Content object. If a value is returned, it will be
emitted as an additional event appended after the agent's execution.
Returning undefined emits no additional content event.
afterContextCompaction
-
Callback executed after context compaction.
This callback provides an opportunity to inspect the context
after it has been compacted.
Returns Promise<void>
afterModelCallback
-
Inspects LLM responses, intercepts model errors or reserved tool calls, and handles retries.
Returns Promise<LlmResponse | undefined>
afterNodeCallback
-
afterNodeCallback( params: { node: BaseNode; nodeContext: NodeContext; output: unknown },): Promise<unknown>
Callback executed after a workflow node has run.
Not called for a node whose body was skipped by beforeNodeCallback, and
not called when the node throws.
Parameters
- params: { node: BaseNode; nodeContext: NodeContext; output: unknown }
-
The node that has just run.
-
The node's execution context.
-
output: unknown
The output the node produced.
Returns Promise<unknown>An optional value replacing the node's output. Returning
undefined keeps the original.
afterRunCallback
-
Callback executed after successful completion, abort, or early iterator close.
This is the final callback in the ADK lifecycle, suitable for cleanup,
final logging, or reporting tasks. It is skipped when execution fails;
onRunErrorCallback is called instead. Receiving this callback does
not imply success: recognized cancellation still propagates to the caller.
Parameters
- _params: { invocationContext: InvocationContext }
-
The context for the entire invocation.
Returns Promise<void>undefined
afterToolCallback
-
afterToolCallback( _params: { result: Record<string, unknown>; tool: BaseTool; toolArgs: Record<string, unknown>; toolContext: Context; },): Promise<Record<string, unknown> | undefined>
Callback executed after a tool has been called.
This callback allows for inspecting, logging, or modifying the result
returned by a tool.
Parameters
- _params: {
result: Record<string, unknown>; tool: BaseTool; toolArgs: Record<string, unknown>; toolContext: Context; }
-
result: Record<string, unknown>
The dictionary returned by the tool invocation.
-
The tool instance that has just been executed.
-
toolArgs: Record<string, unknown>
The original arguments that were passed to the tool.
-
The context specific to the tool execution.
Returns Promise<Record<string, unknown> | undefined>An optional dictionary. If a dictionary is returned, it will
replace the original result from the tool. This allows for
post-processing or altering tool outputs. Returning undefined uses
the original, unmodified result.
beforeAgentCallback
-
beforeAgentCallback( _params: { agent: BaseAgent; callbackContext: Context },): Promise<Content | undefined>
Callback executed before an agent's primary logic is invoked.
This callback can be used for logging, setup, or to short-circuit the
agent's execution by returning a value.
Parameters
- _params: { agent: BaseAgent; callbackContext: Context }
-
The agent that is about to run.
-
callbackContext: Context
The context for the agent invocation.
Returns Promise<Content | undefined>An optional Content object. If a value is returned, it will
bypass the agent's callbacks and its execution, and return this value
directly. Returning undefined allows the agent to proceed normally.
beforeContextCompaction
-
Callback executed before context compaction.
This callback provides an opportunity to inspect or modify the context
before it is compacted.
Returns Promise<void>
beforeModelCallback
-
Prepares the LLM request by attaching the reflection tool.
Returns Promise<LlmResponse | undefined>
beforeNodeCallback
-
beforeNodeCallback( params: { input: unknown; node: BaseNode; nodeContext: NodeContext },): Promise<unknown>
Callback executed before a workflow node runs.
Parameters
- params: { input: unknown; node: BaseNode; nodeContext: NodeContext }
-
input: unknown
The input the node is about to receive.
-
The node that is about to run.
-
The node's execution context.
Returns Promise<unknown>An optional value. If anything other than undefined is returned,
the node's body is skipped and the returned value becomes its output,
which is how a plugin implements a node-level cache or a stub.
Returning undefined lets the node run normally.
beforeRunCallback
-
beforeRunCallback( _params: { invocationContext: InvocationContext },): Promise<Content | undefined>
Callback executed before the ADK runner runs.
This is the first callback to be called in the lifecycle, ideal for global
setup or initialization tasks.
Parameters
- _params: { invocationContext: InvocationContext }
-
The context for the entire invocation, containing
session information, the root agent, etc.
Returns Promise<Content | undefined>An optional Content to be returned to the ADK. Returning a value
halts execution of the runner and ends the run with an event containing
that content. Return undefined to proceed normally.
beforeToolCallback
-
beforeToolCallback( _params: { tool: BaseTool; toolArgs: Record<string, unknown>; toolContext: Context; },): Promise<Record<string, unknown> | undefined>
Callback executed before a tool is called.
This callback is useful for logging tool usage, input validation, or
modifying the arguments before they are passed to the tool.
Parameters
- _params: { tool: BaseTool; toolArgs: Record<string, unknown>; toolContext: Context }
-
The tool instance that is about to be executed.
-
toolArgs: Record<string, unknown>
The dictionary of arguments to be used for invoking the
tool.
-
The context specific to the tool execution.
Returns Promise<Record<string, unknown> | undefined>An optional dictionary. If a dictionary is returned, it will stop
the tool execution and return this response immediately. Returning
undefined uses the original, unmodified arguments.
beforeToolSelection
-
beforeToolSelection( _params: { callbackContext: Context; tools: Readonly<Record<string, BaseTool>>; },): Promise<Readonly<Record<string, BaseTool>> | undefined>
Callback executed before a tool is selected.
This callback provides an opportunity to inspect, log, or modify the
available tools before they are selected.
Parameters
- _params: { callbackContext: Context; tools: Readonly<Record<string, BaseTool>> }
-
callbackContext: Context
The context for the current agent call.
-
tools: Readonly<Record<string, BaseTool>>
Returns Promise<Readonly<Record<string, BaseTool>> | undefined>An optional value. A non-undefined return may be used by the
framework to modify or replace the available tools. Returning
undefined allows the original tools to be used.
ProtectedcheckForModelError
-
Checks if the model response contains an error matching onModelErrors.
Returns boolean
ProtectedgenerateModelRetryPart
-
generateModelRetryPart( retryCount: number, errorType?: string, errorDetails?: string, finishReason?: FinishReason,): Part
Generates a function call part for the model retry tool.
Parameters
- retryCount: number
OptionalerrorType: string
OptionalerrorDetails: string
OptionalfinishReason: FinishReason
Returns Part
ProtectedgetModelNameFromContext
-
getModelNameFromContext(callbackContext: Context): string
Retrieves the model name from context.
Returns string
ProtectedgetModelRetryUuid
-
getModelRetryUuid(): string
Generates a unique ID for the model retry tool call.
Returns string
ProtectedgetModelScopeKey
-
getModelScopeKey(callbackContext: Context): string
Returns the scope key for model failure tracking.
Returns string
ProtectedhandleModelError
-
Handles detected model errors by initiating retry logic.
Returns Promise<LlmResponse | undefined>
ProtectedhandleModelRetry
-
handleModelRetry( callbackContext: Context, errorType?: string, errorDetails?: string, finishReason?: FinishReason,): Promise<LlmResponse | undefined>
Tracks retry count, generates retry response, and checks against limits.
Parameters
- callbackContext: Context
OptionalerrorType: string
OptionalerrorDetails: string
OptionalfinishReason: FinishReason
Returns Promise<LlmResponse | undefined>
ProtectedhandleReservedToolCall
-
Handles direct calls to the reserved reflection tool.
Returns Promise<LlmResponse | undefined>
ProtectedhasReservedToolCall
-
Checks if the model response calls the reserved reflection tool.
Returns boolean
ProtectedincrementModelFailureCount
-
incrementModelFailureCount(scopeKey: string, itemName: string): Promise<number>
Increment the failure count for a model within a scope.
Parameters
- scopeKey: string
- itemName: string
Returns Promise<number>
onAgentErrorCallback
-
onAgentErrorCallback( _params: { agent: BaseAgent; callbackContext: Context; error: Error },): Promise<void>
Observes an error escaping an agent's execution or lifecycle callbacks.
Notification-only: return values are ignored and callback failures are
logged. The original execution error always propagates.
Returns Promise<void>
onEventCallback
-
Callback executed after an event is yielded from runner.
This is the ideal place to make modification to the event before the event
is handled by the underlying agent app.
Parameters
- _params: { event: Event; invocationContext: InvocationContext }
-
The event raised by the runner.
-
The context for the entire invocation.
Returns Promise<Event | undefined>An optional value. A non-undefined return may be used by the
framework to modify or replace the response. Copy the incoming
event when constructing a replacement to preserve fields that are not
being modified, such as event actions. Returning undefined allows the
original response to be used.
onModelErrorCallback
-
Callback executed when a model call encounters an Error.
This callback provides an opportunity to handle model errors gracefully,
potentially providing alternative responses or recovery mechanisms.
Parameters
- _params: { callbackContext: Context; error: Error; llmRequest: LlmRequest }
-
callbackContext: Context
The context for the current agent call.
-
error: Error
The Error instance thrown during model execution.
-
The request that was sent to the model when the error
occurred.
Returns Promise<LlmResponse | undefined>An optional LlmResponse. If an LlmResponse is returned, it
will be used instead of the default error response. Returning
undefined causes an error response/event with errorCode and
errorMessage derived from error to be yielded.
onRunErrorCallback
-
onRunErrorCallback( _params: { error: Error; invocationContext: InvocationContext },): Promise<void>
Observes an error in the run lifecycle, including before/after-run callbacks.
Notification-only: return values are ignored and callback failures are
logged. The original execution error always propagates. A completion-hook
failure during early iterator close is reported but does not prevent closing.
Returns Promise<void>
onToolErrorCallback
-
onToolErrorCallback( _params: { error: Error; tool: BaseTool; toolArgs: Record<string, unknown>; toolContext: Context; },): Promise<Record<string, unknown> | undefined>
Callback executed when a tool call encounters an Error or cannot be
resolved.
This callback provides an opportunity to handle tool errors gracefully,
potentially providing alternative responses or recovery mechanisms.
Parameters
- _params: {
error: Error; tool: BaseTool; toolArgs: Record<string, unknown>; toolContext: Context; }
-
error: Error
The Error that was raised during tool resolution or
execution.
-
The tool instance that encountered an error.
-
toolArgs: Record<string, unknown>
The arguments that were passed to the tool.
-
The context specific to the tool execution.
Returns Promise<Record<string, unknown> | undefined>An optional dictionary. If a dictionary is returned, it will be
used as the tool response instead of the default error response.
Returning undefined causes {error: error.message} to be returned as
the tool response (unlike adk-python, which re-raises). Non-Error
throwables bypass this callback and become {error: thrownValue}.
onUserMessageCallback
-
onUserMessageCallback( _params: { invocationContext: InvocationContext; userMessage: Content },): Promise<Content | undefined>
Callback executed when a user message is received before an invocation
starts.
This callback helps logging and modifying the user message before the
runner starts the invocation.
Parameters
- _params: { invocationContext: InvocationContext; userMessage: Content }
-
The context for the entire invocation.
-
userMessage: Content
The message content input by user.
Returns Promise<Content | undefined>An optional Content to be returned to the ADK. Returning a
value to replace the user message. Returning undefined to proceed
normally.
ProtectedprovideReflectionTool
-
provideReflectionTool(llmRequest: LlmRequest): void
Provides the adk_handle_model_error tool for reflection and retries.
Returns void
ProtectedresetModelFailureCount
-
resetModelFailureCount(scopeKey: string, itemName: string): Promise<void>
Reset the failure count for a model within a scope.
Parameters
- scopeKey: string
- itemName: string
Returns Promise<void>
Constructors
Properties
Methods
|