44from typing import TYPE_CHECKING
55
66from sentry_sdk ._types import BLOB_DATA_SUBSTITUTE
7+ from sentry_sdk .ai .consts import DATA_URL_BASE64_REGEX
78
89if TYPE_CHECKING :
910 from typing import Any , Callable , Dict , List , Optional , Tuple
@@ -588,6 +589,20 @@ def _find_truncation_index(messages: "List[Dict[str, Any]]", max_bytes: int) ->
588589 return 0
589590
590591
592+ def _is_image_type_with_blob_content (item : "Dict[str, Any]" ) -> bool :
593+ """
594+ Some content blocks contain an image_url property with base64 content as its value.
595+ This is used to identify those while not leading to unnecessary copying of data when the image URL does not contain base64 content.
596+ """
597+ if item .get ("type" ) != "image_url" :
598+ return False
599+
600+ image_url = item .get ("image_url" , {}).get ("url" , "" )
601+ data_url_match = DATA_URL_BASE64_REGEX .match (image_url )
602+
603+ return bool (data_url_match )
604+
605+
591606def redact_blob_message_parts (
592607 messages : "List[Dict[str, Any]]" ,
593608) -> "List[Dict[str, Any]]" :
@@ -640,7 +655,9 @@ def redact_blob_message_parts(
640655 content = message .get ("content" )
641656 if isinstance (content , list ):
642657 for item in content :
643- if isinstance (item , dict ) and item .get ("type" ) == "blob" :
658+ if isinstance (item , dict ) and (
659+ item .get ("type" ) == "blob" or _is_image_type_with_blob_content (item )
660+ ):
644661 has_blobs = True
645662 break
646663 if has_blobs :
@@ -661,8 +678,11 @@ def redact_blob_message_parts(
661678 content = message .get ("content" )
662679 if isinstance (content , list ):
663680 for item in content :
664- if isinstance (item , dict ) and item .get ("type" ) == "blob" :
665- item ["content" ] = BLOB_DATA_SUBSTITUTE
681+ if isinstance (item , dict ):
682+ if item .get ("type" ) == "blob" :
683+ item ["content" ] = BLOB_DATA_SUBSTITUTE
684+ elif _is_image_type_with_blob_content (item ):
685+ item ["image_url" ]["url" ] = BLOB_DATA_SUBSTITUTE
666686
667687 return messages_copy
668688
0 commit comments