Skip to content

Commit 8cc1ef3

Browse files
committed
add more tests around collecting ai data
1 parent 576bb7d commit 8cc1ef3

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

tests/integrations/anthropic/test_anthropic.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ async def __call__(self, *args, **kwargs):
1010

1111

1212
import pytest
13-
from anthropic import AsyncAnthropic, Anthropic, AnthropicError, AsyncStream, Stream
13+
from anthropic import Anthropic, AnthropicError, AsyncAnthropic, AsyncStream, Stream
1414
from anthropic.types import MessageDeltaUsage, TextDelta, Usage
1515
from anthropic.types.content_block_delta_event import ContentBlockDeltaEvent
1616
from anthropic.types.content_block_start_event import ContentBlockStartEvent
@@ -19,6 +19,7 @@ async def __call__(self, *args, **kwargs):
1919
from anthropic.types.message_delta_event import MessageDeltaEvent
2020
from anthropic.types.message_start_event import MessageStartEvent
2121

22+
from sentry_sdk.integrations.anthropic import _add_ai_data_to_span, _collect_ai_data
2223
from sentry_sdk.utils import package_version
2324

2425
try:
@@ -757,3 +758,59 @@ async def test_span_origin_async(sentry_init, capture_events):
757758

758759
assert event["contexts"]["trace"]["origin"] == "manual"
759760
assert event["spans"][0]["origin"] == "auto.ai.anthropic"
761+
762+
763+
@pytest.mark.skipif(
764+
ANTHROPIC_VERSION < (0, 27),
765+
reason="Versions <0.27.0 do not include InputJSONDelta.",
766+
)
767+
def test_collect_ai_data_with_input_json_delta():
768+
event = ContentBlockDeltaEvent(
769+
delta=InputJSONDelta(partial_json="test", type="input_json_delta"),
770+
index=0,
771+
type="content_block_delta",
772+
)
773+
774+
input_tokens = 10
775+
output_tokens = 20
776+
content_blocks = []
777+
778+
new_input_tokens, new_output_tokens, new_content_blocks = _collect_ai_data(
779+
event, input_tokens, output_tokens, content_blocks
780+
)
781+
782+
assert new_input_tokens == input_tokens
783+
assert new_output_tokens == output_tokens
784+
assert new_content_blocks == ["test"]
785+
786+
787+
@pytest.mark.skipif(
788+
ANTHROPIC_VERSION < (0, 27),
789+
reason="Versions <0.27.0 do not include InputJSONDelta.",
790+
)
791+
def test_add_ai_data_to_span_with_input_json_delta(sentry_init):
792+
sentry_init(
793+
integrations=[AnthropicIntegration(include_prompts=True)],
794+
traces_sample_rate=1.0,
795+
send_default_pii=True,
796+
)
797+
798+
with start_transaction(name="test") as transaction:
799+
span = transaction.start_span()
800+
integration = AnthropicIntegration()
801+
802+
_add_ai_data_to_span(
803+
span,
804+
integration,
805+
input_tokens=10,
806+
output_tokens=20,
807+
content_blocks=["{'test': 'data'}", "'more': 'json'"],
808+
)
809+
810+
assert span.get_data(SPANDATA.AI_RESPONSES) == [
811+
{"type": "text", "text": "{'test': 'data''more': 'json'}"}
812+
]
813+
assert span.get_data("ai.streaming") is True
814+
assert span.get_measurement("ai_prompt_tokens_used")["value"] == 10
815+
assert span.get_measurement("ai_completion_tokens_used")["value"] == 20
816+
assert span.get_measurement("ai_total_tokens_used")["value"] == 30

0 commit comments

Comments
 (0)