Skip to content

Commit 886ef2a

Browse files
fix(ai): Truncate the parts of a single gen_ai message in the input messages format
`truncate_and_annotate_messages()` only truncated a top-level `content` field, so a message in the `gen_ai.input.messages` format, which keeps its text in `parts`, such as Bedrock's, was kept in full. Truncate its text parts with the same shared character limit.
1 parent 1197d69 commit 886ef2a

2 files changed

Lines changed: 57 additions & 19 deletions

File tree

‎sentry_sdk/ai/utils.py‎

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -552,8 +552,15 @@ def _truncate_single_message_content_if_present(
552552
Truncate a message's content to at most `max_chars` characters and append an
553553
ellipsis if truncation occurs.
554554
"""
555-
if not isinstance(message, dict) or "content" not in message:
555+
if not isinstance(message, dict):
556556
return message
557+
558+
if "content" not in message:
559+
# messages in the `gen_ai.input.messages` format keep their text in `parts`.
560+
if isinstance(message.get("parts"), list):
561+
_truncate_text_items(message["parts"], "content", max_chars)
562+
return message
563+
557564
content = message["content"]
558565

559566
if isinstance(content, str):
@@ -563,21 +570,28 @@ def _truncate_single_message_content_if_present(
563570
return message
564571

565572
if isinstance(content, list):
566-
remaining = max_chars
567-
for item in content:
568-
if isinstance(item, dict) and "text" in item:
569-
text = item["text"]
570-
if isinstance(text, str):
571-
if len(text) > remaining:
572-
item["text"] = text[:remaining] + "..."
573-
remaining = 0
574-
else:
575-
remaining -= len(text)
573+
_truncate_text_items(content, "text", max_chars)
576574
return message
577575

578576
return message
579577

580578

579+
def _truncate_text_items(items: "List[Any]", key: str, max_chars: int) -> None:
580+
"""
581+
Truncate the `key` strings of `items` to at most `max_chars` characters in total.
582+
"""
583+
remaining = max_chars
584+
for item in items:
585+
if isinstance(item, dict) and key in item:
586+
text = item[key]
587+
if isinstance(text, str):
588+
if len(text) > remaining:
589+
item[key] = text[:remaining] + "..."
590+
remaining = 0
591+
else:
592+
remaining -= len(text)
593+
594+
581595
def _find_truncation_index(messages: "List[Dict[str, Any]]", max_bytes: int) -> int:
582596
"""
583597
Find the index of the first message that would exceed the max bytes limit.

‎tests/integrations/boto3/test_bedrock_runtime.py‎

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from botocore.stub import Stubber
77

88
import sentry_sdk
9+
from sentry_sdk.ai.utils import MAX_SINGLE_MESSAGE_CONTENT_CHARS
910
from sentry_sdk.consts import OP, SPANDATA
1011
from sentry_sdk.integrations.boto3 import Boto3Integration
1112
from sentry_sdk.integrations.boto3.consts import AI_ORIGIN, ORIGIN
@@ -31,6 +32,8 @@ def _has_shape_member(shape_name, member_name):
3132

3233

3334
MODEL_ID = "anthropic.claude-3-5-sonnet-20240620-v1:0"
35+
# too long to be kept in full when the last input message is truncated.
36+
LONG_TEXT = "a" * MAX_SINGLE_MESSAGE_CONTENT_CHARS
3437

3538
CONVERSE_RESPONSE = {
3639
"output": {
@@ -194,7 +197,7 @@ def test_converse(
194197
"messages": [
195198
{"role": "user", "content": [{"text": "Hi!"}]},
196199
{"role": "assistant", "content": [{"text": "Hi! What do you need?"}]},
197-
{"role": "user", "content": [{"text": "Hello!"}]},
200+
{"role": "user", "content": [{"text": "Hello!"}, {"text": LONG_TEXT}]},
198201
],
199202
"inferenceConfig": {
200203
"maxTokens": 100,
@@ -257,18 +260,39 @@ def test_converse(
257260
assert json.loads(attributes[SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS]) == [
258261
{"type": "text", "content": "You are a helpful assistant."}
259262
]
260-
assert json.loads(
261-
attributes[SPANDATA.GEN_AI_INPUT_MESSAGES]
262-
) == _expected_input_messages(
263-
trace_mode,
264-
[
263+
if trace_mode.get("stream_gen_ai_spans") is False:
264+
# the last message's text is truncated as a whole.
265+
expected_input_messages = [
266+
{
267+
"role": "user",
268+
"parts": [
269+
{"type": "text", "content": "Hello!"},
270+
{
271+
"type": "text",
272+
"content": LONG_TEXT[: MAX_SINGLE_MESSAGE_CONTENT_CHARS - 6]
273+
+ "...",
274+
},
275+
],
276+
}
277+
]
278+
else:
279+
expected_input_messages = [
265280
{"role": "user", "parts": [{"type": "text", "content": "Hi!"}]},
266281
{
267282
"role": "assistant",
268283
"parts": [{"type": "text", "content": "Hi! What do you need?"}],
269284
},
270-
{"role": "user", "parts": [{"type": "text", "content": "Hello!"}]},
271-
],
285+
{
286+
"role": "user",
287+
"parts": [
288+
{"type": "text", "content": "Hello!"},
289+
{"type": "text", "content": LONG_TEXT},
290+
],
291+
},
292+
]
293+
assert (
294+
json.loads(attributes[SPANDATA.GEN_AI_INPUT_MESSAGES])
295+
== expected_input_messages
272296
)
273297
assert json.loads(attributes[SPANDATA.GEN_AI_TOOL_DEFINITIONS]) == [
274298
{

0 commit comments

Comments
 (0)