Skip to content

Commit 489c39d

Browse files
GWealecopybara-github
authored andcommitted
fix: Remove redundant format field from LiteLLM content objects
LiteLLM providers can extract the MIME type from the data URI. Removing the separate `format` field avoids redundancy and potential issues with backends that may reject requests containing this field. Close #2017 Co-authored-by: George Weale <gweale@google.com> PiperOrigin-RevId: 828014286
1 parent 38ea749 commit 489c39d

File tree

2 files changed

+13
-20
lines changed

2 files changed

+13
-20
lines changed

src/google/adk/models/lite_llm.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -314,42 +314,35 @@ def _get_content(
314314
):
315315
base64_string = base64.b64encode(part.inline_data.data).decode("utf-8")
316316
data_uri = f"data:{part.inline_data.mime_type};base64,{base64_string}"
317+
# LiteLLM providers extract the MIME type from the data URI; avoid
318+
# passing a separate `format` field that some backends reject.
317319

318320
if part.inline_data.mime_type.startswith("image"):
319-
# Use full MIME type (e.g., "image/png") for providers that validate it
320-
format_type = part.inline_data.mime_type
321321
content_objects.append({
322322
"type": "image_url",
323-
"image_url": {"url": data_uri, "format": format_type},
323+
"image_url": {"url": data_uri},
324324
})
325325
elif part.inline_data.mime_type.startswith("video"):
326-
# Use full MIME type (e.g., "video/mp4") for providers that validate it
327-
format_type = part.inline_data.mime_type
328326
content_objects.append({
329327
"type": "video_url",
330-
"video_url": {"url": data_uri, "format": format_type},
328+
"video_url": {"url": data_uri},
331329
})
332330
elif part.inline_data.mime_type.startswith("audio"):
333-
# Use full MIME type (e.g., "audio/mpeg") for providers that validate it
334-
format_type = part.inline_data.mime_type
335331
content_objects.append({
336332
"type": "audio_url",
337-
"audio_url": {"url": data_uri, "format": format_type},
333+
"audio_url": {"url": data_uri},
338334
})
339335
elif part.inline_data.mime_type == "application/pdf":
340-
format_type = part.inline_data.mime_type
341336
content_objects.append({
342337
"type": "file",
343-
"file": {"file_data": data_uri, "format": format_type},
338+
"file": {"file_data": data_uri},
344339
})
345340
else:
346341
raise ValueError("LiteLlm(BaseLlm) does not support this content part.")
347342
elif part.file_data and part.file_data.file_uri:
348343
file_object: ChatCompletionFileUrlObject = {
349344
"file_id": part.file_data.file_uri,
350345
}
351-
if part.file_data.mime_type:
352-
file_object["format"] = part.file_data.mime_type
353346
content_objects.append({
354347
"type": "file",
355348
"file": file_object,

tests/unittests/models/test_litellm.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,7 +1107,7 @@ def test_content_to_message_param_user_message_with_file_uri():
11071107
assert message["content"][0]["text"] == "Summarize this file."
11081108
assert message["content"][1]["type"] == "file"
11091109
assert message["content"][1]["file"]["file_id"] == "gs://bucket/document.pdf"
1110-
assert message["content"][1]["file"]["format"] == "application/pdf"
1110+
assert "format" not in message["content"][1]["file"]
11111111

11121112

11131113
def test_content_to_message_param_user_message_file_uri_only():
@@ -1126,7 +1126,7 @@ def test_content_to_message_param_user_message_file_uri_only():
11261126
assert isinstance(message["content"], list)
11271127
assert message["content"][0]["type"] == "file"
11281128
assert message["content"][0]["file"]["file_id"] == "gs://bucket/only.pdf"
1129-
assert message["content"][0]["file"]["format"] == "application/pdf"
1129+
assert "format" not in message["content"][0]["file"]
11301130

11311131

11321132
def test_content_to_message_param_multi_part_function_response():
@@ -1278,7 +1278,7 @@ def test_get_content_image():
12781278
content[0]["image_url"]["url"]
12791279
== "data:image/png;base64,dGVzdF9pbWFnZV9kYXRh"
12801280
)
1281-
assert content[0]["image_url"]["format"] == "image/png"
1281+
assert "format" not in content[0]["image_url"]
12821282

12831283

12841284
def test_get_content_video():
@@ -1291,7 +1291,7 @@ def test_get_content_video():
12911291
content[0]["video_url"]["url"]
12921292
== "data:video/mp4;base64,dGVzdF92aWRlb19kYXRh"
12931293
)
1294-
assert content[0]["video_url"]["format"] == "video/mp4"
1294+
assert "format" not in content[0]["video_url"]
12951295

12961296

12971297
def test_get_content_pdf():
@@ -1304,7 +1304,7 @@ def test_get_content_pdf():
13041304
content[0]["file"]["file_data"]
13051305
== "data:application/pdf;base64,dGVzdF9wZGZfZGF0YQ=="
13061306
)
1307-
assert content[0]["file"]["format"] == "application/pdf"
1307+
assert "format" not in content[0]["file"]
13081308

13091309

13101310
def test_get_content_file_uri():
@@ -1317,7 +1317,7 @@ def test_get_content_file_uri():
13171317
content = _get_content(parts)
13181318
assert content[0]["type"] == "file"
13191319
assert content[0]["file"]["file_id"] == "gs://bucket/document.pdf"
1320-
assert content[0]["file"]["format"] == "application/pdf"
1320+
assert "format" not in content[0]["file"]
13211321

13221322

13231323
def test_get_content_audio():
@@ -1330,7 +1330,7 @@ def test_get_content_audio():
13301330
content[0]["audio_url"]["url"]
13311331
== "data:audio/mpeg;base64,dGVzdF9hdWRpb19kYXRh"
13321332
)
1333-
assert content[0]["audio_url"]["format"] == "audio/mpeg"
1333+
assert "format" not in content[0]["audio_url"]
13341334

13351335

13361336
def test_to_litellm_role():

0 commit comments

Comments
 (0)