@@ -142,8 +142,20 @@ async def after_tool_callback(
142142 tool_args : dict [str , Any ],
143143 tool_context : ToolContext ,
144144 result : Any ,
145- ) -> Optional [dict ]:
146- """Handles successful tool calls or extracts and processes errors."""
145+ ) -> Optional [dict [str , Any ]]:
146+ """Handles successful tool calls or extracts and processes errors.
147+
148+ Args:
149+ tool: The tool that was called.
150+ tool_args: The arguments passed to the tool.
151+ tool_context: The context of the tool call.
152+ result: The result of the tool call.
153+
154+ Returns:
155+ An optional dictionary containing reflection guidance if an error is
156+ detected, or None if the tool call was successful or the
157+ response is already a reflection message.
158+ """
147159 if (
148160 isinstance (result , dict )
149161 and result .get ("response_type" ) == REFLECT_AND_RETRY_RESPONSE_TYPE
@@ -157,7 +169,8 @@ async def after_tool_callback(
157169 if error :
158170 return await self ._handle_tool_error (tool , tool_args , tool_context , error )
159171
160- # On success, reset the failure count for this specific tool within its scope.
172+ # On success, reset the failure count for this specific tool within
173+ # its scope.
161174 await self ._reset_failures_for_tool (tool_context , tool .name )
162175 return None
163176
@@ -168,14 +181,23 @@ async def extract_error_from_result(
168181 tool_args : dict [str , Any ],
169182 tool_context : ToolContext ,
170183 result : Any ,
171- ) -> Optional [Any ]:
184+ ) -> Optional [dict [ str , Any ] ]:
172185 """Extracts an error from a successful tool result and triggers retry logic.
173186
174187 This is useful when tool call finishes successfully but the result contains
175188 an error object like {"error": ...} that should be handled by the plugin.
176189
177190 By overriding this method, you can trigger retry logic on these successful
178191 results that contain errors.
192+
193+ Args:
194+ tool: The tool that was called.
195+ tool_args: The arguments passed to the tool.
196+ tool_context: The context of the tool call.
197+ result: The result of the tool call.
198+
199+ Returns:
200+ The extracted error if any, or None if no error was detected.
179201 """
180202 return None
181203
@@ -186,8 +208,18 @@ async def on_tool_error_callback(
186208 tool_args : dict [str , Any ],
187209 tool_context : ToolContext ,
188210 error : Exception ,
189- ) -> Optional [dict ]:
190- """Handles tool exceptions by providing reflection guidance."""
211+ ) -> Optional [dict [str , Any ]]:
212+ """Handles tool exceptions by providing reflection guidance.
213+
214+ Args:
215+ tool: The tool that was called.
216+ tool_args: The arguments passed to the tool.
217+ tool_context: The context of the tool call.
218+ error: The exception raised by the tool.
219+
220+ Returns:
221+ An optional dictionary containing reflection guidance for the error.
222+ """
191223 return await self ._handle_tool_error (tool , tool_args , tool_context , error )
192224
193225 async def _handle_tool_error (
@@ -196,8 +228,18 @@ async def _handle_tool_error(
196228 tool_args : dict [str , Any ],
197229 tool_context : ToolContext ,
198230 error : Any ,
199- ) -> Optional [dict ]:
200- """Central, thread-safe logic for processing tool errors."""
231+ ) -> Optional [dict [str , Any ]]:
232+ """Central, thread-safe logic for processing tool errors.
233+
234+ Args:
235+ tool: The tool that was called.
236+ tool_args: The arguments passed to the tool.
237+ tool_context: The context of the tool call.
238+ error: The error to be handled.
239+
240+ Returns:
241+ An optional dictionary containing reflection guidance for the error.
242+ """
201243 if self .max_retries == 0 :
202244 if self .throw_exception_if_retry_exceeded :
203245 raise error
@@ -285,6 +327,7 @@ def _create_tool_reflection_response(
2853272. **State or Preconditions**: Did a previous step fail or not produce the necessary state/resource for this tool to succeed?
2863283. **Alternative Approach**: Is this the right tool for the job? Could another tool or a different sequence of steps achieve the goal?
2873294. **Simplify the Task**: Can you break the problem down into smaller, simpler steps?
330+ 5. **Wrong Function Name**: Does the error indicates the tool is not found? Please check again and only use available tools.
288331
289332Formulate a new plan based on your analysis and try a corrected or different approach.
290333"""
0 commit comments