@@ -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
0 commit comments