@@ -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