From 39eba6ac368133ab4e69a0589fcd53c481dddc44 Mon Sep 17 00:00:00 2001 From: Jensen Chen <71129832+ZHIHANCHEN03@users.noreply.github.com> Date: Sun, 23 Aug 2026 08:43:51 -0700 Subject: [PATCH] [Misc] Use VLLMValidationError in offline inference input validation (#53432) Signed-off-by: Jensen Chen Co-authored-by: Claude Opus 5 --- tests/entrypoints/llm/test_generate.py | 7 +-- tests/entrypoints/test_offline_utils.py | 57 +++++++++++++++++++++++ tests/lora/test_qwen3_with_multi_loras.py | 3 +- vllm/entrypoints/offline_utils.py | 7 +-- 4 files changed, 67 insertions(+), 7 deletions(-) create mode 100644 tests/entrypoints/test_offline_utils.py diff --git a/tests/entrypoints/llm/test_generate.py b/tests/entrypoints/llm/test_generate.py index e79e62b361..4a2c59dc58 100644 --- a/tests/entrypoints/llm/test_generate.py +++ b/tests/entrypoints/llm/test_generate.py @@ -6,6 +6,7 @@ import weakref import pytest from vllm import LLM, SamplingParams +from vllm.exceptions import VLLMValidationError MODEL_NAME = "distilbert/distilgpt2" @@ -52,7 +53,7 @@ def test_multiple_sampling_params(llm: LLM): assert len(PROMPTS) == len(outputs) # Exception raised, if the size of params does not match the size of prompts - with pytest.raises(ValueError): + with pytest.raises(VLLMValidationError): outputs = llm.generate(PROMPTS, sampling_params=sampling_params[:3]) # Single SamplingParams should be applied to every prompt @@ -75,13 +76,13 @@ def test_multiple_priority(llm: LLM): assert len(PROMPTS) == len(outputs) # Exception raised, if the length of priority does not match the length of prompts - with pytest.raises(ValueError): + with pytest.raises(VLLMValidationError): outputs = llm.generate( PROMPTS, sampling_params=None, priority=[0] * (len(PROMPTS) - 1) ) # Exception raised, if the priority list is empty - with pytest.raises(ValueError): + with pytest.raises(VLLMValidationError): outputs = llm.generate(PROMPTS, sampling_params=None, priority=[]) diff --git a/tests/entrypoints/test_offline_utils.py b/tests/entrypoints/test_offline_utils.py new file mode 100644 index 0000000000..39f77672a4 --- /dev/null +++ b/tests/entrypoints/test_offline_utils.py @@ -0,0 +1,57 @@ +# SPDX-License-Identifier: Apache-2.0 +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project + +import pytest + +from vllm import SamplingParams +from vllm.entrypoints.offline_utils import OfflineInferenceMixin +from vllm.exceptions import VLLMValidationError +from vllm.lora.request import LoRARequest + + +@pytest.fixture +def mixin() -> OfflineInferenceMixin: + return object.__new__(OfflineInferenceMixin) + + +def test_rejects_mismatched_params(mixin: OfflineInferenceMixin): + with pytest.raises(VLLMValidationError) as exc_info: + mixin._params_to_seq([SamplingParams()], num_requests=2) + + assert str(exc_info.value) == ( + "The lengths of prompts (2) and params (1) must be the same." + ) + assert exc_info.value.parameter is None + assert exc_info.value.value is None + + +def test_rejects_mismatched_lora_requests(mixin: OfflineInferenceMixin): + with pytest.raises(VLLMValidationError) as exc_info: + mixin._lora_request_to_seq([None], num_requests=2) + + assert str(exc_info.value) == ( + "The lengths of prompts (2) and lora_request (1) must be the same." + ) + assert exc_info.value.parameter is None + assert exc_info.value.value is None + + +def test_rejects_mismatched_priority(mixin: OfflineInferenceMixin): + with pytest.raises(VLLMValidationError) as exc_info: + mixin._priority_to_seq([0], num_requests=2) + + assert str(exc_info.value) == ( + "The lengths of prompts (2) and priority (1) must be the same." + ) + assert exc_info.value.parameter is None + assert exc_info.value.value is None + + +def test_matching_lengths_pass_through(mixin: OfflineInferenceMixin): + lora = LoRARequest("l", 1, "/tmp/l") + + assert mixin._params_to_seq([SamplingParams()], num_requests=1) == [ + SamplingParams() + ] + assert mixin._lora_request_to_seq([lora], num_requests=1) == [lora] + assert mixin._priority_to_seq([3], num_requests=1) == [3] diff --git a/tests/lora/test_qwen3_with_multi_loras.py b/tests/lora/test_qwen3_with_multi_loras.py index 0cc8884aba..9870da6959 100644 --- a/tests/lora/test_qwen3_with_multi_loras.py +++ b/tests/lora/test_qwen3_with_multi_loras.py @@ -12,6 +12,7 @@ import pytest from tests.utils import multi_gpu_test from vllm import LLM, SamplingParams +from vllm.exceptions import VLLMValidationError from vllm.lora.request import LoRARequest MODEL_PATH = "Qwen/Qwen3-0.6B" @@ -192,7 +193,7 @@ def test_multiple_lora_requests(): assert len(PROMPTS) == len(outputs) # Exception raised, if the size of params does not match the size of prompts - with pytest.raises(ValueError): + with pytest.raises(VLLMValidationError): outputs = llm.generate(PROMPTS, lora_request=lora_request[:1]) # Single LoRARequest should be applied to every prompt diff --git a/vllm/entrypoints/offline_utils.py b/vllm/entrypoints/offline_utils.py index 7522532fc4..2b0086d311 100644 --- a/vllm/entrypoints/offline_utils.py +++ b/vllm/entrypoints/offline_utils.py @@ -19,6 +19,7 @@ from vllm.entrypoints.chat_utils import ( ChatCompletionMessageParam, ChatTemplateContentFormatOption, ) +from vllm.exceptions import VLLMValidationError from vllm.inputs import EngineInput from vllm.logger import init_logger from vllm.lora.request import LoRARequest @@ -246,7 +247,7 @@ class OfflineInferenceMixin: ) -> Sequence[_P]: if isinstance(params, Sequence): if len(params) != num_requests: - raise ValueError( + raise VLLMValidationError( f"The lengths of prompts ({num_requests}) " f"and params ({len(params)}) must be the same." ) @@ -262,7 +263,7 @@ class OfflineInferenceMixin: ) -> Sequence[LoRARequest | None]: if isinstance(lora_request, Sequence): if len(lora_request) != num_requests: - raise ValueError( + raise VLLMValidationError( f"The lengths of prompts ({num_requests}) " f"and lora_request ({len(lora_request)}) must be the same." ) @@ -278,7 +279,7 @@ class OfflineInferenceMixin: ) -> Sequence[int]: if priority is not None: if len(priority) != num_requests: - raise ValueError( + raise VLLMValidationError( f"The lengths of prompts ({num_requests}) " f"and priority ({len(priority)}) must be the same." )