[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:
@@ -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]
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user