Skip to content

Commit 470ad11

Browse files
[Frontend] Align finish_reason when tool is called with OpenAI (#25054)
Signed-off-by: Sungyoon Jeong <[email protected]> Co-authored-by: Chauncey <[email protected]>
1 parent 1bf43ae commit 470ad11

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

vllm/entrypoints/openai/serving_chat.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,9 +1170,13 @@ async def chat_completion_stream_generator(
11701170
)
11711171

11721172
# Send the finish response for each request.n only once
1173+
# In OpenAI's API, when a tool is called, the
1174+
# finish_reason is:
1175+
# "tool_calls" for "auto" or "required" tool calls,
1176+
# and "stop" for named tool calls.
11731177
if (
11741178
auto_tools_called
1175-
or tools_streamed[i]
1179+
or (tools_streamed[i] and not tool_choice_function_name)
11761180
or (self.use_harmony and harmony_tools_streamed[i])
11771181
):
11781182
finish_reason_ = "tool_calls"
@@ -1523,18 +1527,24 @@ async def chat_completion_full_generator(
15231527
message = ChatMessage(
15241528
role=role, reasoning_content=reasoning_content, content=content
15251529
)
1530+
# In OpenAI's API, when a tool is called, the finish_reason is:
1531+
# "tool_calls" for "auto" or "required" tool calls,
1532+
# and "stop" for named tool calls.
1533+
is_finish_reason_tool_calls = auto_tools_called or (
1534+
request.tool_choice
1535+
and request.tool_choice == "required"
1536+
and output.finish_reason == "stop"
1537+
)
15261538

15271539
choice_data = ChatCompletionResponseChoice(
15281540
index=output.index,
15291541
message=message,
15301542
logprobs=logprobs,
1531-
finish_reason=(
1532-
"tool_calls"
1533-
if auto_tools_called
1534-
else output.finish_reason
1535-
if output.finish_reason
1536-
else "stop"
1537-
),
1543+
finish_reason="tool_calls"
1544+
if is_finish_reason_tool_calls
1545+
else output.finish_reason
1546+
if output.finish_reason
1547+
else "stop",
15381548
stop_reason=output.stop_reason,
15391549
token_ids=(
15401550
as_list(output.token_ids) if request.return_token_ids else None

0 commit comments

Comments
 (0)