From 35bc22f23ce3fcd9c57f318836ec102f7c85647a Mon Sep 17 00:00:00 2001 From: Jialin Ouyang Date: Mon, 13 Oct 2025 16:31:35 -0700 Subject: [PATCH] [ResponseAPI] Further polish message serialization and unit tests (#26728) Signed-off-by: Jialin Ouyang --- tests/entrypoints/openai/test_protocol.py | 36 +++++++++++++++++++ .../openai/test_response_api_with_harmony.py | 31 ---------------- vllm/entrypoints/openai/protocol.py | 2 +- 3 files changed, 37 insertions(+), 32 deletions(-) create mode 100644 tests/entrypoints/openai/test_protocol.py diff --git a/tests/entrypoints/openai/test_protocol.py b/tests/entrypoints/openai/test_protocol.py new file mode 100644 index 0000000000000..e9b1cfb58b502 --- /dev/null +++ b/tests/entrypoints/openai/test_protocol.py @@ -0,0 +1,36 @@ +# SPDX-License-Identifier: Apache-2.0 +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project +from openai_harmony import ( + Message, +) + +from vllm.entrypoints.openai.protocol import serialize_message, serialize_messages + + +def test_serialize_message() -> None: + dict_value = {"a": 1, "b": "2"} + assert serialize_message(dict_value) == dict_value + + msg_value = { + "role": "assistant", + "name": None, + "content": [{"type": "text", "text": "Test 1"}], + "channel": "analysis", + } + msg = Message.from_dict(msg_value) + assert serialize_message(msg) == msg_value + + +def test_serialize_messages() -> None: + assert serialize_messages(None) is None + assert serialize_messages([]) is None + + dict_value = {"a": 3, "b": "4"} + msg_value = { + "role": "assistant", + "name": None, + "content": [{"type": "text", "text": "Test 2"}], + "channel": "analysis", + } + msg = Message.from_dict(msg_value) + assert serialize_messages([msg, dict_value]) == [msg_value, dict_value] diff --git a/tests/entrypoints/openai/test_response_api_with_harmony.py b/tests/entrypoints/openai/test_response_api_with_harmony.py index 0720c8aa51219..57d88f84d2519 100644 --- a/tests/entrypoints/openai/test_response_api_with_harmony.py +++ b/tests/entrypoints/openai/test_response_api_with_harmony.py @@ -12,8 +12,6 @@ from openai_harmony import ( Message, ) -from vllm.entrypoints.openai.protocol import serialize_message, serialize_messages - from ...utils import RemoteOpenAIServer MODEL_NAME = "openai/gpt-oss-20b" @@ -760,32 +758,3 @@ async def test_output_messages_enabled(client: OpenAI, model_name: str, server): assert response.status == "completed" assert len(response.input_messages) > 0 assert len(response.output_messages) > 0 - - -def test_serialize_message() -> None: - dict_value = {"a": 1, "b": "2"} - assert serialize_message(dict_value) == dict_value - - msg_value = { - "role": "assistant", - "name": None, - "content": [{"type": "text", "text": "Test 1"}], - "channel": "analysis", - } - msg = Message.from_dict(msg_value) - assert serialize_message(msg) == msg_value - - -def test_serialize_messages() -> None: - assert serialize_messages(None) is None - assert serialize_messages([]) is None - - dict_value = {"a": 3, "b": "4"} - msg_value = { - "role": "assistant", - "name": None, - "content": [{"type": "text", "text": "Test 2"}], - "channel": "analysis", - } - msg = Message.from_dict(msg_value) - assert serialize_messages([msg, dict_value]) == [msg_value, dict_value] diff --git a/vllm/entrypoints/openai/protocol.py b/vllm/entrypoints/openai/protocol.py index de5cf80105a58..1f2c40e703834 100644 --- a/vllm/entrypoints/openai/protocol.py +++ b/vllm/entrypoints/openai/protocol.py @@ -2110,7 +2110,7 @@ def serialize_message(msg): """ if isinstance(msg, dict): return msg - elif hasattr(msg, "__dict__"): + elif hasattr(msg, "to_dict"): return msg.to_dict() else: # fallback to pyandic dump