@@ -124,9 +124,10 @@ 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 span ["data" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ] == [
128- {"role" : "user" , "content" : "Hello, Claude" }
129- ]
127+ assert (
128+ span ["data" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ]
129+ == '[{"role": "user", "content": "Hello, Claude"}]'
130+ )
130131 assert span ["data" ][SPANDATA .GEN_AI_RESPONSE_TEXT ] == "Hi, I'm Claude."
131132 else :
132133 assert SPANDATA .GEN_AI_REQUEST_MESSAGES not in span ["data" ]
@@ -192,9 +193,10 @@ async def test_nonstreaming_create_message_async(
192193 assert span ["data" ][SPANDATA .GEN_AI_REQUEST_MODEL ] == "model"
193194
194195 if send_default_pii and include_prompts :
195- assert span ["data" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ] == [
196- {"role" : "user" , "content" : "Hello, Claude" }
197- ]
196+ assert (
197+ span ["data" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ]
198+ == '[{"role": "user", "content": "Hello, Claude"}]'
199+ )
198200 assert span ["data" ][SPANDATA .GEN_AI_RESPONSE_TEXT ] == "Hi, I'm Claude."
199201 else :
200202 assert SPANDATA .GEN_AI_REQUEST_MESSAGES not in span ["data" ]
@@ -291,9 +293,10 @@ def test_streaming_create_message(
291293 assert span ["data" ][SPANDATA .GEN_AI_REQUEST_MODEL ] == "model"
292294
293295 if send_default_pii and include_prompts :
294- assert span ["data" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ] == [
295- {"role" : "user" , "content" : "Hello, Claude" }
296- ]
296+ assert (
297+ span ["data" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ]
298+ == '[{"role": "user", "content": "Hello, Claude"}]'
299+ )
297300 assert span ["data" ][SPANDATA .GEN_AI_RESPONSE_TEXT ] == "Hi! I'm Claude!"
298301
299302 else :
@@ -394,9 +397,10 @@ async def test_streaming_create_message_async(
394397 assert span ["data" ][SPANDATA .GEN_AI_REQUEST_MODEL ] == "model"
395398
396399 if send_default_pii and include_prompts :
397- assert span ["data" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ] == [
398- {"role" : "user" , "content" : "Hello, Claude" }
399- ]
400+ assert (
401+ span ["data" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ]
402+ == '[{"role": "user", "content": "Hello, Claude"}]'
403+ )
400404 assert span ["data" ][SPANDATA .GEN_AI_RESPONSE_TEXT ] == "Hi! I'm Claude!"
401405
402406 else :
@@ -524,9 +528,10 @@ def test_streaming_create_message_with_input_json_delta(
524528 assert span ["data" ][SPANDATA .GEN_AI_REQUEST_MODEL ] == "model"
525529
526530 if send_default_pii and include_prompts :
527- assert span ["data" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ] == [
528- {"role" : "user" , "content" : "What is the weather like in San Francisco?" }
529- ]
531+ assert (
532+ span ["data" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ]
533+ == '[{"role": "user", "content": "What is the weather like in San Francisco?"}]'
534+ )
530535 assert (
531536 span ["data" ][SPANDATA .GEN_AI_RESPONSE_TEXT ]
532537 == "{'location': 'San Francisco, CA'}"
@@ -663,9 +668,10 @@ async def test_streaming_create_message_with_input_json_delta_async(
663668 assert span ["data" ][SPANDATA .GEN_AI_REQUEST_MODEL ] == "model"
664669
665670 if send_default_pii and include_prompts :
666- assert span ["data" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ] == [
667- {"role" : "user" , "content" : "What is the weather like in San Francisco?" }
668- ]
671+ assert (
672+ span ["data" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ]
673+ == '[{"role": "user", "content": "What is the weather like in San Francisco?"}]'
674+ )
669675 assert (
670676 span ["data" ][SPANDATA .GEN_AI_RESPONSE_TEXT ]
671677 == "{'location': 'San Francisco, CA'}"
@@ -919,7 +925,7 @@ def test_anthropic_message_role_mapping(sentry_init, capture_events):
919925 assert span ["op" ] == "gen_ai.chat"
920926 assert SPANDATA .GEN_AI_REQUEST_MESSAGES in span ["data" ]
921927
922- stored_messages = span ["data" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ]
928+ stored_messages = json . loads ( span ["data" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ])
923929 assert len (stored_messages ) == 4
924930 assert stored_messages [0 ]["role" ] == "system"
925931 assert stored_messages [1 ]["role" ] == "user"
@@ -975,10 +981,13 @@ def test_anthropic_message_truncation(sentry_init, capture_events):
975981
976982 assert SPANDATA .GEN_AI_REQUEST_MESSAGES in span ["data" ]
977983 messages_data = span ["data" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ]
978- assert isinstance (messages_data , list )
979- assert len (messages_data ) <= len (large_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 )
980989
981- result_size = len (serialize ( messages_data , is_vars = False ))
990+ result_size = len (messages_data . encode ( "utf-8" ))
982991 assert result_size <= MAX_GEN_AI_MESSAGE_BYTES
983992
984993
@@ -1021,8 +1030,10 @@ def test_anthropic_single_large_message_preservation(sentry_init, capture_events
10211030
10221031 assert SPANDATA .GEN_AI_REQUEST_MESSAGES in span ["data" ]
10231032 messages_data = span ["data" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ]
1024- assert isinstance (messages_data , list )
1033+ assert isinstance (messages_data , str )
10251034
1026- assert len (messages_data ) == 1
1027- assert messages_data [0 ]["role" ] == "user"
1028- assert len (messages_data [0 ]["content" ]) < len (huge_content )
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 )
0 commit comments