From 6c427dd40141870b9076c9a9f128eec3a7ce86bc Mon Sep 17 00:00:00 2001 From: jack Date: Tue, 23 Jun 2026 13:43:53 +0800 Subject: [PATCH] [BugFix] Omit empty tool_calls from OpenAI chat responses (#44105) Signed-off-by: QwertyJack <7554089+QwertyJack@users.noreply.github.com> Signed-off-by: Chauncey Co-authored-by: QwertyJack <7554089+QwertyJack@users.noreply.github.com> Co-authored-by: Chauncey --- .../test_completion_with_function_calling.py | 4 +- .../chat_completion/test_serving_chat.py | 2 +- .../openai/test_tool_choice_content_none.py | 117 +++++++++++++++++- .../openai/chat_completion/protocol.py | 7 ++ vllm/entrypoints/openai/engine/protocol.py | 7 ++ 5 files changed, 133 insertions(+), 4 deletions(-) diff --git a/tests/entrypoints/openai/chat_completion/test_completion_with_function_calling.py b/tests/entrypoints/openai/chat_completion/test_completion_with_function_calling.py index a3e05027b3..62e4965b8e 100644 --- a/tests/entrypoints/openai/chat_completion/test_completion_with_function_calling.py +++ b/tests/entrypoints/openai/chat_completion/test_completion_with_function_calling.py @@ -540,8 +540,8 @@ async def test_max_tokens_with_tool_choice_required( tool_choice=tool_choice, ) # When `tool_choice="required"` and the tokens of `tools` exceed `max_tokens`, - # both `tool_calls` and `content` should be empty. + # `tool_calls` should be absent and `content` should be empty. # This behavior should be consistent with OpenAI. choice = chat_completion.choices[0] assert choice.finish_reason == "length" - assert len(choice.message.tool_calls) == 0 + assert choice.message.tool_calls is None diff --git a/tests/entrypoints/openai/chat_completion/test_serving_chat.py b/tests/entrypoints/openai/chat_completion/test_serving_chat.py index a12662ec7f..20fa75a770 100644 --- a/tests/entrypoints/openai/chat_completion/test_serving_chat.py +++ b/tests/entrypoints/openai/chat_completion/test_serving_chat.py @@ -465,7 +465,7 @@ class TestGPTOSSChat: ) msg = tool_choice_none.choices[0].message - assert len(msg.tool_calls) == 0 + assert msg.tool_calls is None class TestGPTOSSSpeculativeChat: diff --git a/tests/entrypoints/openai/test_tool_choice_content_none.py b/tests/entrypoints/openai/test_tool_choice_content_none.py index ec66ff3ad4..20faec5a53 100644 --- a/tests/entrypoints/openai/test_tool_choice_content_none.py +++ b/tests/entrypoints/openai/test_tool_choice_content_none.py @@ -2,8 +2,25 @@ # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import pytest +from openai.types.chat.chat_completion import ChatCompletion as OpenAIChatCompletion +from openai.types.chat.chat_completion_chunk import ChatCompletionChunk -from vllm.entrypoints.openai.chat_completion.protocol import ChatCompletionRequest +from vllm.entrypoints.openai.chat_completion.protocol import ( + ChatCompletionRequest, + ChatCompletionResponse, + ChatCompletionResponseChoice, + ChatCompletionResponseStreamChoice, + ChatCompletionStreamResponse, + ChatMessage, +) +from vllm.entrypoints.openai.engine.protocol import ( + DeltaFunctionCall, + DeltaMessage, + DeltaToolCall, + FunctionCall, + ToolCall, + UsageInfo, +) from vllm.entrypoints.openai.responses.protocol import ResponsesRequest from vllm.parser.abstract_parser import DelegatingParser @@ -86,3 +103,101 @@ def test_responses_parser_allows_named_tool_choice_with_none_content(): assert content is None assert tool_calls == [] + + +def _chat_response(message: ChatMessage) -> ChatCompletionResponse: + return ChatCompletionResponse( + model="test-model", + choices=[ + ChatCompletionResponseChoice( + index=0, + message=message, + finish_reason="stop", + ) + ], + usage=UsageInfo(prompt_tokens=1, completion_tokens=1, total_tokens=2), + ) + + +def test_chat_completion_response_omits_empty_tool_calls_payload(): + response = _chat_response(ChatMessage(role="assistant", content="done")) + + payload = response.model_dump() + payload_exclude_unset = response.model_dump(exclude_unset=True) + + assert "tool_calls" not in payload["choices"][0]["message"] + assert "tool_calls" not in payload_exclude_unset["choices"][0]["message"] + parsed = OpenAIChatCompletion.model_validate(payload) + assert parsed.choices[0].message.tool_calls is None + + +def test_chat_completion_response_keeps_non_empty_tool_calls_payload(): + response = _chat_response( + ChatMessage( + role="assistant", + content="", + tool_calls=[ + ToolCall( + function=FunctionCall( + name="get_weather", + arguments='{"city": "Beijing"}', + ) + ) + ], + ) + ) + + message = response.model_dump()["choices"][0]["message"] + + assert len(message["tool_calls"]) == 1 + assert message["tool_calls"][0]["function"]["name"] == "get_weather" + + +def _stream_response(delta: DeltaMessage) -> ChatCompletionStreamResponse: + return ChatCompletionStreamResponse( + id="chatcmpl-test", + object="chat.completion.chunk", + created=1, + model="test-model", + choices=[ + ChatCompletionResponseStreamChoice( + index=0, + delta=delta, + finish_reason=None, + ) + ], + ) + + +def test_chat_completion_stream_response_omits_empty_tool_calls_payload(): + response = _stream_response(DeltaMessage(content="done")) + + payload = response.model_dump(exclude_unset=True) + payload_json = response.model_dump_json(exclude_unset=True) + + assert "tool_calls" not in payload["choices"][0]["delta"] + parsed = ChatCompletionChunk.model_validate_json(payload_json) + assert parsed.choices[0].delta.tool_calls is None + + +def test_chat_completion_stream_response_keeps_non_empty_tool_calls_payload(): + response = _stream_response( + DeltaMessage( + tool_calls=[ + DeltaToolCall( + index=0, + id="call-test", + type="function", + function=DeltaFunctionCall( + name="get_weather", + arguments='{"city": "Beijing"}', + ), + ) + ] + ) + ) + + delta = response.model_dump(exclude_unset=True)["choices"][0]["delta"] + + assert len(delta["tool_calls"]) == 1 + assert delta["tool_calls"][0]["function"]["name"] == "get_weather" diff --git a/vllm/entrypoints/openai/chat_completion/protocol.py b/vllm/entrypoints/openai/chat_completion/protocol.py index 3457aa12f4..09ce8bf8da 100644 --- a/vllm/entrypoints/openai/chat_completion/protocol.py +++ b/vllm/entrypoints/openai/chat_completion/protocol.py @@ -66,6 +66,13 @@ class ChatMessage(OpenAIBaseModel): # vLLM-specific fields that are not in OpenAI spec reasoning: str | None = None + @model_serializer(mode="wrap") + def _serialize(self, handler): + data = handler(self) + if len(data.get("tool_calls", [])) == 0: + data.pop("tool_calls", None) + return data + class ChatCompletionLogProb(OpenAIBaseModel): token: str diff --git a/vllm/entrypoints/openai/engine/protocol.py b/vllm/entrypoints/openai/engine/protocol.py index d86c77561d..084d8d429a 100644 --- a/vllm/entrypoints/openai/engine/protocol.py +++ b/vllm/entrypoints/openai/engine/protocol.py @@ -353,6 +353,13 @@ class DeltaMessage(OpenAIBaseModel): reasoning: str | None = None tool_calls: list[DeltaToolCall] = Field(default_factory=list) + @model_serializer(mode="wrap") + def _serialize(self, handler): + data = handler(self) + if len(data.get("tool_calls", [])) == 0: + data.pop("tool_calls", None) + return data + class GenerationError(Exception): """raised when finish_reason indicates internal server error (500)"""