Skip to content

Commit 38b570a

Browse files
authored
Span data is always be a primitive data type (#4643)
The AI Agent insights module excepts the data not to be lists, tuples, or dicts. Make sure that we always send a string in this case.
1 parent bab6215 commit 38b570a

File tree

4 files changed

+29
-22
lines changed

4 files changed

+29
-22
lines changed

sentry_sdk/ai/utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@ def _normalize_data(data):
2323
return list(_normalize_data(x) for x in data)
2424
if isinstance(data, dict):
2525
return {k: _normalize_data(v) for (k, v) in data.items()}
26+
2627
return data
2728

2829

2930
def set_data_normalized(span, key, value):
3031
# type: (Span, str, Any) -> None
3132
normalized = _normalize_data(value)
32-
span.set_data(key, normalized)
33+
if isinstance(normalized, (int, float, bool, str)):
34+
span.set_data(key, normalized)
35+
else:
36+
span.set_data(key, str(normalized))

tests/integrations/cohere/test_cohere.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,14 @@ def test_nonstreaming_chat(
5757
assert span["data"][SPANDATA.AI_MODEL_ID] == "some-model"
5858

5959
if send_default_pii and include_prompts:
60-
assert "some context" in span["data"][SPANDATA.AI_INPUT_MESSAGES][0]["content"]
61-
assert "hello" in span["data"][SPANDATA.AI_INPUT_MESSAGES][1]["content"]
60+
assert (
61+
"{'role': 'system', 'content': 'some context'}"
62+
in span["data"][SPANDATA.AI_INPUT_MESSAGES]
63+
)
64+
assert (
65+
"{'role': 'user', 'content': 'hello'}"
66+
in span["data"][SPANDATA.AI_INPUT_MESSAGES]
67+
)
6268
assert "the model response" in span["data"][SPANDATA.AI_RESPONSES]
6369
else:
6470
assert SPANDATA.AI_INPUT_MESSAGES not in span["data"]
@@ -128,8 +134,14 @@ def test_streaming_chat(sentry_init, capture_events, send_default_pii, include_p
128134
assert span["data"][SPANDATA.AI_MODEL_ID] == "some-model"
129135

130136
if send_default_pii and include_prompts:
131-
assert "some context" in span["data"][SPANDATA.AI_INPUT_MESSAGES][0]["content"]
132-
assert "hello" in span["data"][SPANDATA.AI_INPUT_MESSAGES][1]["content"]
137+
assert (
138+
"{'role': 'system', 'content': 'some context'}"
139+
in span["data"][SPANDATA.AI_INPUT_MESSAGES]
140+
)
141+
assert (
142+
"{'role': 'user', 'content': 'hello'}"
143+
in span["data"][SPANDATA.AI_INPUT_MESSAGES]
144+
)
133145
assert "the model response" in span["data"][SPANDATA.AI_RESPONSES]
134146
else:
135147
assert SPANDATA.AI_INPUT_MESSAGES not in span["data"]

tests/integrations/langchain/test_langchain.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,13 @@ def test_langchain_agent(
196196

197197
if send_default_pii and include_prompts:
198198
assert (
199-
"You are very powerful"
200-
in chat_spans[0]["data"][SPANDATA.AI_INPUT_MESSAGES][0]["content"]
199+
"You are very powerful" in chat_spans[0]["data"][SPANDATA.AI_INPUT_MESSAGES]
201200
)
202201
assert "5" in chat_spans[0]["data"][SPANDATA.AI_RESPONSES]
203202
assert "word" in tool_exec_span["data"][SPANDATA.AI_INPUT_MESSAGES]
204203
assert 5 == int(tool_exec_span["data"][SPANDATA.AI_RESPONSES])
205204
assert (
206-
"You are very powerful"
207-
in chat_spans[1]["data"][SPANDATA.AI_INPUT_MESSAGES][0]["content"]
205+
"You are very powerful" in chat_spans[1]["data"][SPANDATA.AI_INPUT_MESSAGES]
208206
)
209207
assert "5" in chat_spans[1]["data"][SPANDATA.AI_RESPONSES]
210208
else:

tests/integrations/openai/test_openai.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import json
21
import pytest
32
from openai import AsyncOpenAI, OpenAI, AsyncStream, Stream, OpenAIError
43
from openai.types import CompletionUsage, CreateEmbeddingResponse, Embedding
@@ -144,11 +143,8 @@ def test_nonstreaming_chat_completion(
144143
assert span["op"] == "gen_ai.chat"
145144

146145
if send_default_pii and include_prompts:
147-
assert "hello" in span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]["content"]
148-
assert (
149-
"the model response"
150-
in json.loads(span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT])[0]["content"]
151-
)
146+
assert "hello" in span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
147+
assert "the model response" in span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT]
152148
else:
153149
assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["data"]
154150
assert SPANDATA.GEN_AI_RESPONSE_TEXT not in span["data"]
@@ -189,11 +185,8 @@ async def test_nonstreaming_chat_completion_async(
189185
assert span["op"] == "gen_ai.chat"
190186

191187
if send_default_pii and include_prompts:
192-
assert "hello" in span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]["content"]
193-
assert (
194-
"the model response"
195-
in json.loads(span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT])[0]["content"]
196-
)
188+
assert "hello" in span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
189+
assert "the model response" in span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT]
197190
else:
198191
assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["data"]
199192
assert SPANDATA.GEN_AI_RESPONSE_TEXT not in span["data"]
@@ -285,7 +278,7 @@ def test_streaming_chat_completion(
285278
assert span["op"] == "gen_ai.chat"
286279

287280
if send_default_pii and include_prompts:
288-
assert "hello" in span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]["content"]
281+
assert "hello" in span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
289282
assert "hello world" in span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT]
290283
else:
291284
assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["data"]
@@ -381,7 +374,7 @@ async def test_streaming_chat_completion_async(
381374
assert span["op"] == "gen_ai.chat"
382375

383376
if send_default_pii and include_prompts:
384-
assert "hello" in span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]["content"]
377+
assert "hello" in span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
385378
assert "hello world" in span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT]
386379
else:
387380
assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["data"]

0 commit comments

Comments
 (0)