Skip to content

fix: fail fast when LiteLLM file_uri MIME type cannot be determined#5182

Open
YizukiAme wants to merge 1 commit intogoogle:mainfrom
YizukiAme:clean/litellm-file-uri-mime
Open

fix: fail fast when LiteLLM file_uri MIME type cannot be determined#5182
YizukiAme wants to merge 1 commit intogoogle:mainfrom
YizukiAme:clean/litellm-file-uri-mime

Conversation

@YizukiAme
Copy link
Copy Markdown

@YizukiAme YizukiAme commented Apr 7, 2026

Fixes #5184

Problem

When file_data.file_uri has no explicit MIME type and the URI has no recognizable extension, the code silently falls back to application/octet-stream. This causes downstream issues with providers (e.g., Vertex AI) that require an accurate MIME type for GCS URIs — the generic fallback leads to incorrect file handling without any warning.

Fix

  • Fail fast with actionable error: Instead of silently using application/octet-stream, raise a ValueError with a clear message suggesting the user set file_data.mime_type explicitly. This only triggers when a file block actually needs to be constructed — provider-specific text fallback paths (e.g., Anthropic's [File reference: ...]) execute first and are unaffected.
  • Preserve model context in mixed content: Fixed _content_to_message_param() to pass model through recursive calls for mixed function_response + file content, preventing incorrect provider fallback on Vertex AI / Gemini.
  • Removed _DEFAULT_MIME_TYPE constant since the silent fallback is no longer used.

Logs After Fix

$ uv run pytest tests/unittests/models/test_litellm.py -v -k "file_uri or mime or function_response_with_extra_parts"

tests/unittests/models/test_litellm.py::test_file_data_with_file_uri_explicit_mime PASSED
tests/unittests/models/test_litellm.py::test_file_data_with_file_uri_inferred_mime PASSED
tests/unittests/models/test_litellm.py::test_file_data_with_file_uri_no_mime_raises PASSED
tests/unittests/models/test_litellm.py::test_function_response_with_extra_parts_preserves_model PASSED
...

========== 42 passed, 206 deselected in 6.62s ==========

Testing Plan

uv run pytest tests/unittests/models/test_litellm.py -v -k "file_uri or mime or function_response_with_extra_parts"

All 42 related tests pass. Key coverage:

  • test_file_data_with_file_uri_no_mime_raises: Verifies ValueError when MIME cannot be determined and a file block is needed
  • test_file_data_with_file_uri_explicit_mime: Explicit MIME still works
  • test_file_data_with_file_uri_inferred_mime: Extension-based inference still works
  • test_function_response_with_extra_parts_preserves_model: Mixed content recursive calls now preserve model parameter

Signed-off-by: Yizuki_Ame <yinzimike@gmail.com>
@YizukiAme YizukiAme marked this pull request as draft April 7, 2026 07:55
@adk-bot adk-bot added the models [Component] Issues related to model support label Apr 7, 2026
@YizukiAme YizukiAme marked this pull request as ready for review April 7, 2026 08:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

models [Component] Issues related to model support

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LiteLLM silently uses application/octet-stream when file_uri MIME type cannot be determined

2 participants