1+ import pytest
12from unittest import mock
23
3-
44try :
55 from unittest .mock import AsyncMock
66except ImportError :
@@ -10,7 +10,6 @@ async def __call__(self, *args, **kwargs):
1010 return super (AsyncMock , self ).__call__ (* args , ** kwargs )
1111
1212
13- import pytest
1413from anthropic import Anthropic , AnthropicError , AsyncAnthropic , AsyncStream , Stream
1514from anthropic .types import MessageDeltaUsage , TextDelta , Usage
1615from anthropic .types .content_block_delta_event import ContentBlockDeltaEvent
@@ -20,9 +19,6 @@ async def __call__(self, *args, **kwargs):
2019from anthropic .types .message_delta_event import MessageDeltaEvent
2120from anthropic .types .message_start_event import MessageStartEvent
2221
23- from sentry_sdk .integrations .anthropic import _set_output_data , _collect_ai_data
24- from sentry_sdk .utils import package_version
25-
2622try :
2723 from anthropic .types import InputJSONDelta
2824except ImportError :
@@ -46,9 +42,16 @@ async def __call__(self, *args, **kwargs):
4642
4743from sentry_sdk import start_transaction , start_span
4844from 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+
5052
5153ANTHROPIC_VERSION = package_version ("anthropic" )
54+
5255EXAMPLE_MESSAGE = Message (
5356 id = "id" ,
5457 model = "model" ,
@@ -121,10 +124,7 @@ def test_nonstreaming_create_message(
121124 span ["data" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ]
122125 == '[{"role": "user", "content": "Hello, Claude"}]'
123126 )
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."
128128 else :
129129 assert SPANDATA .GEN_AI_REQUEST_MESSAGES not in span ["data" ]
130130 assert SPANDATA .GEN_AI_RESPONSE_TEXT not in span ["data" ]
@@ -193,10 +193,7 @@ async def test_nonstreaming_create_message_async(
193193 span ["data" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ]
194194 == '[{"role": "user", "content": "Hello, Claude"}]'
195195 )
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."
200197 else :
201198 assert SPANDATA .GEN_AI_REQUEST_MESSAGES not in span ["data" ]
202199 assert SPANDATA .GEN_AI_RESPONSE_TEXT not in span ["data" ]
@@ -296,10 +293,7 @@ def test_streaming_create_message(
296293 span ["data" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ]
297294 == '[{"role": "user", "content": "Hello, Claude"}]'
298295 )
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!"
303297
304298 else :
305299 assert SPANDATA .GEN_AI_REQUEST_MESSAGES not in span ["data" ]
@@ -403,10 +397,7 @@ async def test_streaming_create_message_async(
403397 span ["data" ][SPANDATA .GEN_AI_REQUEST_MESSAGES ]
404398 == '[{"role": "user", "content": "Hello, Claude"}]'
405399 )
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!"
410401
411402 else :
412403 assert SPANDATA .GEN_AI_REQUEST_MESSAGES not in span ["data" ]
@@ -539,7 +530,7 @@ def test_streaming_create_message_with_input_json_delta(
539530 )
540531 assert (
541532 span ["data" ][SPANDATA .GEN_AI_RESPONSE_TEXT ]
542- == '[{"text": "{ \ ' location\ ' : \ ' San Francisco, CA\ ' }", "type": "text"}]'
533+ == "{ 'location': 'San Francisco, CA'}"
543534 )
544535 else :
545536 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(
679670 )
680671 assert (
681672 span ["data" ][SPANDATA .GEN_AI_RESPONSE_TEXT ]
682- == '[{"text": "{ \ ' location\ ' : \ ' San Francisco, CA\ ' }", "type": "text"}]'
673+ == "{ 'location': 'San Francisco, CA'}"
683674 )
684675
685676 else :
@@ -835,7 +826,7 @@ def test_set_output_data_with_input_json_delta(sentry_init):
835826
836827 assert (
837828 span ._data .get (SPANDATA .GEN_AI_RESPONSE_TEXT )
838- == "[{ \" text \" : \" { 'test': 'data','more': 'json'}\" , \" type \" : \" text \" }] "
829+ == "{ 'test': 'data','more': 'json'}"
839830 )
840831 assert span ._data .get (SPANDATA .GEN_AI_USAGE_INPUT_TOKENS ) == 10
841832 assert span ._data .get (SPANDATA .GEN_AI_USAGE_OUTPUT_TOKENS ) == 20
0 commit comments