Skip to content

Commit 7074f0b

Browse files
committed
test(integrations): add test for message conversion
1 parent ce29e47 commit 7074f0b

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

tests/integrations/openai/test_openai.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
from sentry_sdk.integrations.openai import (
4444
OpenAIIntegration,
4545
_calculate_token_usage,
46+
_convert_message_parts,
4647
)
4748
from sentry_sdk.ai.utils import MAX_GEN_AI_MESSAGE_BYTES
4849
from sentry_sdk._types import AnnotatedValue
@@ -1509,6 +1510,77 @@ def test_openai_message_role_mapping(sentry_init, capture_events):
15091510
assert "ai" not in roles
15101511

15111512

1513+
def test_convert_message_parts_image_url_to_blob():
1514+
"""Test that OpenAI image_url message parts are correctly converted to blob format"""
1515+
messages = [
1516+
{
1517+
"role": "user",
1518+
"content": [
1519+
{
1520+
"text": "How many ponies do you see in the image?",
1521+
"type": "text",
1522+
},
1523+
{
1524+
"type": "image_url",
1525+
"image_url": {
1526+
"url": "data:image/jpeg;base64,/9j/4AAQSkZJRg==",
1527+
"detail": "high",
1528+
},
1529+
},
1530+
],
1531+
}
1532+
]
1533+
1534+
converted = _convert_message_parts(messages)
1535+
1536+
assert len(converted) == 1
1537+
assert converted[0]["role"] == "user"
1538+
assert isinstance(converted[0]["content"], list)
1539+
assert len(converted[0]["content"]) == 2
1540+
1541+
# First item (text) should remain unchanged
1542+
assert converted[0]["content"][0] == {
1543+
"text": "How many ponies do you see in the image?",
1544+
"type": "text",
1545+
}
1546+
1547+
# Second item (image_url) should be converted to blob format
1548+
blob_item = converted[0]["content"][1]
1549+
assert blob_item["type"] == "blob"
1550+
assert blob_item["modality"] == "image"
1551+
assert blob_item["mime_type"] == "data:image/jpeg"
1552+
assert blob_item["content"] == "/9j/4AAQSkZJRg=="
1553+
# Verify the original image_url structure is replaced
1554+
assert "image_url" not in blob_item
1555+
1556+
1557+
def test_convert_message_parts_image_url_to_uri():
1558+
"""Test that OpenAI image_url with non-data URLs are converted to uri format"""
1559+
messages = [
1560+
{
1561+
"role": "user",
1562+
"content": [
1563+
{
1564+
"type": "image_url",
1565+
"image_url": {
1566+
"url": "https://example.com/image.jpg",
1567+
"detail": "low",
1568+
},
1569+
},
1570+
],
1571+
}
1572+
]
1573+
1574+
converted = _convert_message_parts(messages)
1575+
1576+
assert len(converted) == 1
1577+
uri_item = converted[0]["content"][0]
1578+
assert uri_item["type"] == "uri"
1579+
assert uri_item["uri"] == "https://example.com/image.jpg"
1580+
# Verify the original image_url structure is replaced
1581+
assert "image_url" not in uri_item
1582+
1583+
15121584
def test_openai_message_truncation(sentry_init, capture_events):
15131585
"""Test that large messages are truncated properly in OpenAI integration."""
15141586
sentry_init(

0 commit comments

Comments
 (0)