[Bugfix] Return HTTP 422 for unprocessable image URLs instead of 500 (#47165)

Signed-off-by: Alexis Kinsella <alexis.kinsella@gmail.com>
This commit is contained in:
Alexis K.
2026-07-06 11:56:23 +00:00
committed by GitHub
parent ba22152096
commit 40cc2e8327
5 changed files with 288 additions and 12 deletions
+84 -10
View File
@@ -13,14 +13,17 @@ from pathlib import Path
from typing import Any, TypeVar
from urllib.request import url2pathname
import aiohttp
import numpy as np
import numpy.typing as npt
import requests
import torch
from PIL import Image, UnidentifiedImageError
from urllib3.util import Url, parse_url
import vllm.envs as envs
from vllm.connections import HTTPConnection, global_http_connection
from vllm.exceptions import VLLMUnprocessableEntityError
from vllm.logger import init_logger
from vllm.multimodal.video import get_video_loader_backend_for_processor
from vllm.utils.registry import ExtensionManager
@@ -48,6 +51,65 @@ MODALITY_IO_MAP: dict[str, type[MediaIO]] = {
}
def _wrap_media_fetch_error(
url: str, exc: Exception
) -> VLLMUnprocessableEntityError | Exception:
"""Convert media fetch exceptions to VLLMUnprocessableEntityError.
This handles HTTP errors that indicate the media resource is invalid
(4xx responses except 408/429, malformed URLs) and converts them to a
422 Unprocessable Entity error instead of 500.
Transient errors (5xx, 408, 429, DNS failures, connection errors,
timeouts) are returned as-is to allow retry logic to handle them
appropriately.
Returns:
VLLMUnprocessableEntityError for permanent client errors (4xx except
408/429, invalid URL)
Original exception for transient errors (5xx, 408, 429, network blips)
or other exceptions
"""
if isinstance(exc, aiohttp.ClientResponseError):
if exc.status in (408, 429):
return exc
if exc.status < 500:
return VLLMUnprocessableEntityError(
f"Failed to fetch media from URL: HTTP {exc.status} error",
parameter="image_url",
value=url,
)
return exc
if isinstance(exc, requests.exceptions.HTTPError):
if exc.response is not None:
status_code = exc.response.status_code
if status_code in (408, 429):
return exc
if status_code < 500:
return VLLMUnprocessableEntityError(
f"Failed to fetch media from URL: HTTP {status_code} error",
parameter="image_url",
value=url,
)
return exc
if isinstance(exc, requests.exceptions.InvalidURL):
return VLLMUnprocessableEntityError(
"Failed to fetch media from URL: Invalid URL format",
parameter="image_url",
value=url,
)
if isinstance(exc, ValueError):
return VLLMUnprocessableEntityError(
"Failed to fetch media from URL: Invalid URL",
parameter="image_url",
value=url,
)
return exc
def merge_media_io_kwargs(
defaults: dict[str, dict[str, Any]] | None,
overrides: dict[str, dict[str, Any]] | None,
@@ -303,11 +365,17 @@ class MediaConnector:
return media_io.load_bytes(cached)
connection = self.connection
data = connection.get_bytes(
url_spec.url,
timeout=fetch_timeout,
allow_redirects=envs.VLLM_MEDIA_URL_ALLOW_REDIRECTS,
)
try:
data = connection.get_bytes(
url_spec.url,
timeout=fetch_timeout,
allow_redirects=envs.VLLM_MEDIA_URL_ALLOW_REDIRECTS,
)
except Exception as e:
wrapped = _wrap_media_fetch_error(url, e)
if isinstance(wrapped, VLLMUnprocessableEntityError):
raise wrapped from e
raise
self._put_cached_bytes(url, data)
return media_io.load_bytes(data)
@@ -348,11 +416,17 @@ class MediaConnector:
return await future
connection = self.connection
data = await connection.async_get_bytes(
url_spec.url,
timeout=fetch_timeout,
allow_redirects=envs.VLLM_MEDIA_URL_ALLOW_REDIRECTS,
)
try:
data = await connection.async_get_bytes(
url_spec.url,
timeout=fetch_timeout,
allow_redirects=envs.VLLM_MEDIA_URL_ALLOW_REDIRECTS,
)
except Exception as e:
wrapped = _wrap_media_fetch_error(url, e)
if isinstance(wrapped, VLLMUnprocessableEntityError):
raise wrapped from e
raise
await loop.run_in_executor(
global_thread_pool, self._put_cached_bytes, url, data