@@ -3125,24 +3125,14 @@ async def test_stream_message_with_input_json_delta_async(
31253125 assert span ["data" ][SPANDATA .GEN_AI_REQUEST_MODEL ] == "model"
31263126
31273127 if send_default_pii and include_prompts :
3128- if stream_gen_ai_spans :
3129- assert (
3130- span ["attributes" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ]
3131- == '[{"role": "user", "content": "What is the weather like in San Francisco?"}]'
3132- )
3133- assert (
3134- span ["attributes" ][SPANDATA .GEN_AI_RESPONSE_TEXT ]
3135- == '{"location": "San Francisco, CA"}'
3136- )
3137- else :
3138- assert (
3139- span ["data" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ]
3140- == '[{"role": "user", "content": "What is the weather like in San Francisco?"}]'
3141- )
3142- assert (
3143- span ["data" ][SPANDATA .GEN_AI_RESPONSE_TEXT ]
3144- == '{"location": "San Francisco, CA"}'
3145- )
3128+ assert (
3129+ span ["data" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ]
3130+ == '[{"role": "user", "content": "What is the weather like in San Francisco?"}]'
3131+ )
3132+ assert (
3133+ span ["data" ][SPANDATA .GEN_AI_RESPONSE_TEXT ]
3134+ == '{"location": "San Francisco, CA"}'
3135+ )
31463136
31473137 else :
31483138 assert SPANDATA .GEN_AI_REQUEST_MESSAGES not in span ["data" ]
@@ -3758,53 +3748,69 @@ async def test_anthropic_message_truncation_async(
37583748
37593749 if stream_gen_ai_spans :
37603750 items = capture_items ("transaction" , "span" )
3761- else :
3762- events = capture_events ()
37633751
3764- with start_transaction ():
3765- await client .messages .create (max_tokens = 1024 , messages = messages , model = "model" )
3752+ with start_transaction ():
3753+ await client .messages .create (
3754+ max_tokens = 1024 , messages = messages , model = "model"
3755+ )
37663756
3767- if stream_gen_ai_spans :
37683757 spans = [item .payload for item in items if item .type == "span" ]
37693758 chat_spans = [
37703759 span
37713760 for span in spans
37723761 if span ["attributes" ].get ("sentry.op" ) == OP .GEN_AI_CHAT
37733762 ]
3763+
3764+ assert len (chat_spans ) > 0
3765+
3766+ chat_span = chat_spans [0 ]
3767+
3768+ assert chat_span ["attributes" ][SPANDATA .GEN_AI_SYSTEM ] == "anthropic"
3769+ assert chat_span ["attributes" ][SPANDATA .GEN_AI_OPERATION_NAME ] == "chat"
3770+ assert SPANDATA .GEN_AI_REQUEST_MESSAGES in chat_span ["attributes" ]
3771+
3772+ messages_data = chat_span ["attributes" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ]
3773+
3774+ assert isinstance (messages_data , str )
3775+
3776+ parsed_messages = json .loads (messages_data )
3777+ assert isinstance (parsed_messages , list )
3778+ assert len (parsed_messages ) == 1
3779+ assert "small message 5" in str (parsed_messages [0 ])
3780+
3781+ tx = next (item .payload for item in items if item .type == "transaction" )
37743782 else :
3783+ events = capture_events ()
3784+
3785+ with start_transaction ():
3786+ await client .messages .create (
3787+ max_tokens = 1024 , messages = messages , model = "model"
3788+ )
3789+
37753790 assert len (events ) > 0
37763791 tx = events [0 ]
37773792 assert tx ["type" ] == "transaction"
37783793
37793794 chat_spans = [
37803795 span for span in tx .get ("spans" , []) if span .get ("op" ) == OP .GEN_AI_CHAT
37813796 ]
3782- assert len (chat_spans ) > 0
37833797
3784- chat_span = chat_spans [0 ]
3785- if stream_gen_ai_spans :
3786- assert chat_span ["attributes" ][SPANDATA .GEN_AI_SYSTEM ] == "anthropic"
3787- assert chat_span ["attributes" ][SPANDATA .GEN_AI_OPERATION_NAME ] == "chat"
3788- assert SPANDATA .GEN_AI_REQUEST_MESSAGES in chat_span ["attributes" ]
3798+ assert len (chat_spans ) > 0
3799+
3800+ chat_span = chat_spans [0 ]
37893801
3790- messages_data = chat_span ["attributes" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ]
3791- else :
37923802 assert chat_span ["data" ][SPANDATA .GEN_AI_SYSTEM ] == "anthropic"
37933803 assert chat_span ["data" ][SPANDATA .GEN_AI_OPERATION_NAME ] == "chat"
37943804 assert SPANDATA .GEN_AI_REQUEST_MESSAGES in chat_span ["data" ]
37953805
37963806 messages_data = chat_span ["data" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ]
3797- assert isinstance (messages_data , str )
3807+ assert isinstance (messages_data , str )
37983808
3799- parsed_messages = json .loads (messages_data )
3800- assert isinstance (parsed_messages , list )
3801- assert len (parsed_messages ) == 1
3802- assert "small message 5" in str (parsed_messages [0 ])
3809+ parsed_messages = json .loads (messages_data )
3810+ assert isinstance (parsed_messages , list )
3811+ assert len (parsed_messages ) == 1
3812+ assert "small message 5" in str (parsed_messages [0 ])
38033813
3804- if stream_gen_ai_spans :
3805- tx = next (item .payload for item in items if item .type == "transaction" )
3806- else :
3807- pass
38083814 assert tx ["_meta" ]["spans" ]["0" ]["data" ]["gen_ai.request.messages" ]["" ]["len" ] == 5
38093815
38103816
@@ -3846,24 +3852,21 @@ def test_nonstreaming_create_message_with_system_prompt(
38463852
38473853 if stream_gen_ai_spans :
38483854 items = capture_items ("transaction" , "span" )
3849- else :
3850- events = capture_events ()
38513855
3852- with start_transaction (name = "anthropic" ):
3853- response = client .messages .create (
3854- max_tokens = 1024 ,
3855- messages = messages ,
3856- model = "model" ,
3857- system = "You are a helpful assistant." ,
3858- )
3856+ with start_transaction (name = "anthropic" ):
3857+ response = client .messages .create (
3858+ max_tokens = 1024 ,
3859+ messages = messages ,
3860+ model = "model" ,
3861+ system = "You are a helpful assistant." ,
3862+ )
38593863
3860- assert response == EXAMPLE_MESSAGE
3861- usage = response .usage
3864+ assert response == EXAMPLE_MESSAGE
3865+ usage = response .usage
38623866
3863- assert usage .input_tokens == 10
3864- assert usage .output_tokens == 20
3867+ assert usage .input_tokens == 10
3868+ assert usage .output_tokens == 20
38653869
3866- if stream_gen_ai_spans :
38673870 (event ,) = (item .payload for item in items if item .type == "transaction" )
38683871 assert event ["transaction" ] == "anthropic"
38693872
@@ -3909,6 +3912,22 @@ def test_nonstreaming_create_message_with_system_prompt(
39093912 "end_turn"
39103913 ]
39113914 else :
3915+ events = capture_events ()
3916+
3917+ with start_transaction (name = "anthropic" ):
3918+ response = client .messages .create (
3919+ max_tokens = 1024 ,
3920+ messages = messages ,
3921+ model = "model" ,
3922+ system = "You are a helpful assistant." ,
3923+ )
3924+
3925+ assert response == EXAMPLE_MESSAGE
3926+ usage = response .usage
3927+
3928+ assert usage .input_tokens == 10
3929+ assert usage .output_tokens == 20
3930+
39123931 assert len (events ) == 1
39133932 (event ,) = events
39143933
@@ -3933,16 +3952,8 @@ def test_nonstreaming_create_message_with_system_prompt(
39333952 {"type" : "text" , "content" : "You are a helpful assistant." }
39343953 ]
39353954
3936- if stream_gen_ai_spans :
3937- assert SPANDATA .GEN_AI_REQUEST_MESSAGES in span ["attributes" ]
3938- stored_messages = json .loads (
3939- span ["attributes" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ]
3940- )
3941- else :
3942- assert SPANDATA .GEN_AI_REQUEST_MESSAGES in span ["data" ]
3943- stored_messages = json .loads (
3944- span ["data" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ]
3945- )
3955+ assert SPANDATA .GEN_AI_REQUEST_MESSAGES in span ["data" ]
3956+ stored_messages = json .loads (span ["data" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ])
39463957 assert len (stored_messages ) == 1
39473958 assert stored_messages [0 ]["role" ] == "user"
39483959 assert stored_messages [0 ]["content" ] == "Hello, Claude"
@@ -3998,24 +4009,21 @@ async def test_nonstreaming_create_message_with_system_prompt_async(
39984009
39994010 if stream_gen_ai_spans :
40004011 items = capture_items ("transaction" , "span" )
4001- else :
4002- events = capture_events ()
40034012
4004- with start_transaction (name = "anthropic" ):
4005- response = await client .messages .create (
4006- max_tokens = 1024 ,
4007- messages = messages ,
4008- model = "model" ,
4009- system = "You are a helpful assistant." ,
4010- )
4013+ with start_transaction (name = "anthropic" ):
4014+ response = await client .messages .create (
4015+ max_tokens = 1024 ,
4016+ messages = messages ,
4017+ model = "model" ,
4018+ system = "You are a helpful assistant." ,
4019+ )
40114020
4012- assert response == EXAMPLE_MESSAGE
4013- usage = response .usage
4021+ assert response == EXAMPLE_MESSAGE
4022+ usage = response .usage
40144023
4015- assert usage .input_tokens == 10
4016- assert usage .output_tokens == 20
4024+ assert usage .input_tokens == 10
4025+ assert usage .output_tokens == 20
40174026
4018- if stream_gen_ai_spans :
40194027 (event ,) = (item .payload for item in items if item .type == "transaction" )
40204028 assert event ["transaction" ] == "anthropic"
40214029
@@ -4061,6 +4069,22 @@ async def test_nonstreaming_create_message_with_system_prompt_async(
40614069 "end_turn"
40624070 ]
40634071 else :
4072+ events = capture_events ()
4073+
4074+ with start_transaction (name = "anthropic" ):
4075+ response = await client .messages .create (
4076+ max_tokens = 1024 ,
4077+ messages = messages ,
4078+ model = "model" ,
4079+ system = "You are a helpful assistant." ,
4080+ )
4081+
4082+ assert response == EXAMPLE_MESSAGE
4083+ usage = response .usage
4084+
4085+ assert usage .input_tokens == 10
4086+ assert usage .output_tokens == 20
4087+
40644088 assert len (events ) == 1
40654089 (event ,) = events
40664090
@@ -4566,24 +4590,21 @@ async def test_stream_message_with_system_prompt_async(
45664590
45674591 if stream_gen_ai_spans :
45684592 items = capture_items ("transaction" , "span" )
4569- else :
4570- events = capture_events ()
45714593
4572- with mock .patch .object (
4573- client ._client ,
4574- "send" ,
4575- return_value = response ,
4576- ) as _ , start_transaction (name = "anthropic" ):
4577- async with client .messages .stream (
4578- max_tokens = 1024 ,
4579- messages = messages ,
4580- model = "model" ,
4581- system = "You are a helpful assistant." ,
4582- ) as stream :
4583- async for event in stream :
4584- pass
4594+ with mock .patch .object (
4595+ client ._client ,
4596+ "send" ,
4597+ return_value = response ,
4598+ ) as _ , start_transaction (name = "anthropic" ):
4599+ async with client .messages .stream (
4600+ max_tokens = 1024 ,
4601+ messages = messages ,
4602+ model = "model" ,
4603+ system = "You are a helpful assistant." ,
4604+ ) as stream :
4605+ async for event in stream :
4606+ pass
45854607
4586- if stream_gen_ai_spans :
45874608 (event ,) = (item .payload for item in items if item .type == "transaction" )
45884609 assert event ["transaction" ] == "anthropic"
45894610
@@ -4626,6 +4647,22 @@ async def test_stream_message_with_system_prompt_async(
46264647 assert span ["attributes" ][SPANDATA .GEN_AI_USAGE_TOTAL_TOKENS ] == 20
46274648 assert span ["attributes" ][SPANDATA .GEN_AI_RESPONSE_STREAMING ] is True
46284649 else :
4650+ events = capture_events ()
4651+
4652+ with mock .patch .object (
4653+ client ._client ,
4654+ "send" ,
4655+ return_value = response ,
4656+ ) as _ , start_transaction (name = "anthropic" ):
4657+ async with client .messages .stream (
4658+ max_tokens = 1024 ,
4659+ messages = messages ,
4660+ model = "model" ,
4661+ system = "You are a helpful assistant." ,
4662+ ) as stream :
4663+ async for event in stream :
4664+ pass
4665+
46294666 assert len (events ) == 1
46304667 (event ,) = events
46314668
@@ -4764,26 +4801,7 @@ async def test_streaming_create_message_with_system_prompt_async(
47644801
47654802 async for _ in message :
47664803 pass
4767- else :
4768- events = capture_events ()
47694804
4770- with mock .patch .object (
4771- client ._client ,
4772- "send" ,
4773- return_value = response ,
4774- ) as _ , start_transaction (name = "anthropic" ):
4775- message = await client .messages .create (
4776- max_tokens = 1024 ,
4777- messages = messages ,
4778- model = "model" ,
4779- stream = True ,
4780- system = "You are a helpful assistant." ,
4781- )
4782-
4783- async for _ in message :
4784- pass
4785-
4786- if stream_gen_ai_spans :
47874805 (event ,) = (item .payload for item in items if item .type == "transaction" )
47884806 assert event ["transaction" ] == "anthropic"
47894807
@@ -4828,6 +4846,24 @@ async def test_streaming_create_message_with_system_prompt_async(
48284846 assert span ["attributes" ][SPANDATA .GEN_AI_USAGE_TOTAL_TOKENS ] == 20
48294847 assert span ["attributes" ][SPANDATA .GEN_AI_RESPONSE_STREAMING ] is True
48304848 else :
4849+ events = capture_events ()
4850+
4851+ with mock .patch .object (
4852+ client ._client ,
4853+ "send" ,
4854+ return_value = response ,
4855+ ) as _ , start_transaction (name = "anthropic" ):
4856+ message = await client .messages .create (
4857+ max_tokens = 1024 ,
4858+ messages = messages ,
4859+ model = "model" ,
4860+ stream = True ,
4861+ system = "You are a helpful assistant." ,
4862+ )
4863+
4864+ async for _ in message :
4865+ pass
4866+
48314867 assert len (events ) == 1
48324868 (event ,) = events
48334869
0 commit comments