Skip to content

Commit f676c95

Browse files
committed
fix: handle image_url string shorthand in _is_image_type_with_blob_content
When image_url is a plain string (OpenAI shorthand), calling .get() on it raised AttributeError. Now both dict form {"url": "..."} and string form are handled correctly in both the detection and redaction paths.
1 parent 2ce26d1 commit f676c95

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

sentry_sdk/ai/utils.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,12 @@ def _is_image_type_with_blob_content(item: "Dict[str, Any]") -> bool:
609609
if item.get("type") != "image_url":
610610
return False
611611

612-
image_url = item.get("image_url", {}).get("url", "")
612+
image_url_val = item.get("image_url")
613+
image_url = (
614+
image_url_val.get("url", "")
615+
if isinstance(image_url_val, dict)
616+
else (image_url_val or "")
617+
)
613618
data_url_match = DATA_URL_BASE64_REGEX.match(image_url)
614619

615620
return bool(data_url_match)
@@ -694,7 +699,10 @@ def redact_blob_message_parts(
694699
if item.get("type") == "blob":
695700
item["content"] = BLOB_DATA_SUBSTITUTE
696701
elif _is_image_type_with_blob_content(item):
697-
item["image_url"]["url"] = BLOB_DATA_SUBSTITUTE
702+
if isinstance(item["image_url"], dict):
703+
item["image_url"]["url"] = BLOB_DATA_SUBSTITUTE
704+
else:
705+
item["image_url"] = BLOB_DATA_SUBSTITUTE
698706

699707
return messages_copy
700708

tests/test_ai_monitoring.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,23 @@ def test_handles_non_dict_content_items(self):
923923
# Should return same list since no blobs
924924
assert result is messages
925925

926+
def test_redact_blob_message_parts_image_url_string_shorthand(self):
927+
"""image_url as a plain string (OpenAI shorthand) must not raise AttributeError"""
928+
messages = [
929+
{
930+
"role": "user",
931+
"content": [
932+
{"type": "text", "text": "What is in this image?"},
933+
{
934+
"type": "image_url",
935+
"image_url": "data:image/jpeg;base64,/9j/abc123==",
936+
},
937+
],
938+
}
939+
]
940+
result = redact_blob_message_parts(messages)
941+
assert result[0]["content"][1]["image_url"] == "[Blob substitute]"
942+
926943

927944
class TestParseDataUri:
928945
def test_parses_base64_image_data_uri(self):

0 commit comments

Comments
 (0)