fix(bedrock): handle common image format aliases in BedrockImageProcessor

Feishu (Lark) and other platforms may send images with MIME type
`image/jpg` instead of `image/jpeg`. The Bedrock converse API
validator rejects `jpg` even though it is the same format as `jpeg`.

Add a format alias mapping to normalize common equivalent extensions
before validation (`jpg` → `jpeg`, `mpg` → `mpeg`), preventing
unnecessary 500 errors for valid media types.

Made-with: Cursor
This commit is contained in:
longzhihun 2026-03-21 12:58:44 +08:00 committed by EC2 Default User
parent d8e4fc4dd0
commit 53841f8b7c
2 changed files with 38 additions and 1 deletions

View file

@ -3602,6 +3602,8 @@ class BedrockImageProcessor:
#########################################################
# Check if image_format is an image or video
#########################################################
format_aliases = {"jpg": "jpeg", "mpg": "mpeg"}
image_format = format_aliases.get(image_format, image_format)
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}"

View file

@ -134,7 +134,6 @@ def test_bedrock_validate_format_image_or_video():
"webm",
"flv",
"mpeg",
"mpg",
"wmv",
"3gp",
]
@ -142,6 +141,10 @@ def test_bedrock_validate_format_image_or_video():
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
"""
# '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.