Skip to content

Commit ad4f10a

Browse files
committed
fix tests for messages as list
1 parent 8f0e049 commit ad4f10a

File tree

4 files changed

+60
-68
lines changed

4 files changed

+60
-68
lines changed

tests/integrations/anthropic/test_anthropic.py

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,9 @@ def test_nonstreaming_create_message(
124124
assert span["data"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model"
125125

126126
if send_default_pii and include_prompts:
127-
assert (
128-
span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
129-
== '[{"role": "user", "content": "Hello, Claude"}]'
130-
)
127+
assert span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES] == [
128+
{"role": "user", "content": "Hello, Claude"}
129+
]
131130
assert span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT] == "Hi, I'm Claude."
132131
else:
133132
assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["data"]
@@ -193,10 +192,9 @@ async def test_nonstreaming_create_message_async(
193192
assert span["data"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model"
194193

195194
if send_default_pii and include_prompts:
196-
assert (
197-
span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
198-
== '[{"role": "user", "content": "Hello, Claude"}]'
199-
)
195+
assert span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES] == [
196+
{"role": "user", "content": "Hello, Claude"}
197+
]
200198
assert span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT] == "Hi, I'm Claude."
201199
else:
202200
assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["data"]
@@ -293,10 +291,9 @@ def test_streaming_create_message(
293291
assert span["data"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model"
294292

295293
if send_default_pii and include_prompts:
296-
assert (
297-
span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
298-
== '[{"role": "user", "content": "Hello, Claude"}]'
299-
)
294+
assert span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES] == [
295+
{"role": "user", "content": "Hello, Claude"}
296+
]
300297
assert span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT] == "Hi! I'm Claude!"
301298

302299
else:
@@ -397,10 +394,9 @@ async def test_streaming_create_message_async(
397394
assert span["data"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model"
398395

399396
if send_default_pii and include_prompts:
400-
assert (
401-
span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
402-
== '[{"role": "user", "content": "Hello, Claude"}]'
403-
)
397+
assert span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES] == [
398+
{"role": "user", "content": "Hello, Claude"}
399+
]
404400
assert span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT] == "Hi! I'm Claude!"
405401

406402
else:
@@ -528,10 +524,9 @@ def test_streaming_create_message_with_input_json_delta(
528524
assert span["data"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model"
529525

530526
if send_default_pii and include_prompts:
531-
assert (
532-
span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
533-
== '[{"role": "user", "content": "What is the weather like in San Francisco?"}]'
534-
)
527+
assert span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES] == [
528+
{"role": "user", "content": "What is the weather like in San Francisco?"}
529+
]
535530
assert (
536531
span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT]
537532
== "{'location': 'San Francisco, CA'}"
@@ -668,10 +663,9 @@ async def test_streaming_create_message_with_input_json_delta_async(
668663
assert span["data"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model"
669664

670665
if send_default_pii and include_prompts:
671-
assert (
672-
span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
673-
== '[{"role": "user", "content": "What is the weather like in San Francisco?"}]'
674-
)
666+
assert span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES] == [
667+
{"role": "user", "content": "What is the weather like in San Francisco?"}
668+
]
675669
assert (
676670
span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT]
677671
== "{'location': 'San Francisco, CA'}"
@@ -925,7 +919,7 @@ def test_anthropic_message_role_mapping(sentry_init, capture_events):
925919
assert span["op"] == "gen_ai.chat"
926920
assert SPANDATA.GEN_AI_REQUEST_MESSAGES in span["data"]
927921

928-
stored_messages = json.loads(span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES])
922+
stored_messages = span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
929923
assert len(stored_messages) == 4
930924
assert stored_messages[0]["role"] == "system"
931925
assert stored_messages[1]["role"] == "user"
@@ -981,13 +975,10 @@ def test_anthropic_message_truncation(sentry_init, capture_events):
981975

982976
assert SPANDATA.GEN_AI_REQUEST_MESSAGES in span["data"]
983977
messages_data = span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
984-
assert isinstance(messages_data, str)
985-
986-
parsed_messages = json.loads(messages_data)
987-
assert isinstance(parsed_messages, list)
988-
assert len(parsed_messages) <= len(large_messages)
978+
assert isinstance(messages_data, list)
979+
assert len(messages_data) <= len(large_messages)
989980

990-
result_size = len(messages_data.encode("utf-8"))
981+
result_size = len(serialize(messages_data, is_vars=False))
991982
assert result_size <= MAX_GEN_AI_MESSAGE_BYTES
992983

993984

@@ -1030,10 +1021,8 @@ def test_anthropic_single_large_message_preservation(sentry_init, capture_events
10301021

10311022
assert SPANDATA.GEN_AI_REQUEST_MESSAGES in span["data"]
10321023
messages_data = span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
1033-
assert isinstance(messages_data, str)
1024+
assert isinstance(messages_data, list)
10341025

1035-
parsed_messages = json.loads(messages_data)
1036-
assert isinstance(parsed_messages, list)
1037-
assert len(parsed_messages) == 1
1038-
assert parsed_messages[0]["role"] == "user"
1039-
assert len(parsed_messages[0]["content"]) < len(huge_content)
1026+
assert len(messages_data) == 1
1027+
assert messages_data[0]["role"] == "user"
1028+
assert len(messages_data[0]["content"]) < len(huge_content)

tests/integrations/langgraph/test_langgraph.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -672,9 +672,7 @@ def __init__(self, content, message_type="human"):
672672

673673
# If messages were captured, verify role mapping
674674
if SPANDATA.GEN_AI_REQUEST_MESSAGES in span["data"]:
675-
import json
676-
677-
stored_messages = json.loads(span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES])
675+
stored_messages = span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
678676

679677
# Find messages with specific content to verify role mapping
680678
ai_message = next(

tests/integrations/openai/test_openai.py

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ def test_nonstreaming_chat_completion(
156156
assert span["op"] == "gen_ai.chat"
157157

158158
if send_default_pii and include_prompts:
159-
assert "hello" in span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
159+
messages = span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
160+
assert isinstance(messages, list)
161+
assert any("hello" in msg.get("content", "") for msg in messages)
160162
assert "the model response" in span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT]
161163
else:
162164
assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["data"]
@@ -198,7 +200,9 @@ async def test_nonstreaming_chat_completion_async(
198200
assert span["op"] == "gen_ai.chat"
199201

200202
if send_default_pii and include_prompts:
201-
assert "hello" in span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
203+
messages = span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
204+
assert isinstance(messages, list)
205+
assert any("hello" in msg.get("content", "") for msg in messages)
202206
assert "the model response" in span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT]
203207
else:
204208
assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["data"]
@@ -291,7 +295,9 @@ def test_streaming_chat_completion(
291295
assert span["op"] == "gen_ai.chat"
292296

293297
if send_default_pii and include_prompts:
294-
assert "hello" in span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
298+
messages = span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
299+
assert isinstance(messages, list)
300+
assert any("hello" in msg.get("content", "") for msg in messages)
295301
assert "hello world" in span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT]
296302
else:
297303
assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["data"]
@@ -387,7 +393,9 @@ async def test_streaming_chat_completion_async(
387393
assert span["op"] == "gen_ai.chat"
388394

389395
if send_default_pii and include_prompts:
390-
assert "hello" in span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
396+
messages = span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
397+
assert isinstance(messages, list)
398+
assert any("hello" in msg.get("content", "") for msg in messages)
391399
assert "hello world" in span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT]
392400
else:
393401
assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["data"]
@@ -1060,7 +1068,9 @@ def test_ai_client_span_responses_api(sentry_init, capture_events):
10601068
assert spans[0]["origin"] == "auto.ai.openai"
10611069
assert spans[0]["data"] == {
10621070
"gen_ai.operation.name": "responses",
1063-
"gen_ai.request.messages": '["How do I check if a Python object is an instance of a class?"]',
1071+
"gen_ai.request.messages": [
1072+
"How do I check if a Python object is an instance of a class?"
1073+
],
10641074
"gen_ai.request.model": "gpt-4o",
10651075
"gen_ai.system": "openai",
10661076
"gen_ai.response.model": "response-model-id",
@@ -1140,7 +1150,9 @@ async def test_ai_client_span_responses_async_api(sentry_init, capture_events):
11401150
assert spans[0]["origin"] == "auto.ai.openai"
11411151
assert spans[0]["data"] == {
11421152
"gen_ai.operation.name": "responses",
1143-
"gen_ai.request.messages": '["How do I check if a Python object is an instance of a class?"]',
1153+
"gen_ai.request.messages": [
1154+
"How do I check if a Python object is an instance of a class?"
1155+
],
11441156
"gen_ai.request.model": "gpt-4o",
11451157
"gen_ai.response.model": "response-model-id",
11461158
"gen_ai.system": "openai",
@@ -1186,7 +1198,9 @@ async def test_ai_client_span_streaming_responses_async_api(
11861198
assert spans[0]["origin"] == "auto.ai.openai"
11871199
assert spans[0]["data"] == {
11881200
"gen_ai.operation.name": "responses",
1189-
"gen_ai.request.messages": '["How do I check if a Python object is an instance of a class?"]',
1201+
"gen_ai.request.messages": [
1202+
"How do I check if a Python object is an instance of a class?"
1203+
],
11901204
"gen_ai.request.model": "gpt-4o",
11911205
"gen_ai.response.model": "response-model-id",
11921206
"gen_ai.response.streaming": True,
@@ -1356,7 +1370,7 @@ def test_streaming_responses_api(
13561370
assert span["op"] == "gen_ai.responses"
13571371

13581372
if send_default_pii and include_prompts:
1359-
assert span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES] == '["hello"]'
1373+
assert span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES] == ["hello"]
13601374
assert span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT] == "hello world"
13611375
else:
13621376
assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["data"]
@@ -1411,7 +1425,7 @@ async def test_streaming_responses_api_async(
14111425
assert span["op"] == "gen_ai.responses"
14121426

14131427
if send_default_pii and include_prompts:
1414-
assert span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES] == '["hello"]'
1428+
assert span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES] == ["hello"]
14151429
assert span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT] == "hello world"
14161430
else:
14171431
assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["data"]
@@ -1482,9 +1496,7 @@ def test_openai_message_role_mapping(sentry_init, capture_events):
14821496
assert SPANDATA.GEN_AI_REQUEST_MESSAGES in span["data"]
14831497

14841498
# Parse the stored messages
1485-
import json
1486-
1487-
stored_messages = json.loads(span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES])
1499+
stored_messages = span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
14881500

14891501
# Verify that "ai" role was mapped to "assistant"
14901502
assert len(stored_messages) == 4
@@ -1537,13 +1549,10 @@ def test_openai_message_truncation(sentry_init, capture_events):
15371549
assert SPANDATA.GEN_AI_REQUEST_MESSAGES in span["data"]
15381550

15391551
messages_data = span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
1540-
assert isinstance(messages_data, str)
1541-
1542-
parsed_messages = json.loads(messages_data)
1543-
assert isinstance(parsed_messages, list)
1544-
assert len(parsed_messages) <= len(large_messages)
1552+
assert isinstance(messages_data, list)
1553+
assert len(messages_data) <= len(large_messages)
15451554

1546-
if "_meta" in event and len(parsed_messages) < len(large_messages):
1555+
if "_meta" in event and len(messages_data) < len(large_messages):
15471556
meta_path = event["_meta"]
15481557
if (
15491558
"spans" in meta_path
@@ -1585,13 +1594,11 @@ def test_openai_single_large_message_content_truncation(sentry_init, capture_eve
15851594
assert SPANDATA.GEN_AI_REQUEST_MESSAGES in span["data"]
15861595

15871596
messages_data = span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
1588-
assert isinstance(messages_data, str)
1597+
assert isinstance(messages_data, list)
15891598

1590-
parsed_messages = json.loads(messages_data)
1591-
assert isinstance(parsed_messages, list)
1592-
assert len(parsed_messages) == 1
1593-
assert parsed_messages[0]["role"] == "user"
1594-
assert len(parsed_messages[0]["content"]) < len(huge_content)
1599+
assert len(messages_data) == 1
1600+
assert messages_data[0]["role"] == "user"
1601+
assert len(messages_data[0]["content"]) < len(huge_content)
15951602

1596-
result_size = len(messages_data.encode("utf-8"))
1603+
result_size = len(serialize(messages_data, is_vars=False))
15971604
assert result_size <= MAX_GEN_AI_MESSAGE_BYTES

tests/integrations/openai_agents/test_openai_agents.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,9 +1066,7 @@ def test_openai_agents_message_role_mapping(sentry_init, capture_events):
10661066
from sentry_sdk.consts import SPANDATA
10671067

10681068
if SPANDATA.GEN_AI_REQUEST_MESSAGES in span._data:
1069-
import json
1070-
1071-
stored_messages = json.loads(span._data[SPANDATA.GEN_AI_REQUEST_MESSAGES])
1069+
stored_messages = span._data[SPANDATA.GEN_AI_REQUEST_MESSAGES]
10721070

10731071
# Verify roles were properly mapped
10741072
found_assistant_roles = 0

0 commit comments

Comments
 (0)