1
+ import pytest
1
2
from unittest import mock
2
3
3
-
4
4
try :
5
5
from unittest .mock import AsyncMock
6
6
except ImportError :
@@ -10,7 +10,6 @@ async def __call__(self, *args, **kwargs):
10
10
return super (AsyncMock , self ).__call__ (* args , ** kwargs )
11
11
12
12
13
- import pytest
14
13
from anthropic import Anthropic , AnthropicError , AsyncAnthropic , AsyncStream , Stream
15
14
from anthropic .types import MessageDeltaUsage , TextDelta , Usage
16
15
from anthropic .types .content_block_delta_event import ContentBlockDeltaEvent
@@ -20,9 +19,6 @@ async def __call__(self, *args, **kwargs):
20
19
from anthropic .types .message_delta_event import MessageDeltaEvent
21
20
from anthropic .types .message_start_event import MessageStartEvent
22
21
23
- from sentry_sdk .integrations .anthropic import _set_output_data , _collect_ai_data
24
- from sentry_sdk .utils import package_version
25
-
26
22
try :
27
23
from anthropic .types import InputJSONDelta
28
24
except ImportError :
@@ -46,9 +42,16 @@ async def __call__(self, *args, **kwargs):
46
42
47
43
from sentry_sdk import start_transaction , start_span
48
44
from sentry_sdk .consts import OP , SPANDATA
49
- from sentry_sdk .integrations .anthropic import AnthropicIntegration
45
+ from sentry_sdk .integrations .anthropic import (
46
+ AnthropicIntegration ,
47
+ _set_output_data ,
48
+ _collect_ai_data ,
49
+ )
50
+ from sentry_sdk .utils import package_version
51
+
50
52
51
53
ANTHROPIC_VERSION = package_version ("anthropic" )
54
+
52
55
EXAMPLE_MESSAGE = Message (
53
56
id = "id" ,
54
57
model = "model" ,
@@ -121,10 +124,7 @@ def test_nonstreaming_create_message(
121
124
span ["data" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ]
122
125
== '[{"role": "user", "content": "Hello, Claude"}]'
123
126
)
124
- assert (
125
- span ["data" ][SPANDATA .GEN_AI_RESPONSE_TEXT ]
126
- == '[{"text": "Hi, I\' m Claude.", "type": "text"}]'
127
- )
127
+ assert span ["data" ][SPANDATA .GEN_AI_RESPONSE_TEXT ] == "Hi, I'm Claude."
128
128
else :
129
129
assert SPANDATA .GEN_AI_REQUEST_MESSAGES not in span ["data" ]
130
130
assert SPANDATA .GEN_AI_RESPONSE_TEXT not in span ["data" ]
@@ -193,10 +193,7 @@ async def test_nonstreaming_create_message_async(
193
193
span ["data" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ]
194
194
== '[{"role": "user", "content": "Hello, Claude"}]'
195
195
)
196
- assert (
197
- span ["data" ][SPANDATA .GEN_AI_RESPONSE_TEXT ]
198
- == '[{"text": "Hi, I\' m Claude.", "type": "text"}]'
199
- )
196
+ assert span ["data" ][SPANDATA .GEN_AI_RESPONSE_TEXT ] == "Hi, I'm Claude."
200
197
else :
201
198
assert SPANDATA .GEN_AI_REQUEST_MESSAGES not in span ["data" ]
202
199
assert SPANDATA .GEN_AI_RESPONSE_TEXT not in span ["data" ]
@@ -296,10 +293,7 @@ def test_streaming_create_message(
296
293
span ["data" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ]
297
294
== '[{"role": "user", "content": "Hello, Claude"}]'
298
295
)
299
- assert (
300
- span ["data" ][SPANDATA .GEN_AI_RESPONSE_TEXT ]
301
- == '[{"text": "Hi! I\' m Claude!", "type": "text"}]'
302
- )
296
+ assert span ["data" ][SPANDATA .GEN_AI_RESPONSE_TEXT ] == "Hi! I'm Claude!"
303
297
304
298
else :
305
299
assert SPANDATA .GEN_AI_REQUEST_MESSAGES not in span ["data" ]
@@ -403,10 +397,7 @@ async def test_streaming_create_message_async(
403
397
span ["data" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ]
404
398
== '[{"role": "user", "content": "Hello, Claude"}]'
405
399
)
406
- assert (
407
- span ["data" ][SPANDATA .GEN_AI_RESPONSE_TEXT ]
408
- == '[{"text": "Hi! I\' m Claude!", "type": "text"}]'
409
- )
400
+ assert span ["data" ][SPANDATA .GEN_AI_RESPONSE_TEXT ] == "Hi! I'm Claude!"
410
401
411
402
else :
412
403
assert SPANDATA .GEN_AI_REQUEST_MESSAGES not in span ["data" ]
@@ -539,7 +530,7 @@ def test_streaming_create_message_with_input_json_delta(
539
530
)
540
531
assert (
541
532
span ["data" ][SPANDATA .GEN_AI_RESPONSE_TEXT ]
542
- == '[{"text": "{ \ ' location\ ' : \ ' San Francisco, CA\ ' }", "type": "text"}]'
533
+ == "{ 'location': 'San Francisco, CA'}"
543
534
)
544
535
else :
545
536
assert SPANDATA .GEN_AI_REQUEST_MESSAGES not in span ["data" ]
@@ -679,7 +670,7 @@ async def test_streaming_create_message_with_input_json_delta_async(
679
670
)
680
671
assert (
681
672
span ["data" ][SPANDATA .GEN_AI_RESPONSE_TEXT ]
682
- == '[{"text": "{ \ ' location\ ' : \ ' San Francisco, CA\ ' }", "type": "text"}]'
673
+ == "{ 'location': 'San Francisco, CA'}"
683
674
)
684
675
685
676
else :
@@ -835,7 +826,7 @@ def test_set_output_data_with_input_json_delta(sentry_init):
835
826
836
827
assert (
837
828
span ._data .get (SPANDATA .GEN_AI_RESPONSE_TEXT )
838
- == "[{ \" text \" : \" { 'test': 'data','more': 'json'}\" , \" type \" : \" text \" }] "
829
+ == "{ 'test': 'data','more': 'json'}"
839
830
)
840
831
assert span ._data .get (SPANDATA .GEN_AI_USAGE_INPUT_TOKENS ) == 10
841
832
assert span ._data .get (SPANDATA .GEN_AI_USAGE_OUTPUT_TOKENS ) == 20
0 commit comments