Skip to content

Commit e1804a8

Browse files
committed
Serialize dict like attributes
1 parent 7422a4c commit e1804a8

File tree

2 files changed

+31
-29
lines changed

2 files changed

+31
-29
lines changed

sentry_sdk/integrations/anthropic.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from functools import wraps
2+
import json
23
from typing import TYPE_CHECKING
34

45
import sentry_sdk
@@ -74,9 +75,9 @@ def _calculate_token_usage(result, span):
7475

7576

7677
def _get_responses(content):
77-
# type: (list[Any]) -> list[dict[str, Any]]
78+
# type: (list[Any]) -> str
7879
"""
79-
Get JSON of a Anthropic responses.
80+
Get Anthropic responses as serialized JSON.
8081
"""
8182
responses = []
8283
for item in content:
@@ -87,7 +88,7 @@ def _get_responses(content):
8788
"text": item.text,
8889
}
8990
)
90-
return responses
91+
return json.dumps(responses)
9192

9293

9394
def _collect_ai_data(event, input_tokens, output_tokens, content_blocks):
@@ -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+
json.dumps([{"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,7 +166,7 @@ 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(SPANDATA.AI_INPUT_MESSAGES, json.dumps(messages))
169170

170171
if hasattr(result, "content"):
171172
if should_send_default_pii() and integration.include_prompts:

tests/integrations/anthropic/test_anthropic.py

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
from unittest import mock
23

34
try:
@@ -115,10 +116,10 @@ def test_nonstreaming_create_message(
115116
assert span["data"][SPANDATA.AI_MODEL_ID] == "model"
116117

117118
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-
]
119+
assert span["data"][SPANDATA.AI_INPUT_MESSAGES] == json.dumps(messages)
120+
assert span["data"][SPANDATA.AI_RESPONSES] == json.dumps(
121+
[{"type": "text", "text": "Hi, I'm Claude."}]
122+
)
122123
else:
123124
assert SPANDATA.AI_INPUT_MESSAGES not in span["data"]
124125
assert SPANDATA.AI_RESPONSES not in span["data"]
@@ -183,10 +184,10 @@ async def test_nonstreaming_create_message_async(
183184
assert span["data"][SPANDATA.AI_MODEL_ID] == "model"
184185

185186
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-
]
187+
assert span["data"][SPANDATA.AI_INPUT_MESSAGES] == json.dumps(messages)
188+
assert span["data"][SPANDATA.AI_RESPONSES] == json.dumps(
189+
[{"type": "text", "text": "Hi, I'm Claude."}]
190+
)
190191
else:
191192
assert SPANDATA.AI_INPUT_MESSAGES not in span["data"]
192193
assert SPANDATA.AI_RESPONSES not in span["data"]
@@ -282,10 +283,10 @@ def test_streaming_create_message(
282283
assert span["data"][SPANDATA.AI_MODEL_ID] == "model"
283284

284285
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-
]
286+
assert span["data"][SPANDATA.AI_INPUT_MESSAGES] == json.dumps(messages)
287+
assert span["data"][SPANDATA.AI_RESPONSES] == json.dumps(
288+
[{"type": "text", "text": "Hi! I'm Claude!"}]
289+
)
289290

290291
else:
291292
assert SPANDATA.AI_INPUT_MESSAGES not in span["data"]
@@ -385,10 +386,10 @@ async def test_streaming_create_message_async(
385386
assert span["data"][SPANDATA.AI_MODEL_ID] == "model"
386387

387388
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-
]
389+
assert span["data"][SPANDATA.AI_INPUT_MESSAGES] == json.dumps(messages)
390+
assert span["data"][SPANDATA.AI_RESPONSES] == json.dumps(
391+
[{"type": "text", "text": "Hi! I'm Claude!"}]
392+
)
392393

393394
else:
394395
assert SPANDATA.AI_INPUT_MESSAGES not in span["data"]
@@ -515,10 +516,10 @@ def test_streaming_create_message_with_input_json_delta(
515516
assert span["data"][SPANDATA.AI_MODEL_ID] == "model"
516517

517518
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
519+
assert span["data"][SPANDATA.AI_INPUT_MESSAGES] == json.dumps(messages)
520+
assert span["data"][SPANDATA.AI_RESPONSES] == json.dumps(
521+
[{"type": "text", "text": ""}]
522+
) # we do not record InputJSONDelta because it could contain PII
522523

523524
else:
524525
assert SPANDATA.AI_INPUT_MESSAGES not in span["data"]
@@ -652,10 +653,10 @@ async def test_streaming_create_message_with_input_json_delta_async(
652653
assert span["data"][SPANDATA.AI_MODEL_ID] == "model"
653654

654655
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
656+
assert span["data"][SPANDATA.AI_INPUT_MESSAGES] == json.dumps(messages)
657+
assert span["data"][SPANDATA.AI_RESPONSES] == json.dumps(
658+
[{"type": "text", "text": ""}]
659+
) # we do not record InputJSONDelta because it could contain PII
659660

660661
else:
661662
assert SPANDATA.AI_INPUT_MESSAGES not in span["data"]

0 commit comments

Comments
 (0)