diff --git a/tests/tool_use/test_chat_completion_request_validations.py b/tests/tool_use/test_chat_completion_request_validations.py index 70f4ac2254..d832feda7f 100644 --- a/tests/tool_use/test_chat_completion_request_validations.py +++ b/tests/tool_use/test_chat_completion_request_validations.py @@ -61,3 +61,58 @@ def test_chat_completion_request_with_tool_choice_but_no_tools(tool_choice): "tools": None, } ) + + +def test_reasoning_content_normalized_to_reasoning(): + request = ChatCompletionRequest.model_validate( + { + "messages": [ + {"role": "user", "content": "What is 2+2?"}, + { + "role": "assistant", + "content": "4", + "reasoning_content": "2+2 equals 4", + }, + {"role": "user", "content": "Are you sure?"}, + ], + "model": "facebook/opt-125m", + } + ) + assistant_msg = request.messages[1] + assert assistant_msg.get("reasoning") == "2+2 equals 4" + assert "reasoning_content" not in assistant_msg + + +def test_reasoning_takes_precedence_over_reasoning_content(): + request = ChatCompletionRequest.model_validate( + { + "messages": [ + {"role": "user", "content": "What is 2+2?"}, + { + "role": "assistant", + "content": "4", + "reasoning": "from reasoning field", + "reasoning_content": "from reasoning_content field", + }, + ], + "model": "facebook/opt-125m", + } + ) + assistant_msg = request.messages[1] + assert assistant_msg.get("reasoning") == "from reasoning field" + assert "reasoning_content" not in assistant_msg + + +def test_no_reasoning_fields_unchanged(): + request = ChatCompletionRequest.model_validate( + { + "messages": [ + {"role": "user", "content": "Hello"}, + {"role": "assistant", "content": "Hi"}, + ], + "model": "facebook/opt-125m", + } + ) + assistant_msg = request.messages[1] + assert assistant_msg.get("reasoning") is None + assert "reasoning_content" not in assistant_msg diff --git a/vllm/entrypoints/openai/chat_completion/protocol.py b/vllm/entrypoints/openai/chat_completion/protocol.py index 23ca42a3ce..3e4baa6cb7 100644 --- a/vllm/entrypoints/openai/chat_completion/protocol.py +++ b/vllm/entrypoints/openai/chat_completion/protocol.py @@ -413,13 +413,14 @@ class ChatCompletionRequest(OpenAIBaseModel): @model_validator(mode="before") @classmethod - def _materialize_tool_calls_before(cls, data: Any) -> Any: - """Eagerly convert tool_calls generators/iterators to lists. + def _normalize_messages_before(cls, data: Any) -> Any: + """Pre-process message dicts before Pydantic field validation. - Must run before Pydantic field validation so that one-shot - generators are not consumed during union type matching of - ChatCompletionAssistantMessageParam (which types tool_calls - as Iterable[...]). + Performs two normalizations in a single pass: + - Converts tool_calls generators/iterators to lists so one-shot + generators are not consumed during union type matching. + - Renames the deprecated ``reasoning_content`` field to + ``reasoning`` so downstream code only needs to check one field. """ if not isinstance(data, dict): return data @@ -432,6 +433,9 @@ class ChatCompletionRequest(OpenAIBaseModel): tool_calls = msg.get("tool_calls") if tool_calls is not None and not isinstance(tool_calls, list): msg["tool_calls"] = list(tool_calls) + reasoning_content = msg.pop("reasoning_content", None) + if reasoning_content is not None and msg.get("reasoning") is None: + msg["reasoning"] = reasoning_content return data @model_validator(mode="after")