[Bugfix] Responses API assistant EasyInputMessageParam input (#44361)

Signed-off-by: Yifan Zong <yzong@redhat.com>
Co-authored-by: Ben Browning <bbrownin@redhat.com>
This commit is contained in:
yzong-rh
2026-06-23 08:54:46 -04:00
committed by GitHub
co-authored by Ben Browning
parent 7d47cff933
commit 2a675a7b9f
4 changed files with 93 additions and 13 deletions
@@ -5,7 +5,7 @@
import json
import pytest
from openai.types.responses import ResponseFunctionToolCall
from openai.types.responses import ResponseFunctionToolCall, ResponseOutputMessage
from vllm.entrypoints.openai.responses.protocol import ResponsesRequest
@@ -328,3 +328,52 @@ def test_validator_handles_empty_iterator():
request = ResponsesRequest(**mock_data)
assert request.input == []
def test_assistant_string_content_stays_easyinput():
"""EasyInput assistant message with plain string content is not
coerced into a ResponseOutputMessage."""
request_data = {
"model": "test-model",
"input": [
{"type": "message", "role": "assistant", "content": "hello"},
],
}
request = ResponsesRequest(**request_data)
item = request.input[0]
assert isinstance(item, dict), (
"String-content assistant message should remain a dict (EasyInput), "
f"got {type(item)}"
)
assert item.get("content") == "hello"
assert "id" not in item
assert "status" not in item
def test_assistant_output_style_content_coerced():
"""Assistant message whose content is output-message-shaped (list of
output_text items) should be coerced to ResponseOutputMessage."""
request_data = {
"model": "test-model",
"input": [
{
"type": "message",
"role": "assistant",
"content": [{"type": "output_text", "text": "world"}],
},
],
}
request = ResponsesRequest(**request_data)
item = request.input[0]
assert isinstance(item, ResponseOutputMessage), (
"Output-style assistant message should be coerced to "
f"ResponseOutputMessage, got {type(item)}"
)
assert item.content[0].text == "world"
assert item.content[0].annotations == []
assert item.status == "completed"
assert item.id.startswith("msg_")
@@ -782,6 +782,16 @@ class TestConstructChatMessagesCombinePolicy:
["call_123", "call_456"],
id="reasoning-output-tool-call",
),
pytest.param(
[
make_reasoning_item(content_text="Let me think"),
{"type": "message", "role": "assistant", "content": "Hello"},
],
"Hello",
"Let me think",
None,
id="reasoning-easyinput-assistant",
),
],
)
def test_assistant_side_items_merge_until_tool_output(
+19 -12
View File
@@ -525,23 +525,30 @@ class ResponsesRequest(OpenAIBaseModel):
processed_input.append(item)
elif item_type == "message" and item.get("role") == "assistant":
content = item.get("content")
if not isinstance(content, list):
# String content is a valid EasyInputMessageParam,
# do not coerce it to ResponseOutputMessage
processed_input.append(item)
continue
original_item = item
item = dict(item)
if "id" not in item:
item["id"] = f"msg_{random_uuid()}"
if "status" not in item:
item["status"] = "completed"
# ResponseOutputText requires annotations
if isinstance(item.get("content"), list):
new_content = []
for c in item["content"]:
if (
isinstance(c, dict)
and c.get("type") == "output_text"
and "annotations" not in c
):
c = {**c, "annotations": []}
new_content.append(c)
item["content"] = new_content
new_content = []
for c in content:
if (
isinstance(c, dict)
and c.get("type") == "output_text"
and "annotations" not in c
):
c = {**c, "annotations": []}
new_content.append(c)
item["content"] = new_content
try:
processed_input.append(ResponseOutputMessage(**item))
except ValidationError:
@@ -549,7 +556,7 @@ class ResponsesRequest(OpenAIBaseModel):
"Failed to parse assistant message to ResponseOutputMessage, "
"leaving for Pydantic validation"
)
processed_input.append(item)
processed_input.append(original_item)
else:
processed_input.append(item)
@@ -306,6 +306,20 @@ def _construct_message_from_response_item(
content=item.get("output"),
tool_call_id=item.get("call_id"),
)
elif isinstance(item, dict) and item.get("role") == "assistant":
content = item.get("content")
text: str | None = None
if isinstance(content, str):
text = content
elif isinstance(content, list) and content:
text = content[0].get("text")
if text is not None:
if prev_assistant_msg:
previous_content = prev_assistant_msg.get("content")
if previous_content is None:
prev_assistant_msg["content"] = text
return None
return {"role": "assistant", "content": text}
return item # type: ignore[arg-type]