[gpt-oss][2/N] Support input_messages in responsesRequest (#26962)

Signed-off-by: Andrew Xia <axia@fb.com>
Co-authored-by: Andrew Xia <axia@fb.com>
This commit is contained in:
Andrew Xia
2025-10-27 23:15:49 +00:00
committed by GitHub
co-authored by Andrew Xia
parent 69f064062b
commit 53a56e658b
7 changed files with 481 additions and 17 deletions
+38 -1
View File
@@ -38,11 +38,14 @@ from openai_harmony import (
ToolDescription,
load_harmony_encoding,
)
from openai_harmony import Message as OpenAIHarmonyMessage
from openai_harmony import Role as OpenAIHarmonyRole
from vllm import envs
from vllm.entrypoints.openai.protocol import (
ChatCompletionToolsParam,
ResponseInputOutputItem,
ResponsesRequest,
)
from vllm.utils import random_uuid
@@ -228,7 +231,7 @@ def parse_response_input(
return msg
def parse_chat_input(chat_msg) -> list[Message]:
def parse_input_to_harmony_message(chat_msg) -> list[Message]:
if not isinstance(chat_msg, dict):
# Handle Pydantic models
chat_msg = chat_msg.model_dump(exclude_none=True)
@@ -279,6 +282,40 @@ def parse_chat_input(chat_msg) -> list[Message]:
return [msg]
def construct_harmony_previous_input_messages(
request: ResponsesRequest,
) -> list[OpenAIHarmonyMessage]:
messages: list[OpenAIHarmonyMessage] = []
if request.previous_input_messages:
for message in request.previous_input_messages:
# Handle both OpenAIHarmonyMessage objects and dictionary inputs
if isinstance(message, OpenAIHarmonyMessage):
message_role = message.author.role
# To match OpenAI, instructions, reasoning and tools are
# always taken from the most recent Responses API request
# not carried over from previous requests
if (
message_role == OpenAIHarmonyRole.SYSTEM
or message_role == OpenAIHarmonyRole.DEVELOPER
):
continue
messages.append(message)
else:
harmony_messages = parse_input_to_harmony_message(message)
for harmony_msg in harmony_messages:
message_role = harmony_msg.author.role
# To match OpenAI, instructions, reasoning and tools are
# always taken from the most recent Responses API request
# not carried over from previous requests
if (
message_role == OpenAIHarmonyRole.SYSTEM
or message_role == OpenAIHarmonyRole.DEVELOPER
):
continue
messages.append(harmony_msg)
return messages
def render_for_completion(messages: list[Message]) -> list[int]:
conversation = Conversation.from_messages(messages)
token_ids = get_encoding().render_conversation_for_completion(
+7 -1
View File
@@ -47,6 +47,7 @@ from openai.types.responses import (
from openai.types.responses.response_reasoning_item import (
Content as ResponseReasoningTextContent,
)
from openai_harmony import Message as OpenAIHarmonyMessage
from vllm.utils.serial_utils import (
EmbedDType,
@@ -383,10 +384,15 @@ class ResponsesRequest(OpenAIBaseModel):
default=False,
description=(
"Dictates whether or not to return messages as part of the "
"response object. Currently only supported for non-streaming "
"response object. Currently only supported for"
"non-background and gpt-oss only. "
),
)
# similar to input_messages / output_messages in ResponsesResponse
# we take in previous_input_messages (ie in harmony format)
# this cannot be used in conjunction with previous_response_id
# TODO: consider supporting non harmony messages as well
previous_input_messages: list[OpenAIHarmonyMessage | dict] | None = None
# --8<-- [end:responses-extra-params]
_DEFAULT_SAMPLING_PARAMS = {
+21 -15
View File
@@ -27,8 +27,8 @@ from vllm.entrypoints.harmony_utils import (
get_stop_tokens_for_assistant_actions,
get_streamable_parser_for_assistant,
get_system_message,
parse_chat_input,
parse_chat_output,
parse_input_to_harmony_message,
render_for_completion,
)
from vllm.entrypoints.logger import RequestLogger
@@ -1351,11 +1351,13 @@ class OpenAIServingChat(OpenAIServing):
index=output.index,
message=message,
logprobs=logprobs,
finish_reason="tool_calls"
if (tool_call_info is not None and tool_call_info.tools_called)
else output.finish_reason
if output.finish_reason
else "stop",
finish_reason=(
"tool_calls"
if (tool_call_info is not None and tool_call_info.tools_called)
else output.finish_reason
if output.finish_reason
else "stop"
),
stop_reason=output.stop_reason,
)
choices.append(choice_data)
@@ -1522,11 +1524,13 @@ class OpenAIServingChat(OpenAIServing):
index=output.index,
message=message,
logprobs=logprobs,
finish_reason="tool_calls"
if auto_tools_called
else output.finish_reason
if output.finish_reason
else "stop",
finish_reason=(
"tool_calls"
if auto_tools_called
else output.finish_reason
if output.finish_reason
else "stop"
),
stop_reason=output.stop_reason,
token_ids=(
as_list(output.token_ids) if request.return_token_ids else None
@@ -1685,9 +1689,11 @@ class OpenAIServingChat(OpenAIServing):
should_return_as_token_id,
),
logprob=max(step_token.logprob, -9999.0),
bytes=None
if step_decoded is None
else list(step_decoded.encode("utf-8", errors="replace")),
bytes=(
None
if step_decoded is None
else list(step_decoded.encode("utf-8", errors="replace"))
),
top_logprobs=self._get_top_logprobs(
step_top_logprobs,
num_output_top_logprobs,
@@ -1764,7 +1770,7 @@ class OpenAIServingChat(OpenAIServing):
# Add user message.
for chat_msg in request.messages:
messages.extend(parse_chat_input(chat_msg))
messages.extend(parse_input_to_harmony_message(chat_msg))
# Render prompt token ids.
prompt_token_ids = render_for_completion(messages)
@@ -63,6 +63,7 @@ from vllm.entrypoints.context import (
StreamingHarmonyContext,
)
from vllm.entrypoints.harmony_utils import (
construct_harmony_previous_input_messages,
get_developer_message,
get_stop_tokens_for_assistant_actions,
get_system_message,
@@ -248,6 +249,13 @@ class OpenAIServingResponses(OpenAIServing):
),
status_code=HTTPStatus.BAD_REQUEST,
)
if request.previous_input_messages and request.previous_response_id:
return self.create_error_response(
err_type="invalid_request_error",
message="Only one of `previous_input_messages` and "
"`previous_response_id` can be set.",
status_code=HTTPStatus.BAD_REQUEST,
)
return None
async def create_responses(
@@ -941,6 +949,8 @@ class OpenAIServingResponses(OpenAIServing):
instructions=request.instructions, tools=request.tools
)
messages.append(dev_msg)
messages += construct_harmony_previous_input_messages(request)
else:
# Continue the previous conversation.
# FIXME(woosuk): Currently, request params like reasoning and