-
Notifications
You must be signed in to change notification settings - Fork 572
fix(ai): redact message parts content of type blob #5243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
1f32952
795bcea
a623e13
3d3ce5b
767050c
614d439
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,8 @@ | |
| from sys import getsizeof | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from sentry_sdk._types import SENSITIVE_DATA_SUBSTITUTE | ||
|
|
||
| if TYPE_CHECKING: | ||
| from typing import Any, Callable, Dict, List, Optional, Tuple | ||
|
|
||
|
|
@@ -141,6 +143,85 @@ def _find_truncation_index(messages: "List[Dict[str, Any]]", max_bytes: int) -> | |
| return 0 | ||
|
|
||
|
|
||
| def redact_blob_message_parts( | ||
| messages: "List[Dict[str, Any]]", | ||
| ) -> "List[Dict[str, Any]]": | ||
| """ | ||
| Redact blob message parts from the messages by replacing blob content with "[Filtered]". | ||
|
|
||
| This function creates a deep copy of messages that contain blob content to avoid | ||
| mutating the original message dictionaries. Messages without blob content are | ||
| returned as-is to minimize copying overhead. | ||
|
|
||
| e.g: | ||
| { | ||
| "role": "user", | ||
| "content": [ | ||
| { | ||
| "text": "How many ponies do you see in the image?", | ||
| "type": "text" | ||
| }, | ||
| { | ||
| "type": "blob", | ||
| "modality": "image", | ||
| "mime_type": "image/jpeg", | ||
| "content": "data:image/jpeg;base64,..." | ||
| } | ||
| ] | ||
| } | ||
| becomes: | ||
| { | ||
| "role": "user", | ||
| "content": [ | ||
| { | ||
| "text": "How many ponies do you see in the image?", | ||
| "type": "text" | ||
| }, | ||
| { | ||
| "type": "blob", | ||
| "modality": "image", | ||
| "mime_type": "image/jpeg", | ||
| "content": "[Filtered]" | ||
| } | ||
| ] | ||
| } | ||
| """ | ||
|
|
||
| # First pass: check if any message contains blob content | ||
| has_blobs = False | ||
| for message in messages: | ||
| if not isinstance(message, dict): | ||
| continue | ||
| content = message.get("content") | ||
| if isinstance(content, list): | ||
| for item in content: | ||
| if isinstance(item, dict) and item.get("type") == "blob": | ||
| has_blobs = True | ||
| break | ||
| if has_blobs: | ||
| break | ||
|
|
||
| # If no blobs found, return original messages to avoid unnecessary copying | ||
| if not has_blobs: | ||
| return messages | ||
|
|
||
| # Deep copy messages to avoid mutating the original | ||
| messages_copy = deepcopy(messages) | ||
|
|
||
| # Second pass: redact blob content in the copy | ||
| for message in messages_copy: | ||
| if not isinstance(message, dict): | ||
| continue | ||
|
|
||
| content = message.get("content") | ||
| if isinstance(content, list): | ||
| for item in content: | ||
| if isinstance(item, dict) and item.get("type") == "blob": | ||
| item["content"] = BLOB_DATA_SUBSTITUTE | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Undefined variable
|
||
|
|
||
| return messages_copy | ||
|
|
||
|
|
||
| def truncate_messages_by_size( | ||
| messages: "List[Dict[str, Any]]", | ||
| max_bytes: int = MAX_GEN_AI_MESSAGE_BYTES, | ||
|
|
@@ -186,6 +267,8 @@ def truncate_and_annotate_messages( | |
| if not messages: | ||
| return None | ||
|
|
||
| messages = redact_blob_message_parts(messages) | ||
|
|
||
| truncated_messages, removed_count = truncate_messages_by_size(messages, max_bytes) | ||
| if removed_count > 0: | ||
| scope._gen_ai_original_message_count[span.span_id] = len(messages) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: The variable
BLOB_DATA_SUBSTITUTEis used inredact_blob_message_partsbut is never defined or imported, which will cause aNameErrorat runtime.Severity: CRITICAL
🔍 Detailed Analysis
The function
redact_blob_message_partsinsentry_sdk/ai/utils.pyattempts to assign the value ofBLOB_DATA_SUBSTITUTEon line 220. However, this constant is never defined in the codebase or imported into the file. This will result in aNameErrorwhenever the function is called to redact blob content, such as images in AI monitoring messages. This is a guaranteed runtime crash for the feature being implemented. The intended constant was likelySENSITIVE_DATA_SUBSTITUTE, which is correctly imported and used in tests.💡 Suggested Fix
In
sentry_sdk/ai/utils.pyon line 220, replace the undefined variableBLOB_DATA_SUBSTITUTEwith the correctly imported and defined constantSENSITIVE_DATA_SUBSTITUTE.🤖 Prompt for AI Agent
Did we get this right? 👍 / 👎 to inform future reviews.
Reference ID:
8491562