fix: change LiteLLM content and tool parameter handling

This changes how content parts are converted for LiteLLM, treating all "text/" mime types as plain text and only "application/pdf" as a file

Close #1940

Co-authored-by: George Weale <gweale@google.com>
PiperOrigin-RevId: 831418696
This commit is contained in:
George Weale
2025-11-12 09:23:00 -08:00
committed by Copybara-Service
parent e2d3b2d862
commit a19be12c1f
2 changed files with 55 additions and 8 deletions
+23 -2
View File
@@ -81,10 +81,19 @@ _FINISH_REASON_MAPPING = {
}
_SUPPORTED_FILE_CONTENT_MIME_TYPES = set(
["application/pdf", "application/json", "text/plain"]
["application/pdf", "application/json"]
)
def _decode_inline_text_data(raw_bytes: bytes) -> str:
"""Decodes inline file bytes that represent textual content."""
try:
return raw_bytes.decode("utf-8")
except UnicodeDecodeError:
logger.debug("Falling back to latin-1 decoding for inline file bytes.")
return raw_bytes.decode("latin-1", errors="replace")
class ChatCompletionFileUrlObject(TypedDict, total=False):
file_data: str
file_id: str
@@ -371,6 +380,15 @@ def _get_content(
and part.inline_data.data
and part.inline_data.mime_type
):
if part.inline_data.mime_type.startswith("text/"):
decoded_text = _decode_inline_text_data(part.inline_data.data)
if len(parts) == 1:
return decoded_text
content_objects.append({
"type": "text",
"text": decoded_text,
})
continue
base64_string = base64.b64encode(part.inline_data.data).decode("utf-8")
data_uri = f"data:{part.inline_data.mime_type};base64,{base64_string}"
# LiteLLM providers extract the MIME type from the data URI; avoid
@@ -397,7 +415,10 @@ def _get_content(
"file": {"file_data": data_uri},
})
else:
raise ValueError("LiteLlm(BaseLlm) does not support this content part.")
raise ValueError(
"LiteLlm(BaseLlm) does not support content part with MIME type "
f"{part.inline_data.mime_type}."
)
elif part.file_data and part.file_data.file_uri:
file_object: ChatCompletionFileUrlObject = {
"file_id": part.file_data.file_uri,
+32 -6
View File
@@ -109,12 +109,6 @@ FILE_BYTES_TEST_CASES = [
"data:application/json;base64,eyJoZWxsbyI6IndvcmxkIn0=",
id="json",
),
pytest.param(
b"hello world",
"text/plain",
"data:text/plain;base64,aGVsbG8gd29ybGQ=",
id="txt",
),
]
STREAMING_MODEL_RESPONSE = [
@@ -1477,6 +1471,38 @@ def test_get_content_text():
assert content == "Test text"
def test_get_content_text_inline_data_single_part():
parts = [
types.Part.from_bytes(
data="Inline text".encode("utf-8"), mime_type="text/plain"
)
]
content = _get_content(parts)
assert content == "Inline text"
def test_get_content_text_inline_data_multiple_parts():
parts = [
types.Part.from_bytes(
data="First part".encode("utf-8"), mime_type="text/plain"
),
types.Part.from_text(text="Second part"),
]
content = _get_content(parts)
assert content[0]["type"] == "text"
assert content[0]["text"] == "First part"
assert content[1]["type"] == "text"
assert content[1]["text"] == "Second part"
def test_get_content_text_inline_data_fallback_decoding():
parts = [
types.Part.from_bytes(data=b"\xff", mime_type="text/plain"),
]
content = _get_content(parts)
assert content == "ÿ"
def test_get_content_image():
parts = [
types.Part.from_bytes(data=b"test_image_data", mime_type="image/png")