Skip to content

Commit 79768c6

Browse files
committed
More tests
1 parent caafc14 commit 79768c6

File tree

2 files changed

+67
-22
lines changed

2 files changed

+67
-22
lines changed

sentry_sdk/tracing_utils.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,18 +1094,31 @@ def _get_output_attributes(template, send_pii, result):
10941094

10951095
if template in [SPANTEMPLATE.AI_AGENT, SPANTEMPLATE.AI_TOOL, SPANTEMPLATE.AI_CHAT]:
10961096
attributes.update(_get_usage_attributes(result))
1097-
if hasattr(result, "usage"):
1098-
attributes.update(_get_usage_attributes(result.usage))
1099-
elif hasattr(result, "metadata"):
1100-
if hasattr(result.metadata, "usage"):
1101-
attributes.update(_get_usage_attributes(result.metadata.usage))
1102-
elif isinstance(result, dict) and "usage" in result:
1103-
attributes.update(_get_usage_attributes(result["usage"]))
11041097

1105-
elif hasattr(result, "model") and isinstance(result.model, str):
1106-
attributes[SPANDATA.GEN_AI_RESPONSE_MODEL] = result.model
1107-
elif hasattr(result, "model_name") and isinstance(result.model_name, str):
1108-
attributes[SPANDATA.GEN_AI_RESPONSE_MODEL] = result.model_name
1098+
if isinstance(result, dict):
1099+
if "usage" in result:
1100+
attributes.update(_get_usage_attributes(result["usage"]))
1101+
if (
1102+
"metadata" in result
1103+
and result["metadata"]
1104+
and "usage" in result["metadata"]
1105+
):
1106+
attributes.update(_get_usage_attributes(result["metadata"]["usage"]))
1107+
if "model" in result:
1108+
attributes[SPANDATA.GEN_AI_RESPONSE_MODEL] = result["model"]
1109+
if "model_name" in result:
1110+
attributes[SPANDATA.GEN_AI_RESPONSE_MODEL] = result["model_name"]
1111+
else:
1112+
if hasattr(result, "usage"):
1113+
attributes.update(_get_usage_attributes(result.usage))
1114+
if hasattr(result, "metadata") and hasattr(result.metadata, "usage"):
1115+
attributes.update(_get_usage_attributes(result.metadata.usage))
1116+
if isinstance(result, dict) and "usage" in result:
1117+
attributes.update(_get_usage_attributes(result["usage"]))
1118+
if hasattr(result, "model") and isinstance(result.model, str):
1119+
attributes[SPANDATA.GEN_AI_RESPONSE_MODEL] = result.model
1120+
if hasattr(result, "model_name") and isinstance(result.model_name, str):
1121+
attributes[SPANDATA.GEN_AI_RESPONSE_MODEL] = result.model_name
11091122

11101123
if template == SPANTEMPLATE.AI_TOOL:
11111124
if send_pii:

tests/tracing/test_decorator.py

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,23 +127,43 @@ def test_span_templates(sentry_init, capture_events):
127127

128128
@sentry_sdk.trace(template=SPANTEMPLATE.AI_TOOL)
129129
def my_tool(arg1, arg2):
130-
return "my_tool_result"
130+
mock_usage = mock.Mock()
131+
mock_usage.input_tokens = 11
132+
mock_usage.output_tokens = 22
133+
mock_usage.total_tokens = 33
131134

132-
@sentry_sdk.trace(template=SPANTEMPLATE.AI_CHAT)
133-
def my_chat():
134135
return {
135-
"content": "my_chat_result",
136-
"usage": {
137-
"prompt_tokens": 10,
138-
"completion_tokens": 20,
139-
"total_tokens": 30,
140-
},
136+
"output": "my_tool_result",
137+
"usage": mock_usage,
138+
}
139+
140+
@sentry_sdk.trace(template=SPANTEMPLATE.AI_CHAT)
141+
def my_chat(model=None, **kwargs):
142+
mock_result = mock.Mock()
143+
mock_result.content = "my_chat_result"
144+
mock_result.usage = {
145+
"prompt_tokens": 10,
146+
"completion_tokens": 20,
147+
"total_tokens": 30,
141148
}
149+
mock_result.model = f"{model}-v123"
150+
151+
return mock_result
142152

143153
@sentry_sdk.trace(template=SPANTEMPLATE.AI_AGENT)
144154
def my_agent():
145155
my_tool(1, 2)
146-
my_chat()
156+
my_chat(
157+
model="my-gpt-4o-mini",
158+
prompt="What is the weather in Tokyo?",
159+
system_prompt="You are a helpful assistant that can answer questions about the weather.",
160+
max_tokens=100,
161+
temperature=0.5,
162+
top_p=0.9,
163+
top_k=40,
164+
frequency_penalty=1.0,
165+
presence_penalty=2.0,
166+
)
147167

148168
with sentry_sdk.start_transaction(name="test-transaction"):
149169
my_agent()
@@ -171,14 +191,26 @@ def my_agent():
171191
assert tool_span["data"] == {
172192
"gen_ai.tool.name": "test_decorator.test_span_templates.<locals>.my_tool",
173193
"gen_ai.operation.name": "execute_tool",
194+
"gen_ai.usage.input_tokens": 11,
195+
"gen_ai.usage.output_tokens": 22,
196+
"gen_ai.usage.total_tokens": 33,
174197
"thread.id": mock.ANY,
175198
"thread.name": mock.ANY,
176199
}
177200

178201
assert chat_span["op"] == "gen_ai.chat"
179-
assert chat_span["description"] == "chat"
202+
assert chat_span["description"] == "chat my-gpt-4o-mini"
180203
assert chat_span["data"] == {
181204
"gen_ai.operation.name": "chat",
205+
"gen_ai.request.frequency_penalty": 1.0,
206+
"gen_ai.request.max_tokens": 100,
207+
"gen_ai.request.messages": "[{'role': 'user', 'content': 'What is the weather in Tokyo?'}, {'role': 'system', 'content': 'You are a helpful assistant that can answer questions about the weather.'}]",
208+
"gen_ai.request.model": "my-gpt-4o-mini",
209+
"gen_ai.request.presence_penalty": 2.0,
210+
"gen_ai.request.temperature": 0.5,
211+
"gen_ai.request.top_k": 40,
212+
"gen_ai.request.top_p": 0.9,
213+
"gen_ai.response.model": "my-gpt-4o-mini-v123",
182214
"gen_ai.usage.input_tokens": 10,
183215
"gen_ai.usage.output_tokens": 20,
184216
"gen_ai.usage.total_tokens": 30,

0 commit comments

Comments
 (0)