Skip to content

Commit 29f6ba2

Browse files
committed
test(anthropic) fixup tests
1 parent 8a0894f commit 29f6ba2

File tree

1 file changed

+60
-34
lines changed

1 file changed

+60
-34
lines changed

tests/integrations/anthropic/test_anthropic.py

Lines changed: 60 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ async def __call__(self, *args, **kwargs):
2020
from anthropic.types.message_delta_event import MessageDeltaEvent
2121
from anthropic.types.message_start_event import MessageStartEvent
2222

23-
from sentry_sdk.integrations.anthropic import _add_ai_data_to_span, _collect_ai_data
23+
from sentry_sdk.integrations.anthropic import _set_output_data, _collect_ai_data
2424
from sentry_sdk.utils import package_version
2525

2626
try:
@@ -117,10 +117,14 @@ def test_nonstreaming_create_message(
117117
assert span["data"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model"
118118

119119
if send_default_pii and include_prompts:
120-
assert span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES] == messages
121-
assert span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT] == [
122-
{"type": "text", "text": "Hi, I'm Claude."}
123-
]
120+
assert (
121+
span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
122+
== '[{"role": "user", "content": "Hello, Claude"}]'
123+
)
124+
assert (
125+
span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT]
126+
== '[{"text": "Hi, I\'m Claude.", "type": "text"}]'
127+
)
124128
else:
125129
assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["data"]
126130
assert SPANDATA.GEN_AI_RESPONSE_TEXT not in span["data"]
@@ -185,10 +189,14 @@ async def test_nonstreaming_create_message_async(
185189
assert span["data"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model"
186190

187191
if send_default_pii and include_prompts:
188-
assert span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES] == messages
189-
assert span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT] == [
190-
{"type": "text", "text": "Hi, I'm Claude."}
191-
]
192+
assert (
193+
span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
194+
== '[{"role": "user", "content": "Hello, Claude"}]'
195+
)
196+
assert (
197+
span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT]
198+
== '[{"text": "Hi, I\'m Claude.", "type": "text"}]'
199+
)
192200
else:
193201
assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["data"]
194202
assert SPANDATA.GEN_AI_RESPONSE_TEXT not in span["data"]
@@ -284,10 +292,14 @@ def test_streaming_create_message(
284292
assert span["data"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model"
285293

286294
if send_default_pii and include_prompts:
287-
assert span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES] == messages
288-
assert span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT] == [
289-
{"type": "text", "text": "Hi! I'm Claude!"}
290-
]
295+
assert (
296+
span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
297+
== '[{"role": "user", "content": "Hello, Claude"}]'
298+
)
299+
assert (
300+
span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT]
301+
== '[{"text": "Hi! I\'m Claude!", "type": "text"}]'
302+
)
291303

292304
else:
293305
assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["data"]
@@ -387,10 +399,14 @@ async def test_streaming_create_message_async(
387399
assert span["data"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model"
388400

389401
if send_default_pii and include_prompts:
390-
assert span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES] == messages
391-
assert span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT] == [
392-
{"type": "text", "text": "Hi! I'm Claude!"}
393-
]
402+
assert (
403+
span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
404+
== '[{"role": "user", "content": "Hello, Claude"}]'
405+
)
406+
assert (
407+
span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT]
408+
== '[{"text": "Hi! I\'m Claude!", "type": "text"}]'
409+
)
394410

395411
else:
396412
assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["data"]
@@ -517,10 +533,14 @@ def test_streaming_create_message_with_input_json_delta(
517533
assert span["data"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model"
518534

519535
if send_default_pii and include_prompts:
520-
assert span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES] == messages
521-
assert span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT] == [
522-
{"text": "{'location': 'San Francisco, CA'}", "type": "text"}
523-
]
536+
assert (
537+
span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
538+
== '[{"role": "user", "content": "What is the weather like in San Francisco?"}]'
539+
)
540+
assert (
541+
span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT]
542+
== '[{"text": "{\'location\': \'San Francisco, CA\'}", "type": "text"}]'
543+
)
524544
else:
525545
assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["data"]
526546
assert SPANDATA.GEN_AI_RESPONSE_TEXT not in span["data"]
@@ -653,10 +673,14 @@ async def test_streaming_create_message_with_input_json_delta_async(
653673
assert span["data"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model"
654674

655675
if send_default_pii and include_prompts:
656-
assert span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES] == messages
657-
assert span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT] == [
658-
{"text": "{'location': 'San Francisco, CA'}", "type": "text"}
659-
]
676+
assert (
677+
span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
678+
== '[{"role": "user", "content": "What is the weather like in San Francisco?"}]'
679+
)
680+
assert (
681+
span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT]
682+
== '[{"text": "{\'location\': \'San Francisco, CA\'}", "type": "text"}]'
683+
)
660684

661685
else:
662686
assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["data"]
@@ -770,15 +794,16 @@ def test_collect_ai_data_with_input_json_delta():
770794
index=0,
771795
type="content_block_delta",
772796
)
773-
797+
model = None
774798
input_tokens = 10
775799
output_tokens = 20
776800
content_blocks = []
777801

778-
new_input_tokens, new_output_tokens, new_content_blocks = _collect_ai_data(
779-
event, input_tokens, output_tokens, content_blocks
802+
model, new_input_tokens, new_output_tokens, new_content_blocks = _collect_ai_data(
803+
event, model, input_tokens, output_tokens, content_blocks
780804
)
781805

806+
assert model is None
782807
assert new_input_tokens == input_tokens
783808
assert new_output_tokens == output_tokens
784809
assert new_content_blocks == ["test"]
@@ -788,7 +813,7 @@ def test_collect_ai_data_with_input_json_delta():
788813
ANTHROPIC_VERSION < (0, 27),
789814
reason="Versions <0.27.0 do not include InputJSONDelta.",
790815
)
791-
def test_add_ai_data_to_span_with_input_json_delta(sentry_init):
816+
def test_set_output_data_with_input_json_delta(sentry_init):
792817
sentry_init(
793818
integrations=[AnthropicIntegration(include_prompts=True)],
794819
traces_sample_rate=1.0,
@@ -799,18 +824,19 @@ def test_add_ai_data_to_span_with_input_json_delta(sentry_init):
799824
span = start_span()
800825
integration = AnthropicIntegration()
801826

802-
_add_ai_data_to_span(
827+
_set_output_data(
803828
span,
804829
integration,
830+
model="",
805831
input_tokens=10,
806832
output_tokens=20,
807833
content_blocks=["{'test': 'data',", "'more': 'json'}"],
808834
)
809835

810-
assert span._data.get("ai.responses") == [
811-
{"type": "text", "text": "{'test': 'data','more': 'json'}"}
812-
]
813-
assert span._data.get(SPANDATA.GEN_AI_RESPONSE_STREAMING) is True
836+
assert (
837+
span._data.get(SPANDATA.GEN_AI_RESPONSE_TEXT)
838+
== "[\"{'test': 'data',\", \"'more': 'json'}\"]"
839+
)
814840
assert span._data.get(SPANDATA.GEN_AI_USAGE_INPUT_TOKENS) == 10
815841
assert span._data.get(SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS) == 20
816842
assert span._data.get(SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS) == 30

0 commit comments

Comments
 (0)