diff --git a/tests/entrypoints/pooling/embed/test_online.py b/tests/entrypoints/pooling/embed/test_online.py index be600cdc88..10c9bfd3f5 100644 --- a/tests/entrypoints/pooling/embed/test_online.py +++ b/tests/entrypoints/pooling/embed/test_online.py @@ -89,6 +89,17 @@ def hf_model(hf_runner): yield hf_model +@pytest.mark.asyncio +@pytest.mark.parametrize("model_name", [MODEL_NAME]) +async def test_negative_token_ids(client: openai.AsyncOpenAI, model_name: str): + # A negative token id is out of vocabulary just like an over-large one, but + # is not caught by the upper-bound check. Unlike the OpenAI completion + # schema, the pooling schema does not constrain token ids to be + # non-negative, so the request reaches the shared engine-level validation. + with pytest.raises(openai.BadRequestError, match=".*out of vocabulary.*"): + await client.embeddings.create(model=model_name, input=[-1]) + + @pytest.mark.asyncio @pytest.mark.parametrize("model_name", [MODEL_NAME]) async def test_basic( diff --git a/vllm/v1/engine/input_processor.py b/vllm/v1/engine/input_processor.py index 686044a403..ac870c931a 100644 --- a/vllm/v1/engine/input_processor.py +++ b/vllm/v1/engine/input_processor.py @@ -483,6 +483,7 @@ class InputProcessor: if prompt_ids and tokenizer is not None: max_input_id = max(prompt_ids, default=0) + min_input_id = min(prompt_ids, default=0) # NOTE: tokenizer.max_token_id is the tokenizer’s vocab size while # self.model_config.get_vocab_size() is the model’s vocab size. @@ -495,6 +496,15 @@ class InputProcessor: # Here we take the max of the two to determine if a token id is # truly out-of-vocabulary. model_vocab_size = model_config.get_vocab_size() + # A negative id is out of vocabulary just like an over-large one, + # but is not caught by the upper-bound check below. Reject it here + # so it is not used as an embedding index downstream. This + # validation path is shared by generate, embedding and pooling + # requests, so the check covers all three. + if min_input_id < 0: + raise VLLMValidationError( + f"Token id {min_input_id} is out of vocabulary" + ) if max_input_id > max(tokenizer.max_token_id, model_vocab_size - 1): raise VLLMValidationError( f"Token id {max_input_id} is out of vocabulary"