-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
fix(bedrock): handle common image format aliases in BedrockImageProce… #24269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3602,6 +3602,8 @@ def _validate_format(mime_type: str, image_format: str) -> str: | |||||||||
| ######################################################### | ||||||||||
| # Check if image_format is an image or video | ||||||||||
| ######################################################### | ||||||||||
| format_aliases = {"jpg": "jpeg", "mpg": "mpeg"} | ||||||||||
| image_format = format_aliases.get(image_format, image_format) | ||||||||||
|
Comment on lines
+3605
to
+3606
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
def get_supported_video_types(self) -> List[str]:
return ["mp4", "mov", "mkv", "webm", "flv", "mpeg", "mpg", "wmv", "3gp"]This means The real bug — and the correct, minimal fix — only affects Consider either removing the
Suggested change
|
||||||||||
| if image_format not in supported_image_and_video_formats: | ||||||||||
| raise ValueError( | ||||||||||
| f"Unsupported image format: {image_format}. Supported formats: {supported_image_and_video_formats}" | ||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -134,14 +134,17 @@ def test_bedrock_validate_format_image_or_video(): | |||||||||||
| "webm", | ||||||||||||
| "flv", | ||||||||||||
| "mpeg", | ||||||||||||
| "mpg", | ||||||||||||
| "wmv", | ||||||||||||
| "3gp", | ||||||||||||
| ] | ||||||||||||
| for format in valid_video_formats: | ||||||||||||
| result = BedrockImageProcessor._validate_format(f"video/{format}", format) | ||||||||||||
| assert result == format, f"Expected {format}, got {result}" | ||||||||||||
|
|
||||||||||||
| # 'mpg' is aliased to 'mpeg' | ||||||||||||
| result = BedrockImageProcessor._validate_format("video/mpg", "mpg") | ||||||||||||
| assert result == "mpeg", f"Expected 'mpeg', got '{result}'" | ||||||||||||
|
|
||||||||||||
| # Test valid document formats | ||||||||||||
| valid_document_formats = { | ||||||||||||
| "application/pdf": "pdf", | ||||||||||||
|
|
@@ -155,6 +158,38 @@ def test_bedrock_validate_format_image_or_video(): | |||||||||||
| assert result == expected, f"Expected {expected}, got {result}" | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| def test_bedrock_validate_format_resolves_common_aliases(): | ||||||||||||
| """ | ||||||||||||
| Test that _validate_format resolves common format aliases like | ||||||||||||
| 'jpg' -> 'jpeg' and 'mpg' -> 'mpeg' instead of raising ValueError. | ||||||||||||
|
|
||||||||||||
| These aliases are standard equivalents (JPEG files commonly use .jpg | ||||||||||||
| extension, MPEG videos commonly use .mpg extension) but were previously | ||||||||||||
| rejected because only the canonical names appeared in the supported | ||||||||||||
| formats list. | ||||||||||||
|
|
||||||||||||
| Regression test for: https://github.com/BerriAI/litellm/issues/XXXXX | ||||||||||||
| """ | ||||||||||||
|
Comment on lines
+170
to
+172
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The docstring references a placeholder GitHub issue URL (
Suggested change
|
||||||||||||
| # 'jpg' should be resolved to 'jpeg' | ||||||||||||
| result_jpg = BedrockImageProcessor._validate_format("image/jpg", "jpg") | ||||||||||||
| assert result_jpg == "jpeg", f"Expected 'jpeg', got '{result_jpg}'" | ||||||||||||
|
|
||||||||||||
| # 'mpg' should be resolved to 'mpeg' | ||||||||||||
| result_mpg = BedrockImageProcessor._validate_format("video/mpg", "mpg") | ||||||||||||
| assert result_mpg == "mpeg", f"Expected 'mpeg', got '{result_mpg}'" | ||||||||||||
|
|
||||||||||||
| # Canonical names should still work unchanged | ||||||||||||
| result_jpeg = BedrockImageProcessor._validate_format("image/jpeg", "jpeg") | ||||||||||||
| assert result_jpeg == "jpeg", f"Expected 'jpeg', got '{result_jpeg}'" | ||||||||||||
|
|
||||||||||||
| result_mpeg = BedrockImageProcessor._validate_format("video/mpeg", "mpeg") | ||||||||||||
| assert result_mpeg == "mpeg", f"Expected 'mpeg', got '{result_mpeg}'" | ||||||||||||
|
|
||||||||||||
| # Unsupported formats should still raise ValueError | ||||||||||||
| with pytest.raises(ValueError, match="Unsupported image format"): | ||||||||||||
| BedrockImageProcessor._validate_format("image/bmp", "bmp") | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| def test_bedrock_get_document_format_fallback_mimes(): | ||||||||||||
| """ | ||||||||||||
| Test the _get_document_format method with fallback MIME types for DOCX and XLSX. | ||||||||||||
|
|
||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
format_aliasesdict re-created on every invocation_validate_formatis called on every image/video request. Allocating a new dict object on each call is a minor but unnecessary overhead. Since the alias map is static, it should be defined as a class-level constant:Then use
self._FORMAT_ALIASES.get(image_format, image_format)(orBedrockImageProcessor._FORMAT_ALIASES.get(...)since it's a@staticmethod).