Skip to content

Commit 88c04f9

Browse files
committed
refactor(litellm): use transform_openai_content_part directly
LiteLLM uses OpenAI's message format, so use the OpenAI-specific transform_openai_content_part function directly for better performance. Also update test to reflect correct behavior: data URIs without base64 encoding are still inline data and should be treated as blobs, not URIs.
1 parent 15c63ff commit 88c04f9

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

sentry_sdk/integrations/litellm.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
get_start_span_function,
99
set_data_normalized,
1010
truncate_and_annotate_messages,
11-
transform_message_content,
11+
transform_openai_content_part,
1212
)
1313
from sentry_sdk.consts import SPANDATA
1414
from sentry_sdk.integrations import DidNotEnable, Integration
@@ -41,7 +41,7 @@ def _get_metadata_dict(kwargs: "Dict[str, Any]") -> "Dict[str, Any]":
4141
def _convert_message_parts(messages: "List[Dict[str, Any]]") -> "List[Dict[str, Any]]":
4242
"""
4343
Convert the message parts from OpenAI format to the `gen_ai.request.messages` format
44-
using the shared transform_message_content function.
44+
using the OpenAI-specific transformer (LiteLLM uses OpenAI's message format).
4545
4646
Deep copies messages to avoid mutating original kwargs.
4747
"""
@@ -51,8 +51,17 @@ def _convert_message_parts(messages: "List[Dict[str, Any]]") -> "List[Dict[str,
5151
for message in messages:
5252
if not isinstance(message, dict):
5353
continue
54-
if "content" in message:
55-
message["content"] = transform_message_content(message["content"])
54+
content = message.get("content")
55+
if isinstance(content, (list, tuple)):
56+
transformed = []
57+
for item in content:
58+
if isinstance(item, dict):
59+
result = transform_openai_content_part(item)
60+
# If transformation succeeded, use the result; otherwise keep original
61+
transformed.append(result if result is not None else item)
62+
else:
63+
transformed.append(item)
64+
message["content"] = transformed
5665
return messages
5766

5867

tests/integrations/litellm/test_litellm.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ def test_convert_message_parts_does_not_mutate_original():
939939

940940

941941
def test_convert_message_parts_data_url_without_base64():
942-
"""Data URLs without ;base64, marker should be treated as regular URIs."""
942+
"""Data URLs without ;base64, marker are still inline data and should be blobs."""
943943
messages = [
944944
{
945945
"role": "user",
@@ -952,10 +952,12 @@ def test_convert_message_parts_data_url_without_base64():
952952
}
953953
]
954954
converted = _convert_message_parts(messages)
955-
uri_item = converted[0]["content"][0]
956-
# Should be converted to uri type, not blob (since no base64 encoding)
957-
assert uri_item["type"] == "uri"
958-
assert uri_item["uri"] == "data:image/png,rawdata"
955+
blob_item = converted[0]["content"][0]
956+
# Data URIs (with or without base64 encoding) contain inline data and should be blobs
957+
assert blob_item["type"] == "blob"
958+
assert blob_item["modality"] == "image"
959+
assert blob_item["mime_type"] == "image/png"
960+
assert blob_item["content"] == "rawdata"
959961

960962

961963
def test_convert_message_parts_image_url_none():

0 commit comments

Comments
 (0)