Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/agents/run_internal/tool_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -1570,10 +1570,14 @@ async def _run_single_tool(
agent_hooks=agent_hooks,
)
except Exception as e:
trace_error = get_trace_tool_error(
trace_include_sensitive_data=self.config.trace_include_sensitive_data,
error_message=str(e),
)
_error_tracing.attach_error_to_current_span(
SpanError(
message="Error running tool",
data={"tool_name": func_tool.name, "error": str(e)},
data={"tool_name": func_tool.name, "error": trace_error},
)
)
if isinstance(e, AgentsException):
Expand Down
42 changes: 42 additions & 0 deletions tests/test_run_step_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,48 @@ async def _error_tool() -> str:
await get_execute_result(agent, response)


@pytest.mark.asyncio
async def test_function_tool_error_trace_respects_sensitive_data_setting():
async def _error_tool() -> str:
raise ValueError("secret-token-123")

error_tool = function_tool(
_error_tool,
name_override="error_tool",
failure_error_function=None,
)
agent = Agent(name="test", tools=[error_tool])
response = ModelResponse(
output=[get_function_tool_call("error_tool", "{}", call_id="1")],
usage=Usage(),
response_id=None,
)

with trace("test"):
with pytest.raises(UserError, match="Error running tool error_tool: secret-token-123"):
await get_execute_result(
agent,
response,
run_config=RunConfig(trace_include_sensitive_data=False),
)

function_spans = []
for span in SPAN_PROCESSOR_TESTING.get_ordered_spans(including_empty=True):
exported = span.export()
if not exported:
continue
span_data = exported.get("span_data")
if isinstance(span_data, dict) and span_data.get("type") == "function":
function_spans.append(exported)

assert len(function_spans) == 1
error = function_spans[0]["error"]
assert error["message"] == "Error running tool"
assert error["data"]["tool_name"] == "error_tool"
assert error["data"]["error"] == "Tool execution failed. Error details are redacted."
assert "secret-token-123" not in str(error)


@pytest.mark.asyncio
async def test_multiple_tool_calls_still_raise_when_sibling_cancelled():
async def _ok_tool() -> str:
Expand Down