8000 Make Anthropic integration work with Potel (#3686) · getsentry/sentry-python@0ffc5cd · GitHub
[go: up one dir, main page]

Skip to content

Commit 0ffc5cd

Browse files
authored
Make Anthropic integration work with Potel (#3686)
1 parent e7218da commit 0ffc5cd

File tree

2 files changed

+47
-28
lines changed

2 files changed

+47
-28
lines changed

sentry_sdk/integrations/anthropic.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from sentry_sdk.integrations import DidNotEnable, Integration
88
from sentry_sdk.scope import should_send_default_pii
99
from sentry_sdk.utils import (
10+
_serialize_span_attribute,
1011
capture_internal_exceptions,
1112
event_from_exception,
1213
package_version,
@@ -126,7 +127,7 @@ def _add_ai_data_to_span(
126127
complete_message = "".join(content_blocks)
127128
span.set_data(
128129
SPANDATA.AI_RESPONSES,
129-
[{"type": "text", "text": complete_message}],
130+
_serialize_span_attribute([{"type": "text", "text": complete_message}]),
130131
)
131132
total_tokens = input_tokens + output_tokens
132133
record_token_usage(span, input_tokens, output_tokens, total_tokens)
@@ -165,11 +166,16 @@ def _sentry_patched_create_common(f, *args, **kwargs):
165166
span.set_data(SPANDATA.AI_STREAMING, False)
166167

167168
if should_send_default_pii() and integration.include_prompts:
168-
span.set_data(SPANDATA.AI_INPUT_MESSAGES, messages)
169+
span.set_data(
170+
SPANDATA.AI_INPUT_MESSAGES, _serialize_span_attribute(messages)
171+
)
169172

170173
if hasattr(result, "content"):
171174
if should_send_default_pii() and integration.include_prompts:
172-
span.set_data(SPANDATA.AI_RESPONSES, _get_responses(result.content))
175+
span.set_data(
176+
SPANDATA.AI_RESPONSES,
177+
_serialize_span_attribute(_get_responses(result.content)),
178+
)
173179
_calculate_token_usage(result, span)
174180
span.__exit__(None, None, None)
175181

tests/integrations/anthropic/test_anthropic.py

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +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.utils import package_version
22+
from sentry_sdk.utils import _serialize_span_attribute, package_version
2323

2424
try:
2525
from anthropic.types import InputJSONDelta
@@ -115,10 +115,12 @@ def test_nonstreaming_create_message(
115115
assert span["data"][SPANDATA.AI_MODEL_ID] == "model"
116116

117117
if send_default_pii and include_prompts:
118-
assert span["data"][SPANDATA.AI_INPUT_MESSAGES] == messages
119-
assert span["data"][SPANDATA.AI_RESPONSES] == [
120-
{"type": "text", "text": "Hi, I'm Claude."}
121-
]
118+
assert span["data"][SPANDATA.AI_INPUT_MESSAGES] == _serialize_span_attribute(
119+
messages
120+
)
121+
assert span["data"][SPANDATA.AI_RESPONSES] == _serialize_span_attribute(
122+
[{"type": "text", "text": "Hi, I'm Claude."}]
123+
)
122124
else:
123125
assert SPANDATA.AI_INPUT_MESSAGES not in span["data"]
124126
assert SPANDATA.AI_RESPONSES not in span["data"]
@@ -183,10 +185,12 @@ async def test_nonstreaming_create_message_async(
183185
assert span["data"][SPANDATA.AI_MODEL_ID] == "model"
184186

185187
if send_default_pii and include_prompts:
186-
assert span["data"][SPANDATA.AI_INPUT_MESSAGES] == messages
187-
assert span["data"][SPANDATA.AI_RESPONSES] == [
188-
{"type": "text", "text": "Hi, I'm Claude."}
189-
]
188+
assert span["data"][SPANDATA.AI_INPUT_MESSAGES] == _serialize_span_attribute(
189+
messages
190+
)
191+
assert span["data"][SPANDATA.AI_RESPONSES] == _serialize_span_attribute(
192+
[{"type": "text", "text": "Hi, I'm Claude."}]
193+
)
190194
else:
191195
assert SPANDATA.AI_INPUT_MESSAGES not in span["data"]
192196
assert SPANDATA.AI_RESPONSES not in span["data"]
@@ -282,10 +286,12 @@ def test_streaming_create_message(
282286
assert span["data"][SPANDATA.AI_MODEL_ID] == "model"
283287

284288
if send_default_pii and include_prompts:
285-
assert span["data"][SPANDATA.AI_INPUT_MESSAGES] == messages
286-
assert span["data"][SPANDATA.AI_RESPONSES] == [
287-
{"type": "text", "text": "Hi! I'm Claude!"}
288-
]
289+
assert span["data"][SPANDATA.AI_INPUT_MESSAGES] == _serialize_span_attribute(
290+
messages
291+
)
292+
assert span["data"][SPANDATA.AI_RESPONSES] == _serialize_span_attribute(
293+
[{"type": "text", "text": "Hi! I'm Claude!"}]
294+
)
289295

290296
else:
291297
assert SPANDATA.AI_INPUT_MESSAGES not in span["data"]
@@ -385,10 +391,12 @@ async def test_streaming_create_message_async(
385391
assert span["data"][SPANDATA.AI_MODEL_ID] == "model"
386392

387393
if send_default_pii and include_prompts:
388-
assert span["data"][SPANDATA.AI_INPUT_MESSAGES] == messages
389-
assert span["data"][SPANDATA.AI_RESPONSES] == [
390-
{"type": "text", "text": "Hi! I'm Claude!"}
391-
]
394+
assert span["data"][SPANDATA.AI_INPUT_MESSAGES] == _serialize_span_attribute(
395+
messages
396+
)
397+
assert span["data"][SPANDATA.AI_RESPONSES] == _serialize_span_attribute(
398+
[{"type": "text", "text": "Hi! I'm Claude!"}]
399+
)
392400

393401
else:
394402
assert SPANDATA.AI_INPUT_MESSAGES not in span["data"]
@@ -515,10 +523,12 @@ def test_streaming_create_message_with_input_json_delta(
515523
assert span["data"][SPANDATA.AI_MODEL_ID] == "model"
516524

517525
if send_default_pii and include_prompts:
518-
assert span["data"][SPANDATA.AI_INPUT_MESSAGES] == messages
519-
assert span["data"][SPANDATA.AI_RESPONSES] == [
520-
{"text": "", "type": "text"}
521-
] # we do not record InputJSONDelta because it could contain PII
526+
assert span["data"][SPANDATA.AI_INPUT_MESSAGES] == _serialize_span_attribute(
527+
messages
528+
)
529+
assert span["data"][SPANDATA.AI_RESPONSES] == _serialize_span_attribute(
530+
[{"type": "text", "text": ""}]
531+
) # we do not record InputJSONDelta because it could contain PII
522532

523533
else:
524534
assert SPANDATA.AI_INPUT_MESSAGES not in span["data"]
@@ -652,10 +662,12 @@ async def test_streaming_create_message_with_input_json_delta_async(
652662
assert span["data"][SPANDATA.AI_MODEL_ID] == "model"
653663

654664
if send_default_pii and include_prompts:
655-
assert span["data"][SPANDATA.AI_INPUT_MESSAGES] == messages
656-
assert span["data"][SPANDATA.AI_RESPONSES] == [
657-
{"text": "", "type": "text"}
658-
] # we do not record InputJSONDelta because it could contain PII
665+
assert span["data"][SPANDATA.AI_INPUT_MESSAGES] == _serialize_span_attribute(
666+
messages
667+
)
668+
assert span["data"][SPANDATA.AI_RESPONSES] == _serialize_span_attribute(
669+
[{"type": "text", "text": ""}]
670+
) # we do not record InputJSONDelta because it could contain PII
659671

660672
else:
661673
assert SPANDATA.AI_INPUT_MESSAGES not in span["data"]
@@ -667,6 +679,7 @@ async def test_streaming_create_message_with_input_json_delta_async(
667679
assert span["data"]["ai.streaming"] is True
668680

669681

682+
@pytest.mark.forked
670683
def test_exception_message_create(sentry_init, capture_events):
671684
sentry_init(integrations=[AnthropicIntegration()], traces_sample_rate=1.0)
672685
events = capture_events()

0 commit comments

Comments
 (0)
0