From 33f36d42605a476a09ed75936e7c931cb8b432c5 Mon Sep 17 00:00:00 2001 From: Bugen Zhao Date: Wed, 29 Apr 2026 19:03:47 +0800 Subject: [PATCH] [DSV4] Support `max` reasoning effort (#40982) Signed-off-by: Bugen Zhao --- .../openai/chat_completion/test_chat.py | 25 +++++++ .../openai/parser/test_harmony_utils.py | 7 ++ tests/tokenizers_/test_deepseek_v4.py | 67 ++++++++++++++++++- .../openai/chat_completion/protocol.py | 14 +++- .../openai/parser/harmony_utils.py | 9 ++- vllm/tokenizers/deepseek_v4.py | 10 ++- 6 files changed, 126 insertions(+), 6 deletions(-) diff --git a/tests/entrypoints/openai/chat_completion/test_chat.py b/tests/entrypoints/openai/chat_completion/test_chat.py index 80f54f6800..6703095aec 100644 --- a/tests/entrypoints/openai/chat_completion/test_chat.py +++ b/tests/entrypoints/openai/chat_completion/test_chat.py @@ -1002,6 +1002,31 @@ def test_chat_completion_request_n_parameter_default(): assert sampling_params.n == 1, f"Expected n=1 (default), got n={sampling_params.n}" +def test_chat_completion_request_accepts_model_specific_reasoning_effort(): + request = ChatCompletionRequest( + model="test-model", + messages=[{"role": "user", "content": "Hello"}], + reasoning_effort="max", + ) + + chat_params = request.build_chat_params( + default_template=None, + default_template_content_format="auto", + ) + + assert request.reasoning_effort == "max" + assert chat_params.chat_template_kwargs["reasoning_effort"] == "max" + + +def test_chat_completion_request_rejects_unknown_reasoning_effort(): + with pytest.raises(ValueError, match="Input should be"): + ChatCompletionRequest( + model="test-model", + messages=[{"role": "user", "content": "Hello"}], + reasoning_effort="extra_high", + ) + + def test_chat_completion_request_n_parameter_various_values(): """Test n parameter with various values.""" for n_value in [1, 2, 5, 10]: diff --git a/tests/entrypoints/openai/parser/test_harmony_utils.py b/tests/entrypoints/openai/parser/test_harmony_utils.py index 21b53dff15..69b9f101ea 100644 --- a/tests/entrypoints/openai/parser/test_harmony_utils.py +++ b/tests/entrypoints/openai/parser/test_harmony_utils.py @@ -843,6 +843,13 @@ class TestGetSystemMessage: f"{channel} missing when with_custom_tools={with_tools}" ) + def test_unsupported_reasoning_effort_raises_clear_error(self) -> None: + with pytest.raises( + ValueError, + match="reasoning_effort='max' is not supported by Harmony", + ): + get_system_message(reasoning_effort="max") + class TestResponseInputToHarmonyReasoningItem: """Tests for response_input_to_harmony handling of reasoning input items. diff --git a/tests/tokenizers_/test_deepseek_v4.py b/tests/tokenizers_/test_deepseek_v4.py index 9f3b88cf65..f8099a5463 100644 --- a/tests/tokenizers_/test_deepseek_v4.py +++ b/tests/tokenizers_/test_deepseek_v4.py @@ -182,7 +182,7 @@ def test_deepseek_v4_renders_parsed_history_tool_arguments(): assert 'parameter name="arguments"' not in prompt -@pytest.mark.parametrize("reasoning_effort", ["none", "low", "medium", "high"]) +@pytest.mark.parametrize("reasoning_effort", ["minimal", "low", "medium", "high"]) def test_deepseek_v4_accepts_openai_reasoning_effort_values(reasoning_effort): prompt = _tokenizer().apply_chat_template( [{"role": "user", "content": "Hello"}], @@ -195,6 +195,58 @@ def test_deepseek_v4_accepts_openai_reasoning_effort_values(reasoning_effort): assert "Reasoning Effort: Absolute maximum" not in prompt +def test_deepseek_v4_none_reasoning_effort_disables_thinking(): + prompt = _tokenizer().apply_chat_template( + [{"role": "user", "content": "Hello"}], + tokenize=False, + enable_thinking=True, + reasoning_effort="none", + ) + + assert prompt == ("<|begin▁of▁sentence|><|User|>Hello<|Assistant|>") + + +@pytest.mark.parametrize( + ("reasoning_effort", "expected_mode", "expected_effort"), + [ + ("none", "chat", None), + ("minimal", "thinking", "high"), + ("low", "thinking", "high"), + ("medium", "thinking", "high"), + ("high", "thinking", "high"), + ("xhigh", "thinking", "max"), + ("max", "thinking", "max"), + ("unexpected", "thinking", "high"), + ], +) +def test_deepseek_v4_maps_compatible_thinking_reasoning_effort_values( + monkeypatch: pytest.MonkeyPatch, + reasoning_effort, + expected_mode, + expected_effort, +): + captured_kwargs = [] + + def fake_encode_messages(messages, **kwargs): + captured_kwargs.append(kwargs) + return "prompt" + + monkeypatch.setattr( + "vllm.tokenizers.deepseek_v4.encode_messages", + fake_encode_messages, + ) + + _tokenizer().apply_chat_template( + [{"role": "user", "content": "Hello"}], + tokenize=False, + enable_thinking=True, + reasoning_effort=reasoning_effort, + ) + + assert captured_kwargs[-1]["thinking_mode"] == expected_mode + assert captured_kwargs[-1]["reasoning_effort"] == expected_effort + + def test_deepseek_v4_preserves_reference_max_reasoning_effort(): prompt = _tokenizer().apply_chat_template( [{"role": "user", "content": "Hello"}], @@ -208,6 +260,19 @@ def test_deepseek_v4_preserves_reference_max_reasoning_effort(): ) +def test_deepseek_v4_maps_xhigh_to_reference_max_reasoning_effort(): + prompt = _tokenizer().apply_chat_template( + [{"role": "user", "content": "Hello"}], + tokenize=False, + enable_thinking=True, + reasoning_effort="xhigh", + ) + + assert prompt.startswith( + "<|begin▁of▁sentence|>Reasoning Effort: Absolute maximum" + ) + + @pytest.mark.parametrize( ("case_id", "kwargs"), [ diff --git a/vllm/entrypoints/openai/chat_completion/protocol.py b/vllm/entrypoints/openai/chat_completion/protocol.py index 01d2df88d6..9f36199235 100644 --- a/vllm/entrypoints/openai/chat_completion/protocol.py +++ b/vllm/entrypoints/openai/chat_completion/protocol.py @@ -182,7 +182,19 @@ class ChatCompletionRequest(OpenAIBaseModel): | ChatCompletionNamedToolChoiceParam | None ) = "none" - reasoning_effort: Literal["none", "low", "medium", "high"] | None = None + reasoning_effort: ( + Literal["none", "minimal", "low", "medium", "high", "xhigh", "max"] | None + ) = Field( + default=None, + description=( + "Constrains effort on reasoning for reasoning models. " + "Currently supported values are none, minimal, low, medium, " + "high, xhigh, and max. Reducing reasoning effort can result in " + "faster responses and fewer tokens used on reasoning in a response. " + "Note that 'max' is specific to the DeepSeek V4 series and is not " + "part of the standard OpenAI API specification." + ), + ) thinking_token_budget: int | None = None include_reasoning: bool = True parallel_tool_calls: bool | None = True diff --git a/vllm/entrypoints/openai/parser/harmony_utils.py b/vllm/entrypoints/openai/parser/harmony_utils.py index 7550a02867..33e68b4c40 100644 --- a/vllm/entrypoints/openai/parser/harmony_utils.py +++ b/vllm/entrypoints/openai/parser/harmony_utils.py @@ -3,7 +3,6 @@ import datetime from collections.abc import Iterable, Sequence -from typing import Literal from openai.types.responses.tool import Tool from openai_harmony import ( @@ -66,7 +65,7 @@ def get_encoding(): def get_system_message( model_identity: str | None = None, - reasoning_effort: Literal["high", "medium", "low"] | None = None, + reasoning_effort: str | None = None, start_date: str | None = None, browser_description: str | None = None, python_description: str | None = None, @@ -84,6 +83,12 @@ def get_system_message( ) sys_msg_content = sys_msg_content.with_model_identity(new_identity) if reasoning_effort is not None: + if reasoning_effort not in REASONING_EFFORT: + supported_values = ", ".join(REASONING_EFFORT) + raise ValueError( + f"reasoning_effort={reasoning_effort!r} is not supported by " + f"Harmony. Supported values are: {supported_values}." + ) sys_msg_content = sys_msg_content.with_reasoning_effort( REASONING_EFFORT[reasoning_effort] ) diff --git a/vllm/tokenizers/deepseek_v4.py b/vllm/tokenizers/deepseek_v4.py index 76725dab16..2a6aaaf739 100644 --- a/vllm/tokenizers/deepseek_v4.py +++ b/vllm/tokenizers/deepseek_v4.py @@ -40,10 +40,16 @@ def get_deepseek_v4_tokenizer(tokenizer: HfTokenizer) -> HfTokenizer: messages.insert(0, {"role": "system"}) messages[0]["tools"] = tools # type: ignore[typeddict-unknown-key] - # The V4 reference currently accepts only "max", "high", or None. reasoning_effort = kwargs.get("reasoning_effort") - if reasoning_effort not in ("max", "high"): + if not isinstance(reasoning_effort, str): reasoning_effort = None + elif reasoning_effort == "none": + thinking_mode = "chat" + reasoning_effort = None + elif reasoning_effort in ("max", "xhigh"): + reasoning_effort = "max" + else: + reasoning_effort = "high" encode_config = dict( thinking_mode=thinking_mode,