@@ -19,7 +19,7 @@ async def __call__(self, *args, **kwargs):
1919from anthropic .types .message_delta_event import MessageDeltaEvent
2020from anthropic .types .message_start_event import MessageStartEvent
2121
22- from sentry_sdk .utils import package_version
22+ from sentry_sdk .utils import _serialize_span_attribute , package_version
2323
2424try :
2525 from anthropic .types import InputJSONDelta
@@ -115,10 +115,12 @@ def test_nonstreaming_create_message(
115115 assert span ["data" ][SPANDATA .AI_MODEL_ID ] == "model"
116116
117117 if send_default_pii and include_prompts :
118- assert span ["data" ][SPANDATA .AI_INPUT_MESSAGES ] == messages
119- assert span ["data" ][SPANDATA .AI_RESPONSES ] == [
120- {"type" : "text" , "text" : "Hi, I'm Claude." }
121- ]
118+ assert span ["data" ][SPANDATA .AI_INPUT_MESSAGES ] == _serialize_span_attribute (
119+ messages
120+ )
121+ assert span ["data" ][SPANDATA .AI_RESPONSES ] == _serialize_span_attribute (
122+ [{"type" : "text" , "text" : "Hi, I'm Claude." }]
123+ )
122124 else :
123125 assert SPANDATA .AI_INPUT_MESSAGES not in span ["data" ]
124126 assert SPANDATA .AI_RESPONSES not in span ["data" ]
@@ -183,10 +185,12 @@ async def test_nonstreaming_create_message_async(
183185 assert span ["data" ][SPANDATA .AI_MODEL_ID ] == "model"
184186
185187 if send_default_pii and include_prompts :
186- assert span ["data" ][SPANDATA .AI_INPUT_MESSAGES ] == messages
187- assert span ["data" ][SPANDATA .AI_RESPONSES ] == [
188- {"type" : "text" , "text" : "Hi, I'm Claude." }
189- ]
188+ assert span ["data" ][SPANDATA .AI_INPUT_MESSAGES ] == _serialize_span_attribute (
189+ messages
190+ )
191+ assert span ["data" ][SPANDATA .AI_RESPONSES ] == _serialize_span_attribute (
192+ [{"type" : "text" , "text" : "Hi, I'm Claude." }]
193+ )
190194 else :
191195 assert SPANDATA .AI_INPUT_MESSAGES not in span ["data" ]
192196 assert SPANDATA .AI_RESPONSES not in span ["data" ]
@@ -282,10 +286,12 @@ def test_streaming_create_message(
282286 assert span ["data" ][SPANDATA .AI_MODEL_ID ] == "model"
283287
284288 if send_default_pii and include_prompts :
285- assert span ["data" ][SPANDATA .AI_INPUT_MESSAGES ] == messages
286- assert span ["data" ][SPANDATA .AI_RESPONSES ] == [
287- {"type" : "text" , "text" : "Hi! I'm Claude!" }
288- ]
289+ assert span ["data" ][SPANDATA .AI_INPUT_MESSAGES ] == _serialize_span_attribute (
290+ messages
291+ )
292+ assert span ["data" ][SPANDATA .AI_RESPONSES ] == _serialize_span_attribute (
293+ [{"type" : "text" , "text" : "Hi! I'm Claude!" }]
294+ )
289295
290296 else :
291297 assert SPANDATA .AI_INPUT_MESSAGES not in span ["data" ]
@@ -385,10 +391,12 @@ async def test_streaming_create_message_async(
385391 assert span ["data" ][SPANDATA .AI_MODEL_ID ] == "model"
386392
387393 if send_default_pii and include_prompts :
388- assert span ["data" ][SPANDATA .AI_INPUT_MESSAGES ] == messages
389- assert span ["data" ][SPANDATA .AI_RESPONSES ] == [
390- {"type" : "text" , "text" : "Hi! I'm Claude!" }
391- ]
394+ assert span ["data" ][SPANDATA .AI_INPUT_MESSAGES ] == _serialize_span_attribute (
395+ messages
396+ )
397+ assert span ["data" ][SPANDATA .AI_RESPONSES ] == _serialize_span_attribute (
398+ [{"type" : "text" , "text" : "Hi! I'm Claude!" }]
399+ )
392400
393401 else :
394402 assert SPANDATA .AI_INPUT_MESSAGES not in span ["data" ]
@@ -515,10 +523,12 @@ def test_streaming_create_message_with_input_json_delta(
515523 assert span ["data" ][SPANDATA .AI_MODEL_ID ] == "model"
516524
517525 if send_default_pii and include_prompts :
518- assert span ["data" ][SPANDATA .AI_INPUT_MESSAGES ] == messages
519- assert span ["data" ][SPANDATA .AI_RESPONSES ] == [
520- {"text" : "" , "type" : "text" }
521- ] # we do not record InputJSONDelta because it could contain PII
526+ assert span ["data" ][SPANDATA .AI_INPUT_MESSAGES ] == _serialize_span_attribute (
527+ messages
528+ )
529+ assert span ["data" ][SPANDATA .AI_RESPONSES ] == _serialize_span_attribute (
530+ [{"type" : "text" , "text" : "" }]
531+ ) # we do not record InputJSONDelta because it could contain PII
522532
523533 else :
524534 assert SPANDATA .AI_INPUT_MESSAGES not in span ["data" ]
@@ -652,10 +662,12 @@ async def test_streaming_create_message_with_input_json_delta_async(
652662 assert span ["data" ][SPANDATA .AI_MODEL_ID ] == "model"
653663
654664 if send_default_pii and include_prompts :
655- assert span ["data" ][SPANDATA .AI_INPUT_MESSAGES ] == messages
656- assert span ["data" ][SPANDATA .AI_RESPONSES ] == [
657- {"text" : "" , "type" : "text" }
658- ] # we do not record InputJSONDelta because it could contain PII
665+ assert span ["data" ][SPANDATA .AI_INPUT_MESSAGES ] == _serialize_span_attribute (
666+ messages
667+ )
668+ assert span ["data" ][SPANDATA .AI_RESPONSES ] == _serialize_span_attribute (
669+ [{"type" : "text" , "text" : "" }]
670+ ) # we do not record InputJSONDelta because it could contain PII
659671
660672 else :
661673 assert SPANDATA .AI_INPUT_MESSAGES not in span ["data" ]
@@ -667,6 +679,7 @@ async def test_streaming_create_message_with_input_json_delta_async(
667679 assert span ["data" ]["ai.streaming" ] is True
668680
669681
682+ @pytest .mark .forked
670683def test_exception_message_create (sentry_init , capture_events ):
671684 sentry_init (integrations = [AnthropicIntegration ()], traces_sample_rate = 1.0 )
672685 events = capture_events ()
0 commit comments