diff --git a/tests/entrypoints/openai/chat_completion/test_thinking_token_budget_validation.py b/tests/entrypoints/openai/chat_completion/test_thinking_token_budget_validation.py new file mode 100644 index 0000000000..e66205b7df --- /dev/null +++ b/tests/entrypoints/openai/chat_completion/test_thinking_token_budget_validation.py @@ -0,0 +1,76 @@ +# SPDX-License-Identifier: Apache-2.0 +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project + +import pytest +from pydantic import ValidationError + +from vllm.entrypoints.openai.chat_completion.protocol import ChatCompletionRequest +from vllm.entrypoints.openai.completion.protocol import CompletionRequest + + +@pytest.mark.parametrize("raw_value", [-2, 0.6, 10.5]) +def test_chat_completion_request_rejects_invalid_thinking_token_budget(raw_value): + with pytest.raises(ValidationError, match="thinking_token_budget"): + ChatCompletionRequest.model_validate( + { + "model": "qwen", + "messages": [{"role": "user", "content": "hello"}], + "thinking_token_budget": raw_value, + } + ) + + +def test_chat_completion_request_accepts_valid_thinking_token_budget(): + request = ChatCompletionRequest.model_validate( + { + "model": "qwen", + "messages": [{"role": "user", "content": "hello"}], + "thinking_token_budget": 10, + } + ) + assert request.thinking_token_budget == 10 + + +def test_chat_completion_request_accepts_minus_one_as_unlimited(): + request = ChatCompletionRequest.model_validate( + { + "model": "qwen", + "messages": [{"role": "user", "content": "hello"}], + "thinking_token_budget": -1, + } + ) + assert request.thinking_token_budget is None + + +@pytest.mark.parametrize("raw_value", [0.6, 3.14, -2]) +def test_completion_request_rejects_invalid_thinking_token_budget(raw_value): + with pytest.raises(ValidationError, match="thinking_token_budget"): + CompletionRequest.model_validate( + { + "model": "qwen", + "prompt": "hello", + "thinking_token_budget": raw_value, + } + ) + + +def test_completion_request_accepts_valid_thinking_token_budget(): + request = CompletionRequest.model_validate( + { + "model": "qwen", + "prompt": "hello", + "thinking_token_budget": 5, + } + ) + assert request.thinking_token_budget == 5 + + +def test_completion_request_accepts_minus_one_as_unlimited(): + request = CompletionRequest.model_validate( + { + "model": "qwen", + "prompt": "hello", + "thinking_token_budget": -1, + } + ) + assert request.thinking_token_budget is None diff --git a/tests/v1/logits_processors/test_correctness.py b/tests/v1/logits_processors/test_correctness.py index e7ec663020..80083fd57f 100644 --- a/tests/v1/logits_processors/test_correctness.py +++ b/tests/v1/logits_processors/test_correctness.py @@ -20,7 +20,7 @@ from tests.v1.sample.utils import ( ) from vllm.config import VllmConfig from vllm.platforms import current_platform -from vllm.sampling_params import SamplingParams +from vllm.sampling_params import SamplingParams, validate_thinking_token_budget from vllm.utils.platform_utils import is_pin_memory_available from vllm.v1.sample.logits_processor import ( BatchUpdate, @@ -1194,3 +1194,37 @@ def test_thinking_budget_enforced_without_penalties(): "Budget exceeded: in_end should be True so that apply_to_logits " "forces the end token" ) + + +@pytest.mark.parametrize( + ("raw_value", "expected"), + [ + (None, None), + (-1, None), + (10, 10), + (0, 0), + ], +) +def test_validate_thinking_token_budget(raw_value, expected): + assert validate_thinking_token_budget(raw_value) == expected + + +def test_sampling_params_minus_one_normalizes_to_none(): + params = SamplingParams(thinking_token_budget=-1) + assert params.thinking_token_budget is None + + +@pytest.mark.parametrize("invalid_budget", [-2, 0.6, 10.5, True]) +def test_validate_thinking_token_budget_rejects_invalid(invalid_budget): + from vllm.exceptions import VLLMValidationError + + with pytest.raises(VLLMValidationError, match="thinking_token_budget"): + validate_thinking_token_budget(invalid_budget) + + +@pytest.mark.parametrize("invalid_budget", [-2, 0.6, 10.5]) +def test_thinking_budget_invalid_budget_rejected(invalid_budget): + from vllm.exceptions import VLLMValidationError + + with pytest.raises(VLLMValidationError, match="thinking_token_budget"): + SamplingParams(thinking_token_budget=invalid_budget) diff --git a/vllm/entrypoints/openai/chat_completion/protocol.py b/vllm/entrypoints/openai/chat_completion/protocol.py index 921f4a84a2..cada289e10 100644 --- a/vllm/entrypoints/openai/chat_completion/protocol.py +++ b/vllm/entrypoints/openai/chat_completion/protocol.py @@ -41,6 +41,7 @@ from vllm.sampling_params import ( RequestOutputKind, SamplingParams, StructuredOutputsParams, + ThinkingTokenBudget, ) from vllm.utils import random_uuid @@ -225,7 +226,7 @@ class ChatCompletionRequest(OpenAIBaseModel): "part of the standard OpenAI API specification." ), ) - thinking_token_budget: int | None = None + thinking_token_budget: ThinkingTokenBudget = None include_reasoning: bool = True parallel_tool_calls: bool | None = True diff --git a/vllm/entrypoints/openai/completion/protocol.py b/vllm/entrypoints/openai/completion/protocol.py index cd8d9b89df..cb793a4156 100644 --- a/vllm/entrypoints/openai/completion/protocol.py +++ b/vllm/entrypoints/openai/completion/protocol.py @@ -29,6 +29,7 @@ from vllm.sampling_params import ( RequestOutputKind, SamplingParams, StructuredOutputsParams, + ThinkingTokenBudget, ) from vllm.utils import random_uuid @@ -185,11 +186,12 @@ class CompletionRequest(OpenAIBaseModel): "can detect such behavior and terminate early, saving time and tokens.", ) - thinking_token_budget: int | None = Field( + thinking_token_budget: ThinkingTokenBudget = Field( default=None, description=( "Maximum number of tokens allowed for thinking operations " - "(reasoning models). -1 = unlimited." + "(reasoning models). Non-negative integer sets the limit; " + "-1 means unlimited (treated as unset)." ), ) diff --git a/vllm/sampling_params.py b/vllm/sampling_params.py index e24bd4eef0..6e0be9dbff 100644 --- a/vllm/sampling_params.py +++ b/vllm/sampling_params.py @@ -7,9 +7,10 @@ import json as json_mod from dataclasses import field from enum import Enum, IntEnum from functools import cached_property -from typing import Any +from typing import Annotated, Any import msgspec +from pydantic import BeforeValidator from pydantic.dataclasses import dataclass import vllm.envs as envs @@ -30,6 +31,35 @@ MAX_LOGPROB_TOKEN_IDS = 128 the per-request row width allocated by the sampler's `LogprobTokenIdsState`.""" +def validate_thinking_token_budget(value: int | float | bool | None) -> int | None: + """Validate ``thinking_token_budget``; return ``None`` if unset.""" + if value is None: + return None + if isinstance(value, (bool, float)) or not isinstance(value, int): + raise VLLMValidationError( + "`thinking_token_budget` must be a non-negative integer " + "or -1 for unlimited.", + parameter="thinking_token_budget", + value=value, + ) + if value == -1: + return None + if value < 0: + raise VLLMValidationError( + "`thinking_token_budget` must be a non-negative integer " + "or -1 for unlimited.", + parameter="thinking_token_budget", + value=value, + ) + return value + + +ThinkingTokenBudget = Annotated[ + int | None, + BeforeValidator(validate_thinking_token_budget), +] + + class SamplingType(IntEnum): GREEDY = 0 RANDOM = 1 @@ -409,6 +439,10 @@ class SamplingParams( if self.seed == -1: self.seed = None + self.thinking_token_budget = validate_thinking_token_budget( + self.thinking_token_budget + ) + if self.stop is None: self.stop = [] elif isinstance(self.stop, str):