fix(prompt_templates): strip content-type parameters from fetched media

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
mateo 2026-10-16 21:38:46 +00:00
parent 8ed2976076
commit 3b28e7008e
2 changed files with 37 additions and 2 deletions

View file

@ -1646,12 +1646,16 @@ def infer_content_type_from_url_and_content(
"""
from litellm.litellm_core_utils.token_counter import get_image_type
media_type: Final = (
current_content_type.split(";", 1)[0].strip().lower() if current_content_type else None
)
# If we have a valid content type that's not generic, use it
if current_content_type and current_content_type not in [
if media_type and media_type not in [
"binary/octet-stream",
"application/octet-stream",
]:
return current_content_type
return media_type
# Extension to MIME type mapping
# Supports images, documents, and other common file types

View file

@ -288,6 +288,37 @@ def test_convert_url_to_base64_keeps_a_real_content_type(monkeypatch):
assert result.startswith("data:image/jpeg;base64,")
@pytest.mark.parametrize(
("url", "content_type", "expected"),
[
(
"https://docs.example/document.pdf",
"application/pdf; qs=0.001",
"data:application/pdf;base64,cGRm",
),
(
"https://img.example/image.png",
"image/png; charset=binary",
"data:image/png;base64,cGRm",
),
(
"https://docs.example/document.pdf",
"application/octet-stream; charset=binary",
"data:application/pdf;base64,cGRm",
),
],
)
def test_convert_url_to_base64_uses_bare_content_type(
monkeypatch: pytest.MonkeyPatch,
url: str,
content_type: str,
expected: str,
) -> None:
monkeypatch.setattr(litellm, "module_level_client", _stub_image_client(b"pdf", content_type))
assert convert_url_to_base64(url) == expected
def test_convert_url_to_base64_raises_when_no_content_type_is_determinable(monkeypatch):
monkeypatch.setattr(
litellm,