@@ -364,17 +364,13 @@ def test_stream_messages(
364364 ]
365365 )
366366 )
367- returned_stream = Stream (
368- cast_to = MessageStreamEvent , response = response , client = client
369- )
370367
371368 sentry_init (
372369 integrations = [AnthropicIntegration (include_prompts = include_prompts )],
373370 traces_sample_rate = 1.0 ,
374371 send_default_pii = send_default_pii ,
375372 )
376373 events = capture_events ()
377- client .messages ._post = mock .Mock (return_value = returned_stream )
378374
379375 messages = [
380376 {
@@ -383,23 +379,27 @@ def test_stream_messages(
383379 }
384380 ]
385381
386- with start_transaction (name = "anthropic" ):
387- with client .messages .stream (
388- max_tokens = 1024 ,
389- messages = messages ,
390- model = "model" ,
391- ) as stream :
392- for event in stream :
393- pass
382+ with mock .patch .object (
383+ client ._client ,
384+ "send" ,
385+ return_value = response ,
386+ ) as _ :
387+ with start_transaction (name = "anthropic" ):
388+ with client .messages .stream (
389+ max_tokens = 1024 ,
390+ messages = messages ,
391+ model = "model" ,
392+ ) as stream :
393+ for event in stream :
394+ pass
394395
395396 assert len (events ) == 1
396397 (event ,) = events
397398
398399 assert event ["type" ] == "transaction"
399400 assert event ["transaction" ] == "anthropic"
400401
401- assert len (event ["spans" ]) == 1
402- (span ,) = event ["spans" ]
402+ span = next (span for span in event ["spans" ] if span ["op" ] == OP .GEN_AI_CHAT )
403403
404404 assert span ["op" ] == OP .GEN_AI_CHAT
405405 assert span ["description" ] == "chat model"
@@ -864,17 +864,13 @@ def test_stream_messages_with_input_json_delta(
864864 ]
865865 )
866866 )
867- returned_stream = Stream (
868- cast_to = MessageStreamEvent , response = response , client = client
869- )
870867
871868 sentry_init (
872869 integrations = [AnthropicIntegration (include_prompts = include_prompts )],
873870 traces_sample_rate = 1.0 ,
874871 send_default_pii = send_default_pii ,
875872 )
876873 events = capture_events ()
877- client .messages ._post = mock .Mock (return_value = returned_stream )
878874
879875 messages = [
880876 {
@@ -883,14 +879,19 @@ def test_stream_messages_with_input_json_delta(
883879 }
884880 ]
885881
886- with start_transaction (name = "anthropic" ):
887- with client .messages .stream (
888- max_tokens = 1024 ,
889- messages = messages ,
890- model = "model" ,
891- ) as stream :
892- for event in stream :
893- pass
882+ with mock .patch .object (
883+ client ._client ,
884+ "send" ,
885+ return_value = response ,
886+ ) as _ :
887+ with start_transaction (name = "anthropic" ):
888+ with client .messages .stream (
889+ max_tokens = 1024 ,
890+ messages = messages ,
891+ model = "model" ,
892+ ) as stream :
893+ for event in stream :
894+ pass
894895
895896 assert len (events ) == 1
896897 (event ,) = events
@@ -1932,17 +1933,13 @@ def test_stream_messages_with_system_prompt(
19321933 ]
19331934 )
19341935 )
1935- returned_stream = Stream (
1936- cast_to = MessageStreamEvent , response = response , client = client
1937- )
19381936
19391937 sentry_init (
19401938 integrations = [AnthropicIntegration (include_prompts = include_prompts )],
19411939 traces_sample_rate = 1.0 ,
19421940 send_default_pii = send_default_pii ,
19431941 )
19441942 events = capture_events ()
1945- client .messages ._post = mock .Mock (return_value = returned_stream )
19461943
19471944 messages = [
19481945 {
@@ -1951,15 +1948,20 @@ def test_stream_messages_with_system_prompt(
19511948 }
19521949 ]
19531950
1954- with start_transaction (name = "anthropic" ):
1955- with client .messages .stream (
1956- max_tokens = 1024 ,
1957- messages = messages ,
1958- model = "model" ,
1959- system = "You are a helpful assistant." ,
1960- ) as stream :
1961- for event in stream :
1962- pass
1951+ with mock .patch .object (
1952+ client ._client ,
1953+ "send" ,
1954+ return_value = response ,
1955+ ) as _ :
1956+ with start_transaction (name = "anthropic" ):
1957+ with client .messages .stream (
1958+ max_tokens = 1024 ,
1959+ messages = messages ,
1960+ model = "model" ,
1961+ system = "You are a helpful assistant." ,
1962+ ) as stream :
1963+ for event in stream :
1964+ pass
19631965
19641966 assert len (events ) == 1
19651967 (event ,) = events
@@ -3235,22 +3237,23 @@ def test_stream_messages_input_tokens_include_cache_read_streaming(
32353237 ]
32363238 )
32373239 )
3238- returned_stream = Stream (
3239- cast_to = MessageStreamEvent , response = response , client = client
3240- )
32413240
32423241 sentry_init (integrations = [AnthropicIntegration ()], traces_sample_rate = 1.0 )
32433242 events = capture_events ()
3244- client .messages ._post = mock .Mock (return_value = returned_stream )
32453243
3246- with start_transaction (name = "anthropic" ):
3247- with client .messages .stream (
3248- max_tokens = 1024 ,
3249- messages = [{"role" : "user" , "content" : "What is 5+5?" }],
3250- model = "claude-sonnet-4-20250514" ,
3251- ) as stream :
3252- for event in stream :
3253- pass
3244+ with mock .patch .object (
3245+ client ._client ,
3246+ "send" ,
3247+ return_value = response ,
3248+ ) as _ :
3249+ with start_transaction (name = "anthropic" ):
3250+ with client .messages .stream (
3251+ max_tokens = 1024 ,
3252+ messages = [{"role" : "user" , "content" : "What is 5+5?" }],
3253+ model = "claude-sonnet-4-20250514" ,
3254+ ) as stream :
3255+ for event in stream :
3256+ pass
32543257
32553258 (span ,) = events [0 ]["spans" ]
32563259
@@ -3382,22 +3385,23 @@ def test_stream_messages_cache_tokens(
33823385 ]
33833386 )
33843387 )
3385- returned_stream = Stream (
3386- cast_to = MessageStreamEvent , response = response , client = client
3387- )
33883388
33893389 sentry_init (integrations = [AnthropicIntegration ()], traces_sample_rate = 1.0 )
33903390 events = capture_events ()
3391- client .messages ._post = mock .Mock (return_value = returned_stream )
33923391
3393- with start_transaction (name = "anthropic" ):
3394- with client .messages .stream (
3395- max_tokens = 1024 ,
3396- messages = [{"role" : "user" , "content" : "Hello" }],
3397- model = "claude-3-5-sonnet-20241022" ,
3398- ) as stream :
3399- for event in stream :
3400- pass
3392+ with mock .patch .object (
3393+ client ._client ,
3394+ "send" ,
3395+ return_value = response ,
3396+ ) as _ :
3397+ with start_transaction (name = "anthropic" ):
3398+ with client .messages .stream (
3399+ max_tokens = 1024 ,
3400+ messages = [{"role" : "user" , "content" : "Hello" }],
3401+ model = "claude-3-5-sonnet-20241022" ,
3402+ ) as stream :
3403+ for event in stream :
3404+ pass
34013405
34023406 (span ,) = events [0 ]["spans" ]
34033407 # input_tokens normalized: 100 + 80 (cache_read) + 20 (cache_write) = 200
0 commit comments