Skip to content

Commit e1e1b26

Browse files
committed
tests
1 parent c7fb31e commit e1e1b26

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

sentry_sdk/tracing_utils.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,7 +1035,7 @@ def _get_input_attributes(template, send_pii, args, kwargs):
10351035
attributes[SPANDATA.GEN_AI_REQUEST_MESSAGES]
10361036
)
10371037

1038-
if template == SPANTEMPLATE.AI_TOOL:
1038+
if template == SPANTEMPLATE.AI_TOOL and send_pii:
10391039
if send_pii:
10401040
attributes[SPANDATA.GEN_AI_TOOL_INPUT] = safe_repr(
10411041
{"args": args, "kwargs": kwargs}
@@ -1120,9 +1120,8 @@ def _get_output_attributes(template, send_pii, result):
11201120
if hasattr(result, "model_name") and isinstance(result.model_name, str):
11211121
attributes[SPANDATA.GEN_AI_RESPONSE_MODEL] = result.model_name
11221122

1123-
if template == SPANTEMPLATE.AI_TOOL:
1124-
if send_pii:
1125-
attributes[SPANDATA.GEN_AI_TOOL_OUTPUT] = safe_repr(result)
1123+
if template == SPANTEMPLATE.AI_TOOL and send_pii:
1124+
attributes[SPANDATA.GEN_AI_TOOL_OUTPUT] = safe_repr(result)
11261125

11271126
return attributes
11281127

@@ -1176,9 +1175,6 @@ def _set_output_attributes(span, template, send_pii, result):
11761175
"""
11771176
attributes = {} # type: dict[str, Any]
11781177

1179-
if template == SPANTEMPLATE.AI_AGENT and isinstance(result, str):
1180-
attributes[SPANDATA.GEN_AI_TOOL_OUTPUT] = result
1181-
11821178
attributes.update(_get_output_attributes(template, send_pii, result))
11831179
span.update_data(attributes or {})
11841180

tests/tracing/test_decorator.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ def test_span_templates_ai_dicts(sentry_init, capture_events):
127127

128128
@sentry_sdk.trace(template=SPANTEMPLATE.AI_TOOL)
129129
def my_tool(arg1, arg2):
130+
"""This is a tool function."""
130131
return {
131132
"output": "my_tool_result",
132133
"usage": {
@@ -188,6 +189,7 @@ def my_agent():
188189
)
189190
assert tool_span["data"] == {
190191
"gen_ai.tool.name": "test_decorator.test_span_templates_ai_dicts.<locals>.my_tool",
192+
"gen_ai.tool.description": "This is a tool function.",
191193
"gen_ai.operation.name": "execute_tool",
192194
"gen_ai.usage.input_tokens": 10,
193195
"gen_ai.usage.output_tokens": 20,
@@ -223,6 +225,7 @@ def test_span_templates_ai_objects(sentry_init, capture_events):
223225

224226
@sentry_sdk.trace(template=SPANTEMPLATE.AI_TOOL)
225227
def my_tool(arg1, arg2):
228+
"""This is a tool function."""
226229
mock_usage = mock.Mock()
227230
mock_usage.prompt_tokens = 10
228231
mock_usage.completion_tokens = 20
@@ -287,6 +290,7 @@ def my_agent():
287290
)
288291
assert tool_span["data"] == {
289292
"gen_ai.tool.name": "test_decorator.test_span_templates_ai_objects.<locals>.my_tool",
293+
"gen_ai.tool.description": "This is a tool function.",
290294
"gen_ai.operation.name": "execute_tool",
291295
"gen_ai.usage.input_tokens": 10,
292296
"gen_ai.usage.output_tokens": 20,
@@ -314,3 +318,50 @@ def my_agent():
314318
"thread.id": mock.ANY,
315319
"thread.name": mock.ANY,
316320
}
321+
322+
323+
@pytest.mark.parametrize("send_default_pii", [True, False])
324+
def test_span_templates_ai_pii(sentry_init, capture_events, send_default_pii):
325+
sentry_init(traces_sample_rate=1.0, send_default_pii=send_default_pii)
326+
events = capture_events()
327+
328+
@sentry_sdk.trace(template=SPANTEMPLATE.AI_TOOL)
329+
def my_tool(arg1, arg2, **kwargs):
330+
"""This is a tool function."""
331+
return "tool_output"
332+
333+
@sentry_sdk.trace(template=SPANTEMPLATE.AI_CHAT)
334+
def my_chat(model=None, **kwargs):
335+
return "chat_output"
336+
337+
@sentry_sdk.trace(template=SPANTEMPLATE.AI_AGENT)
338+
def my_agent(*args, **kwargs):
339+
my_tool(1, 2, tool_arg1="3", tool_arg2="4")
340+
my_chat(
341+
model="my-gpt-4o-mini",
342+
prompt="What is the weather in Tokyo?",
343+
system_prompt="You are a helpful assistant that can answer questions about the weather.",
344+
max_tokens=100,
345+
temperature=0.5,
346+
top_p=0.9,
347+
top_k=40,
348+
frequency_penalty=1.0,
349+
presence_penalty=2.0,
350+
)
351+
return "agent_output"
352+
353+
with sentry_sdk.start_transaction(name="test-transaction"):
354+
my_agent(22, 33, arg1=44, arg2=55)
355+
356+
(event,) = events
357+
(_, tool_span, _) = event["spans"]
358+
359+
if send_default_pii:
360+
assert (
361+
tool_span["data"]["gen_ai.tool.input"]
362+
== "{'args': (1, 2), 'kwargs': {'tool_arg1': '3', 'tool_arg2': '4'}}"
363+
)
364+
assert tool_span["data"]["gen_ai.tool.output"] == "'tool_output'"
365+
else:
366+
assert "gen_ai.tool.input" not in tool_span["data"]
367+
assert "gen_ai.tool.output" not in tool_span["data"]

0 commit comments

Comments
 (0)