[Frontend] Make finish_reason and Responses item status truthful (#59324)
Signed-off-by: Flora Feng <4florafeng@gmail.com> Signed-off-by: sfeng33 <4florafeng@gmail.com> Co-authored-by: Chauncey <chaunceyjiang@gmail.com>
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
|
||||
import json
|
||||
from collections.abc import Iterable
|
||||
from typing import Any
|
||||
from typing import Any, Literal
|
||||
|
||||
import partial_json_parser
|
||||
from openai.types.chat import (
|
||||
@@ -100,16 +100,30 @@ def _item_dict(item: ResponseInputOutputItem) -> dict[str, Any]:
|
||||
return item if isinstance(item, dict) else item.model_dump()
|
||||
|
||||
|
||||
def response_item_status(
|
||||
finish_reason: str | None,
|
||||
) -> Literal["completed", "incomplete"]:
|
||||
"""Status of the output item that ended a generation with ``finish_reason``."""
|
||||
return "incomplete" if finish_reason == "length" else "completed"
|
||||
|
||||
|
||||
def build_response_output_items(
|
||||
reasoning: str | None,
|
||||
content: str | None,
|
||||
tool_calls: list[FunctionCall] | None,
|
||||
logprobs: list[Logprob] | None = None,
|
||||
tools: list[Tool] | None = None,
|
||||
finish_reason: str | None = None,
|
||||
) -> list[ResponseOutputItem]:
|
||||
"""Build the output items of one generation.
|
||||
|
||||
Only the last item can have been cut short, so ``finish_reason`` decides
|
||||
that item's status and every earlier item is ``completed``.
|
||||
"""
|
||||
outputs: list[ResponseOutputItem] = []
|
||||
tool_call_name_map = build_responses_tool_call_name_map(tools)
|
||||
custom_names = custom_tool_names(tools)
|
||||
last_item_status = response_item_status(finish_reason)
|
||||
|
||||
if reasoning:
|
||||
outputs.append(
|
||||
@@ -137,13 +151,14 @@ def build_response_output_items(
|
||||
)
|
||||
],
|
||||
role="assistant",
|
||||
status="completed",
|
||||
status="completed" if tool_calls else last_item_status,
|
||||
type="message",
|
||||
)
|
||||
)
|
||||
|
||||
if tool_calls:
|
||||
for idx, tool_call in enumerate(tool_calls):
|
||||
is_last = idx == len(tool_calls) - 1
|
||||
call_id = tool_call.id or make_tool_call_id(
|
||||
func_name=tool_call.name, idx=idx
|
||||
)
|
||||
@@ -166,7 +181,7 @@ def build_response_output_items(
|
||||
id=f"fc_{random_uuid()}",
|
||||
call_id=call_id,
|
||||
type="function_call",
|
||||
status="completed",
|
||||
status=last_item_status if is_last else "completed",
|
||||
name=call_name.name,
|
||||
namespace=call_name.namespace,
|
||||
arguments=tool_call.arguments,
|
||||
|
||||
Reference in New Issue
Block a user