Skip to content
15 changes: 8 additions & 7 deletions sentry_sdk/ai/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,18 +682,19 @@ def truncate_messages_by_size(
In the single message case, the serialized message size may exceed `max_bytes`, because
truncation is based only on character count in that case.
"""
serialized_json = json.dumps(messages, separators=(",", ":"))
normalized_messages = _normalize_data(messages, unpack=False)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normalization silently converts None values to string "None"

Medium Severity

_normalize_data converts None values to the string "None" (line 488's fallback: str(data)). OpenAI assistant messages with tool calls commonly have content: None. After this change, those become content: "None", altering the JSON representation from null to "None" — a semantic difference that also slightly skews the byte-size calculation used for truncation decisions.

Additional Locations (1)
Fix in Cursor Fix in Web

serialized_json = json.dumps(normalized_messages, separators=(",", ":"))
current_size = len(serialized_json.encode("utf-8"))

if current_size <= max_bytes:
return messages, 0
return normalized_messages, 0

truncation_index = _find_truncation_index(messages, max_bytes)
if truncation_index < len(messages):
truncated_messages = messages[truncation_index:]
truncation_index = _find_truncation_index(normalized_messages, max_bytes)
if truncation_index < len(normalized_messages):
truncated_messages = normalized_messages[truncation_index:]
else:
truncation_index = len(messages) - 1
truncated_messages = messages[-1:]
truncation_index = len(normalized_messages) - 1
truncated_messages = normalized_messages[-1:]

if len(truncated_messages) == 1:
truncated_messages[0] = _truncate_single_message_content_if_present(
Expand Down
Loading