Skip to content

Commit 9bc8bb0

Browse files
authored
[Bugfix] properly catch PIL-related errors for vision models when incorrect data urls are provided (#19202)
Signed-off-by: Guillaume Calmettes <gcalmettes@scaleway.com>
1 parent 1aeb925 commit 9bc8bb0

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

tests/multimodal/test_utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,19 @@ async def test_fetch_image_local_files(image_url: str):
141141
f"file://{temp_dir}/../{os.path.basename(image_url)}")
142142

143143

144+
@pytest.mark.asyncio
145+
async def test_fetch_image_error_conversion():
146+
connector = MediaConnector()
147+
broken_img = "data:image/png;base64,aGVsbG9fdmxsbV9jb21tdW5pdHkK"
148+
149+
# PIL.UnidentifiedImageError should be converted to ValueError
150+
with pytest.raises(ValueError):
151+
await connector.fetch_image_async(broken_img)
152+
153+
with pytest.raises(ValueError):
154+
connector.fetch_image(broken_img)
155+
156+
144157
@pytest.mark.asyncio
145158
@pytest.mark.parametrize("video_url", TEST_VIDEO_URLS)
146159
@pytest.mark.parametrize("num_frames", [-1, 32, 1800])

vllm/multimodal/utils.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import numpy as np
1010
import numpy.typing as npt
1111
import torch
12-
from PIL import Image
12+
from PIL import Image, UnidentifiedImageError
1313

1414
import vllm.envs as envs
1515
from vllm.connections import HTTPConnection, global_http_connection
@@ -185,11 +185,15 @@ def fetch_image(
185185
"""
186186
image_io = ImageMediaIO(image_mode=image_mode)
187187

188-
return self.load_from_url(
189-
image_url,
190-
image_io,
191-
fetch_timeout=envs.VLLM_IMAGE_FETCH_TIMEOUT,
192-
)
188+
try:
189+
return self.load_from_url(
190+
image_url,
191+
image_io,
192+
fetch_timeout=envs.VLLM_IMAGE_FETCH_TIMEOUT,
193+
)
194+
except UnidentifiedImageError as e:
195+
# convert to ValueError to be properly caught upstream
196+
raise ValueError(str(e)) from e
193197

194198
async def fetch_image_async(
195199
self,
@@ -204,11 +208,15 @@ async def fetch_image_async(
204208
"""
205209
image_io = ImageMediaIO(image_mode=image_mode)
206210

207-
return await self.load_from_url_async(
208-
image_url,
209-
image_io,
210-
fetch_timeout=envs.VLLM_IMAGE_FETCH_TIMEOUT,
211-
)
211+
try:
212+
return await self.load_from_url_async(
213+
image_url,
214+
image_io,
215+
fetch_timeout=envs.VLLM_IMAGE_FETCH_TIMEOUT,
216+
)
217+
except UnidentifiedImageError as e:
218+
# convert to ValueError to be properly caught upstream
219+
raise ValueError(str(e)) from e
212220

213221
def fetch_video(
214222
self,

0 commit comments

Comments
 (0)