Skip to content

Commit b74bdb9

Browse files
committed
fix(integrations): addressing review comments
1 parent f8345d0 commit b74bdb9

File tree

2 files changed

+106
-25
lines changed

2 files changed

+106
-25
lines changed

sentry_sdk/integrations/openai_agents/utils.py

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ def _transform_openai_agents_content_part(
5656
if part_type in ("image_url", "input_image"):
5757
# Get URL from either format
5858
if part_type == "image_url":
59-
image_url = content_part.get("image_url", {})
59+
image_url = content_part.get("image_url") or {}
6060
url = (
6161
image_url.get("url", "")
6262
if isinstance(image_url, dict)
6363
else str(image_url)
6464
)
6565
else:
6666
# input_image format has image_url directly
67-
url = content_part.get("image_url", "")
67+
url = content_part.get("image_url") or ""
6868

6969
if url.startswith("data:"):
7070
try:
@@ -93,35 +93,44 @@ def _transform_openai_agents_content_part(
9393

9494
# Handle input_audio (OpenAI audio input format)
9595
if part_type == "input_audio":
96-
input_audio = content_part.get("input_audio", {})
97-
audio_format = input_audio.get("format", "")
98-
mime_type = f"audio/{audio_format}" if audio_format else ""
99-
return {
100-
"type": "blob",
101-
"modality": "audio",
102-
"mime_type": mime_type,
103-
"content": input_audio.get("data", ""),
104-
}
96+
input_audio = content_part.get("input_audio") or {}
97+
if isinstance(input_audio, dict):
98+
audio_format = input_audio.get("format", "")
99+
mime_type = f"audio/{audio_format}" if audio_format else ""
100+
return {
101+
"type": "blob",
102+
"modality": "audio",
103+
"mime_type": mime_type,
104+
"content": input_audio.get("data", ""),
105+
}
106+
else:
107+
return content_part
105108

106109
# Handle image_file (Assistants API file-based images)
107110
if part_type == "image_file":
108-
image_file = content_part.get("image_file", {})
109-
return {
110-
"type": "file",
111-
"modality": "image",
112-
"mime_type": "",
113-
"file_id": image_file.get("file_id", ""),
114-
}
111+
image_file = content_part.get("image_file") or {}
112+
if isinstance(image_file, dict):
113+
return {
114+
"type": "file",
115+
"modality": "image",
116+
"mime_type": "",
117+
"file_id": image_file.get("file_id", ""),
118+
}
119+
else:
120+
return content_part
115121

116122
# Handle file (document attachments)
117123
if part_type == "file":
118-
file_data = content_part.get("file", {})
119-
return {
120-
"type": "file",
121-
"modality": "document",
122-
"mime_type": "",
123-
"file_id": file_data.get("file_id", ""),
124-
}
124+
file_data = content_part.get("file") or {}
125+
if isinstance(file_data, dict):
126+
return {
127+
"type": "file",
128+
"modality": "document",
129+
"mime_type": "",
130+
"file_id": file_data.get("file_id", ""),
131+
}
132+
else:
133+
return content_part
125134

126135
return content_part
127136

tests/integrations/openai_agents/test_openai_agents.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2005,6 +2005,78 @@ def test_openai_agents_message_truncation(sentry_init, capture_events):
20052005
assert "small message 5" in str(parsed_messages[1])
20062006

20072007

2008+
def test_transform_does_not_modify_original():
2009+
"""Test that transformation does not modify the original content."""
2010+
import copy
2011+
2012+
content_part = {
2013+
"type": "image_url",
2014+
"image_url": {
2015+
"url": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD",
2016+
"detail": "high",
2017+
},
2018+
}
2019+
original = copy.deepcopy(content_part)
2020+
_transform_openai_agents_content_part(content_part)
2021+
assert content_part == original, "Original content_part should not be modified"
2022+
2023+
content = [
2024+
{"type": "text", "text": "What is in this image?"},
2025+
{
2026+
"type": "image_url",
2027+
"image_url": {
2028+
"url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUg==",
2029+
},
2030+
},
2031+
]
2032+
original_content = copy.deepcopy(content)
2033+
_transform_openai_agents_message_content(content)
2034+
assert content == original_content, "Original content list should not be modified"
2035+
2036+
2037+
def test_transform_handles_none_values():
2038+
"""Test that transformation handles None values gracefully without crashing."""
2039+
# input_image with image_url explicitly set to None - should not crash
2040+
content_part = {"type": "input_image", "image_url": None}
2041+
result = _transform_openai_agents_content_part(content_part)
2042+
assert result == {"type": "uri", "modality": "image", "mime_type": "", "uri": ""}
2043+
2044+
# image_url with nested dict set to None - should not crash
2045+
content_part = {"type": "image_url", "image_url": None}
2046+
result = _transform_openai_agents_content_part(content_part)
2047+
assert result == {"type": "uri", "modality": "image", "mime_type": "", "uri": ""}
2048+
2049+
# input_audio with None value - gracefully returns empty blob
2050+
content_part = {"type": "input_audio", "input_audio": None}
2051+
result = _transform_openai_agents_content_part(content_part)
2052+
assert result == {
2053+
"type": "blob",
2054+
"modality": "audio",
2055+
"mime_type": "",
2056+
"content": "",
2057+
}
2058+
2059+
# image_file with None value - gracefully returns empty file reference
2060+
content_part = {"type": "image_file", "image_file": None}
2061+
result = _transform_openai_agents_content_part(content_part)
2062+
assert result == {
2063+
"type": "file",
2064+
"modality": "image",
2065+
"mime_type": "",
2066+
"file_id": "",
2067+
}
2068+
2069+
# file with None value - gracefully returns empty file reference
2070+
content_part = {"type": "file", "file": None}
2071+
result = _transform_openai_agents_content_part(content_part)
2072+
assert result == {
2073+
"type": "file",
2074+
"modality": "document",
2075+
"mime_type": "",
2076+
"file_id": "",
2077+
}
2078+
2079+
20082080
def test_transform_image_url_to_blob():
20092081
"""Test that OpenAI image_url with data URI is converted to blob format."""
20102082
content_part = {

0 commit comments

Comments
 (0)