🐛 Fix Gemini gcs_uri MIME handling without extension#2
Draft
S0ngRu1 wants to merge 12 commits into
Draft
Conversation
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
…dling Replace generic Exception raises with litellm.BadRequestError in _gemini_convert_messages_with_history() for two error paths: 1. When file_id and file_data are both None 2. When MIME type cannot be determined, with special handling for ImageFetchError Add unit tests covering all three error paths and MIME type URL detection tests.
fix(vertex_ai): use BadRequestError in Gemini transformation file handling
… transformation Replace generic Exception raises with litellm.BadRequestError in _gemini_convert_messages_with_history() for two error paths: 1. When file_id and file_data are both None — raises BadRequestError 2. When _process_gemini_media throws: - ImageFetchError — re-raised as BadRequestError preserving original message - BadRequestError — re-raised as-is - Any other exception — wrapped as BadRequestError with original error details All wrapping raise statements use `raise ... from e` to preserve the original exception chain for server-side debugging. Added tests/test_litellm/llms/vertex_ai/gemini/test_gemini_transformation_exception_handling.py covering all error paths. Fixes BerriAI#24193
…ort values Replace ValueError with litellm.BadRequestError in _map_reasoning_effort so that empty strings and unrecognized values return a 400 response with a clear message instead of being wrapped as a 500 APIConnectionError. Add unit tests covering empty string, invalid string, None, and all valid values.
Resolve merge conflict in transformation.py: - Add trailing comma to llm_provider argument - Add exception chaining (from e) for BadRequestError raises - Add explicit re-raise for BadRequestError to preserve original message
Co-authored-by: Cai Songrui <3230809014@stu.cuit.edu.cn>
Reviewer's GuideAdjusts Gemini media handling for GCS URIs without extensions by deriving MIME type from additional image_url fields, raising a clear BadRequestError when type cannot be inferred, and adds regression tests covering the new behavior. Flow diagram for updated _process_gemini_media GCS URI MIME handlingflowchart TD
A["Start _process_gemini_media with image_url"] --> B{"Does image_url contain gs://?"}
B -- "No" --> Z["Handle non GCS media (unchanged)"]
B -- "Yes" --> C["extension_with_dot = os.path.splitext(image_url)[-1]"]
C --> D["extension = extension_with_dot[1:]"]
D --> E{"Was format explicitly provided?"}
E -- "Yes" --> F["mime_type = format"]
F --> G["file_data = FileDataType(mime_type, file_uri=image_url)"]
G --> H["Return or use file_data (Gemini call)"]
E -- "No" --> I{"Does extension exist?"}
I -- "Yes" --> J["file_type = get_file_type_from_extension(extension)"]
J --> K{"is_gemini_1_5_accepted_file_type(file_type)?"}
K -- "No" --> L["Raise Exception: File type not supported by gemini"]
K -- "Yes" --> M["mime_type = get_file_mime_type_for_file_type(file_type)"]
M --> G
I -- "No" --> N["Raise litellm.BadRequestError with guidance:\nUnable to determine mime type for gs URI (no extension).\nAsk caller to set image_url.format / mime_type / content_type or message.content[].file.format."]
N --> H
Flow diagram for updated format extraction in _gemini_convert_messages_with_historyflowchart TD
A["Start processing img_element in _gemini_convert_messages_with_history"] --> B{"Is img_element[image_url] a dict?"}
B -- "No" --> Z["Handle non dict image_url (unchanged)"]
B -- "Yes" --> C["image_url = img_element[image_url][url]"]
C --> D["format = img_element[image_url].get(format)"]
D --> E{"Is format set?"}
E -- "Yes" --> H["Use format as mime_type for Gemini media"]
E -- "No" --> F["format = img_element[image_url].get(mime_type)"]
F --> G{"Is format set from mime_type?"}
G -- "Yes" --> H
G -- "No" --> I["format = img_element[image_url].get(content_type)"]
I --> J{"Is format set from content_type?"}
J -- "Yes" --> H
J -- "No" --> K["format remains None; rely on downstream MIME inference or error"]
H --> L["detail = img_element[image_url].get(detail)"]
L --> M["media_resolution_enum = _convert_detail_to_media_resolution_enum(detail)"]
M --> N["Pass image_url, format (or mime_type/content_type), media_resolution_enum to Gemini media processing"]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Co-authored-by: Cai Songrui <3230809014@stu.cuit.edu.cn>
| "?fields=contentType" | ||
| ) | ||
| try: | ||
| response = httpx.get(url=metadata_url, headers=headers, timeout=5.0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Relevant issues
Fixes gcs_uri without extension failure in Gemini media transformation by auto-reading GCS object metadata content type.
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
tests/test_litellm/directory, Adding at least 1 test is a hard requirement - see detailsmake test-unit@greptileaiand received a Confidence Score of at least 4/5 before requesting a maintainer reviewDelays in PR merge?
If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).
CI (LiteLLM team)
Branch creation CI run
Link:
CI run for the last commit
Link:
Merge / cherry-pick CI run
Links:
Type
🐛 Bug Fix
✅ Test
Changes
gs://URIs in Gemini media conversion.fields=contentType).format/mime_type/content_type).litellm_paramsthrough_transform_messagesinto_gemini_convert_messages_with_historyso media conversion has access to vertex auth context.image/jpeg.Summary by Sourcery
Improve Gemini Vertex AI media handling for GCS image URIs without file extensions and allow MIME metadata from OpenAI-style image payloads to drive MIME selection.
Bug Fixes:
Tests: