[Bugfix] GPT-OSS instruction rendering (#44330)

Signed-off-by: Yifan Zong <yzong@redhat.com>
This commit is contained in:
yzong-rh
2026-06-05 13:52:32 -04:00
committed by GitHub
parent b593396c7a
commit 703fb17b13
9 changed files with 276 additions and 112 deletions
@@ -1364,6 +1364,57 @@ class TestServingChatWithHarmony:
],
)
@pytest.mark.asyncio
async def test_system_message_without_tools(self, serving_chat, stream):
"""Leading system message produces a developer message with
DeveloperContent (# Instructions header)."""
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello"},
]
req = ChatCompletionRequest(model=MODEL_NAME, messages=messages)
input_messages, _ = (
serving_chat.openai_serving_render._make_request_with_harmony(req)
)
verify_harmony_messages(
input_messages,
[
{"role": "system"},
{
"role": "developer",
"instructions": "You are a helpful assistant.",
},
{"role": "user", "content": "Hello"},
],
)
@pytest.mark.asyncio
async def test_system_message_with_tools(self, serving_chat, stream, weather_tools):
"""Leading system message is folded into the developer message
alongside tool definitions."""
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What's the weather?"},
]
req = ChatCompletionRequest(
model=MODEL_NAME, messages=messages, tools=weather_tools
)
input_messages, _ = (
serving_chat.openai_serving_render._make_request_with_harmony(req)
)
verify_harmony_messages(
input_messages,
[
{"role": "system"},
{
"role": "developer",
"instructions": "You are a helpful assistant.",
"tool_definitions": ["get_weather"],
},
{"role": "user", "content": "What's the weather?"},
],
)
@pytest.mark.asyncio
async def test_tool_call_response_with_content(
self, serving_chat, stream, weather_tools, weather_messages_start
@@ -45,6 +45,31 @@ class TestResponseInputToHarmonyRenderParity:
# Single-message cases
# -----------------------------------------------------------------------
def test_developer_message(self):
"""Both APIs must render developer messages identically using
DeveloperContent (with the '# Instructions' header)."""
chat_msgs = parse_chat_input_to_harmony_message(
{"role": "developer", "content": "Be concise."}
)
resp_msgs = [
response_input_to_harmony(
{
"type": "message",
"role": "developer",
"content": "Be concise.",
},
prev_responses=[],
)
]
expected = [{"role": "developer", "instructions": "Be concise."}]
verify_harmony_messages(chat_msgs, expected)
verify_harmony_messages(resp_msgs, expected)
assert render_for_completion([_system()] + chat_msgs) == render_for_completion(
[_system()] + resp_msgs
)
def test_user_message(self):
chat_msgs = parse_chat_input_to_harmony_message(
{"role": "user", "content": "What's the weather in Paris?"}
@@ -2,7 +2,7 @@
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import pytest
from openai_harmony import Message, Role
from openai_harmony import DeveloperContent, Message, Role
from tests.entrypoints.openai.utils import verify_harmony_messages
from vllm.entrypoints.openai.parser.harmony_utils import (
@@ -269,7 +269,8 @@ class TestCommonParseInputToHarmonyMessage:
assert messages[0].recipient == "functions.get_current_time"
def test_system_message(self, parse_function):
"""Test parsing system message."""
"""Test parsing system messages, which are parsed into developer messages
with DeveloperContent."""
chat_msg = {
"role": "system",
"content": "You are a helpful assistant",
@@ -278,9 +279,9 @@ class TestCommonParseInputToHarmonyMessage:
messages = parse_function(chat_msg)
assert len(messages) == 1
# System messages are converted using Message.from_dict
# which should preserve the role
assert messages[0].author.role == Role.SYSTEM
assert messages[0].author.role == Role.DEVELOPER
assert isinstance(messages[0].content[0], DeveloperContent)
assert messages[0].content[0].instructions == "You are a helpful assistant"
def test_developer_message(self, parse_function):
"""Test parsing developer message."""
@@ -293,6 +294,8 @@ class TestCommonParseInputToHarmonyMessage:
assert len(messages) == 1
assert messages[0].author.role == Role.DEVELOPER
assert isinstance(messages[0].content[0], DeveloperContent)
assert messages[0].content[0].instructions == "Use concise language"
def test_user_message_with_string_content(self, parse_function):
"""Test parsing user message with string content."""
@@ -12,7 +12,7 @@ from openai.types.responses import ResponseFunctionToolCall, ResponseReasoningIt
from openai.types.responses.response_reasoning_item import (
Content as ReasoningTextContent,
)
from openai_harmony import Role
from openai_harmony import DeveloperContent, Role
from vllm.entrypoints.openai.responses.harmony import response_input_to_harmony
@@ -65,14 +65,16 @@ class TestResponseInputToHarmonyMessage:
assert msg.content[0].text == "Hello"
def test_system_message(self):
"""System messages carry developer instructions and must be rendered
as developer messages with DeveloperContent."""
msg = response_input_to_harmony(
{"type": "message", "role": "system", "content": "Be helpful."},
prev_responses=[],
)
assert msg.author.role == Role.SYSTEM
assert msg.content[0].text == "Be helpful."
assert msg.channel is None
assert msg.author.role == Role.DEVELOPER
assert isinstance(msg.content[0], DeveloperContent)
assert msg.content[0].instructions == "Be helpful."
def test_assistant_message_gets_final_channel(self):
msg = response_input_to_harmony(
@@ -85,14 +87,16 @@ class TestResponseInputToHarmonyMessage:
assert msg.content[0].text == "The answer is 42."
def test_developer_message_gets_instructions_prefix(self):
"""Developer messages must use DeveloperContent which adds the
'# Instructions' header the model was trained on."""
msg = response_input_to_harmony(
{"type": "message", "role": "developer", "content": "Be concise."},
prev_responses=[],
)
assert msg.author.role == Role.DEVELOPER
assert msg.content[0].text == "Instructions:\nBe concise."
assert msg.channel is None
assert isinstance(msg.content[0], DeveloperContent)
assert msg.content[0].instructions == "Be concise."
def test_message_with_array_content(self):
msg = response_input_to_harmony(
@@ -112,7 +116,9 @@ class TestResponseInputToHarmonyMessage:
assert msg.content[0].text == "Part one. "
assert msg.content[1].text == "Part two."
def test_developer_message_array_content_gets_prefix_on_each_part(self):
def test_developer_message_array_content_concatenated(self):
"""Array content in developer messages is flattened and rendered
via DeveloperContent with the '# Instructions' header."""
msg = response_input_to_harmony(
{
"type": "message",
@@ -125,8 +131,9 @@ class TestResponseInputToHarmonyMessage:
prev_responses=[],
)
assert msg.content[0].text == "Instructions:\nRule 1."
assert msg.content[1].text == "Instructions:\nRule 2."
assert msg.author.role == Role.DEVELOPER
assert isinstance(msg.content[0], DeveloperContent)
assert msg.content[0].instructions == "Rule 1.Rule 2."
# -----------------------------------------------------------------------
# type="reasoning"
+2
View File
@@ -155,6 +155,8 @@ def verify_harmony_messages(
assert msg.content[0].text == expected["content"]
if "content_type" in expected:
assert msg.content_type == expected["content_type"]
if "instructions" in expected:
assert msg.content[0].instructions == expected["instructions"]
if "tool_definitions" in expected:
# Check that the tool definitions match the expected list of tool names
actual_tools = [t.name for t in msg.content[0].tools["functions"].tools]