[Frontend] Align finish_reason when tool is called with OpenAI (#25054)

Signed-off-by: Sungyoon Jeong <sungyoon.jeong@furiosa.ai>
Co-authored-by: Chauncey <chaunceyjiang@gmail.com>
This commit is contained in:
Sungyoon Jeong 2025-11-03 13:21:18 +09:00 committed by GitHub
parent 1bf43ae35d
commit 470ad118b6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1170,9 +1170,13 @@ class OpenAIServingChat(OpenAIServing):
) )
# Send the finish response for each request.n only once # Send the finish response for each request.n only once
# In OpenAI's API, when a tool is called, the
# finish_reason is:
# "tool_calls" for "auto" or "required" tool calls,
# and "stop" for named tool calls.
if ( if (
auto_tools_called auto_tools_called
or tools_streamed[i] or (tools_streamed[i] and not tool_choice_function_name)
or (self.use_harmony and harmony_tools_streamed[i]) or (self.use_harmony and harmony_tools_streamed[i])
): ):
finish_reason_ = "tool_calls" finish_reason_ = "tool_calls"
@ -1523,18 +1527,24 @@ class OpenAIServingChat(OpenAIServing):
message = ChatMessage( message = ChatMessage(
role=role, reasoning_content=reasoning_content, content=content role=role, reasoning_content=reasoning_content, content=content
) )
# In OpenAI's API, when a tool is called, the finish_reason is:
# "tool_calls" for "auto" or "required" tool calls,
# and "stop" for named tool calls.
is_finish_reason_tool_calls = auto_tools_called or (
request.tool_choice
and request.tool_choice == "required"
and output.finish_reason == "stop"
)
choice_data = ChatCompletionResponseChoice( choice_data = ChatCompletionResponseChoice(
index=output.index, index=output.index,
message=message, message=message,
logprobs=logprobs, logprobs=logprobs,
finish_reason=( finish_reason="tool_calls"
"tool_calls" if is_finish_reason_tool_calls
if auto_tools_called else output.finish_reason
else output.finish_reason if output.finish_reason
if output.finish_reason else "stop",
else "stop"
),
stop_reason=output.stop_reason, stop_reason=output.stop_reason,
token_ids=( token_ids=(
as_list(output.token_ids) if request.return_token_ids else None as_list(output.token_ids) if request.return_token_ids else None