6060from .tool_guardrails import ToolInputGuardrail , ToolOutputGuardrail
6161from .tracing import SpanError
6262from .util import _error_tracing
63+ from .util ._tool_errors import get_trace_tool_error
6364from .util ._types import MaybeAwaitable
6465
6566if TYPE_CHECKING :
@@ -422,7 +423,7 @@ class _FailureHandlingFunctionToolInvoker:
422423 def __init__ (
423424 self ,
424425 invoke_tool_impl : Callable [[ToolContext [Any ], str ], Awaitable [Any ]],
425- on_handled_error : Callable [[FunctionTool , Exception , str ], None ],
426+ on_handled_error : Callable [[FunctionTool , Exception , str , ToolContext [ Any ] ], None ],
426427 * ,
427428 function_tool : FunctionTool | None = None ,
428429 ) -> None :
@@ -457,7 +458,7 @@ async def __call__(self, ctx: ToolContext[Any], input: str) -> Any:
457458 if result is None :
458459 raise
459460
460- self ._on_handled_error (self ._function_tool , e , input )
461+ self ._on_handled_error (self ._function_tool , e , input , ctx )
461462 return result
462463
463464
@@ -466,6 +467,26 @@ def with_function_tool_failure_error_handler(
466467 on_handled_error : Callable [[FunctionTool , Exception , str ], None ],
467468) -> Callable [[ToolContext [Any ], str ], Awaitable [Any ]]:
468469 """Wrap a tool invoker so copied FunctionTools resolve failure policy against themselves."""
470+
471+ def _on_handled_error_with_context (
472+ function_tool : FunctionTool ,
473+ error : Exception ,
474+ input_json : str ,
475+ _context : ToolContext [Any ],
476+ ) -> None :
477+ on_handled_error (function_tool , error , input_json )
478+
479+ return _with_context_function_tool_failure_error_handler (
480+ invoke_tool_impl ,
481+ _on_handled_error_with_context ,
482+ )
483+
484+
485+ def _with_context_function_tool_failure_error_handler (
486+ invoke_tool_impl : Callable [[ToolContext [Any ], str ], Awaitable [Any ]],
487+ on_handled_error : Callable [[FunctionTool , Exception , str , ToolContext [Any ]], None ],
488+ ) -> Callable [[ToolContext [Any ], str ], Awaitable [Any ]]:
489+ """Wrap a tool invoker with context-aware handled-error reporting."""
469490 return _FailureHandlingFunctionToolInvoker (invoke_tool_impl , on_handled_error )
470491
471492
@@ -475,7 +496,7 @@ def _build_wrapped_function_tool(
475496 description : str ,
476497 params_json_schema : dict [str , Any ],
477498 invoke_tool_impl : Callable [[ToolContext [Any ], str ], Awaitable [Any ]],
478- on_handled_error : Callable [[FunctionTool , Exception , str ], None ],
499+ on_handled_error : Callable [[FunctionTool , Exception , str , ToolContext [ Any ] ], None ],
479500 failure_error_function : ToolErrorFunction | None | object = _UNSET_FAILURE_ERROR_FUNCTION ,
480501 strict_json_schema : bool = True ,
481502 is_enabled : bool | Callable [[RunContextWrapper [Any ], AgentBase ], MaybeAwaitable [bool ]] = True ,
@@ -493,7 +514,7 @@ def _build_wrapped_function_tool(
493514 tool_origin : ToolOrigin | None = None ,
494515) -> FunctionTool :
495516 """Create a FunctionTool with copied-tool-aware failure handling bound in one place."""
496- on_invoke_tool = with_function_tool_failure_error_handler (
517+ on_invoke_tool = _with_context_function_tool_failure_error_handler (
497518 invoke_tool_impl ,
498519 on_handled_error ,
499520 )
@@ -1377,24 +1398,36 @@ def _build_handled_function_tool_error_handler(
13771398 span_message_for_json_decode_error : str | None = None ,
13781399 include_input_json_in_logs : bool = True ,
13791400 include_tool_name_in_log_messages : bool = True ,
1380- ) -> Callable [[FunctionTool , Exception , str ], None ]:
1401+ ) -> Callable [[FunctionTool , Exception , str , ToolContext [ Any ] ], None ]:
13811402 """Create a consistent handled-error reporter for wrapped FunctionTools."""
13821403
1383- def _on_handled_error (function_tool : FunctionTool , error : Exception , input_json : str ) -> None :
1404+ def _on_handled_error (
1405+ function_tool : FunctionTool ,
1406+ error : Exception ,
1407+ input_json : str ,
1408+ context : ToolContext [Any ],
1409+ ) -> None :
13841410 json_decode_error = _extract_tool_argument_json_error (error )
13851411 if json_decode_error is not None and span_message_for_json_decode_error is not None :
13861412 resolved_span_message = span_message_for_json_decode_error
13871413 span_error_detail = str (json_decode_error )
13881414 else :
13891415 resolved_span_message = span_message
13901416 span_error_detail = str (error )
1417+ trace_include_sensitive_data = (
1418+ context .run_config is None or context .run_config .trace_include_sensitive_data
1419+ )
1420+ trace_error = get_trace_tool_error (
1421+ trace_include_sensitive_data = trace_include_sensitive_data ,
1422+ error_message = span_error_detail ,
1423+ )
13911424
13921425 _error_tracing .attach_error_to_current_span (
13931426 SpanError (
13941427 message = resolved_span_message ,
13951428 data = {
13961429 "tool_name" : function_tool .name ,
1397- "error" : span_error_detail ,
1430+ "error" : trace_error ,
13981431 },
13991432 )
14001433 )
0 commit comments