From 31b931edc358c0ec01119a3a55952c1ea334a00e Mon Sep 17 00:00:00 2001 From: Ting SUN Date: Sat, 26 Sep 2026 01:51:33 +0800 Subject: [PATCH] fix(multimodal): return 400 for corrupt image inputs (#28131) Signed-off-by: Ting Sun Co-authored-by: Justin Tong Co-authored-by: Xinyuan Tong <115166877+JustinTong0323@users.noreply.github.com> --- python/sglang/srt/utils/common.py | 4 +-- test/registered/unit/utils/test_common.py | 38 ++++++++++++++++++++++- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/python/sglang/srt/utils/common.py b/python/sglang/srt/utils/common.py index bf29bb7641..2c016becef 100644 --- a/python/sglang/srt/utils/common.py +++ b/python/sglang/srt/utils/common.py @@ -1950,7 +1950,7 @@ def _load_image( ) try: image = Image.open(BytesIO(image_bytes)) - except OSError as e: + except (OSError, SyntaxError) as e: raise ValueError(f"Could not decode image: {e}") from e return _fully_load_pil_image(image) @@ -1959,7 +1959,7 @@ def _fully_load_pil_image(image: Image.Image) -> Image.Image: """Force PIL's lazy decode while malformed input is still request-local.""" try: image.load() - except OSError as e: + except (OSError, SyntaxError) as e: raise ValueError(f"Could not decode image: {e}") from e return image diff --git a/test/registered/unit/utils/test_common.py b/test/registered/unit/utils/test_common.py index 2b28b089d6..8b335a6511 100644 --- a/test/registered/unit/utils/test_common.py +++ b/test/registered/unit/utils/test_common.py @@ -1,19 +1,27 @@ +import io import sys import unittest from array import array from unittest import mock import torch +from PIL import Image from sglang.srt.utils.common import ( _get_device_sm_via_nvml, + _load_image, flatten_arrays_to_int64_tensor, get_device_sm_nvidia_smi, get_nvidia_driver_version_str, ) -from sglang.test.ci.ci_register import register_amd_ci, register_cuda_ci +from sglang.test.ci.ci_register import ( + register_amd_ci, + register_cpu_ci, + register_cuda_ci, +) from sglang.test.test_utils import CustomTestCase +register_cpu_ci(est_time=1, suite="base-a-test-cpu") register_cuda_ci(est_time=10, stage="base-b", runner_config="1-gpu-small") register_amd_ci(est_time=5, stage="stage-b", runner_config="1-gpu-small-amd") @@ -147,6 +155,34 @@ class TestGetDeviceSmNvidiaSmi(CustomTestCase): subprocess.run = original +class TestLoadImage(CustomTestCase): + def test_corrupt_image_bytes_raise_value_error(self): + buf = io.BytesIO() + Image.new("RGB", (16, 16), (10, 120, 200)).save(buf, format="PNG") + valid_png = buf.getvalue() + + broken_chunk_png = bytearray(valid_png) + idat_pos = broken_chunk_png.index(b"IDAT") + broken_chunk_png[idat_pos - 1] -= 6 + + img = _load_image(image_bytes=valid_png, gpu_image_decode=False) + self.assertEqual(img.size, (16, 16)) + + cases = [ + b"not an image", + b"", + valid_png[: len(valid_png) // 2], + bytes(broken_chunk_png), + ] + for image_bytes in cases: + with self.subTest(length=len(image_bytes)): + with self.assertRaisesRegex(ValueError, "Could not decode image"): + _load_image( + image_bytes=image_bytes, + gpu_image_decode=False, + ) + + class _FakePynvml: """Records the NVML index it was asked for, so a test can tell which physical GPU the helper would have reported."""