fix: normalize Content-Type parameters in multimodal image processing (#1446)
SWEBenchMultimodalProblemStatement._download_and_convert_image compared the full lowercased Content-Type header against VALID_IMAGE_MIME_TYPES. When a server returns a valid image with media type parameters (for example "Content-Type: image/png; charset=utf-8", which is legal per RFC 9110), the string "image/png; charset=utf-8" is not in the allowed set, so the image is logged as an unsupported MIME type and silently dropped from the problem statement. Strip the media type parameters (split on ";", strip, lowercase) before the existing image/jpg -> image/jpeg normalization and the membership check, so the bare media type is validated and used in the encoded data URI. Behavior for unsupported types, size limits, empty images, and network failures is unchanged. Add a regression test covering a Content-Type header with a charset parameter. Closes #1441 Signed-off-by: Anas Khan <83116240+anxkhn@users.noreply.github.com>
This commit is contained in:
@@ -237,7 +237,8 @@ class SWEBenchMultimodalProblemStatement(_BuiltinProblemStatementBase):
|
||||
}
|
||||
response = requests.get(url, headers=headers, timeout=30, stream=True)
|
||||
response.raise_for_status()
|
||||
content_type = response.headers.get("content-type", "").lower()
|
||||
# strip any media type parameters (e.g. "image/png; charset=utf-8") before validation
|
||||
content_type = response.headers.get("content-type", "").split(";")[0].strip().lower()
|
||||
if content_type == "image/jpg":
|
||||
content_type = "image/jpeg"
|
||||
if content_type not in VALID_IMAGE_MIME_TYPES:
|
||||
|
||||
@@ -43,6 +43,23 @@ class TestSWEBenchMultimodalProblemStatement:
|
||||
assert "Test problem statement" in result
|
||||
assert f"
|
||||
def test_get_problem_statement_with_content_type_parameters(self, mock_get):
|
||||
"""Test that a Content-Type header with media type parameters is still accepted."""
|
||||
# servers may append parameters like charset, which is legal per RFC 9110
|
||||
mock_response = Mock()
|
||||
mock_response.raise_for_status.return_value = None
|
||||
mock_response.headers = {"content-type": "image/png; charset=utf-8"}
|
||||
mock_response.iter_content.return_value = [b"fake_image_data"]
|
||||
mock_get.return_value = mock_response
|
||||
problem_statement = SWEBenchMultimodalProblemStatement(
|
||||
text="Test problem statement", issue_images=[self.example_image_url]
|
||||
)
|
||||
result = problem_statement.get_problem_statement()
|
||||
# the parameters should be stripped before validation and encoding
|
||||
assert "Test problem statement" in result
|
||||
assert f"
|
||||
def test_get_problem_statement_with_network_error(self, mock_get):
|
||||
"""Test that network errors are handled gracefully with warnings."""
|
||||
|
||||
Reference in New Issue
Block a user