Skip to content

Commit 9add711

Browse files
xianzhiTJustinTong0323
authored andcommitted
Fix the issue of incorrect finish reason in final stream response chunk returned during tool call (sgl-project#7708)
Co-authored-by: Xinyuan Tong <[email protected]>
1 parent d724503 commit 9add711

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

python/sglang/srt/entrypoints/openai/serving_chat.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -484,15 +484,21 @@ async def _generate_chat_stream(
484484

485485
# Handle tool calls
486486
if request.tool_choice != "none" and request.tools:
487-
async for chunk in self._process_tool_call_stream(
487+
async for (
488+
chunk,
489+
tool_call_finish_reason_type,
490+
) in self._process_tool_call_stream(
488491
index,
489492
delta,
490493
parser_dict,
491494
content,
492495
request,
493496
finish_reason_type,
494497
):
495-
yield chunk
498+
if chunk:
499+
yield chunk
500+
finish_reason_type = tool_call_finish_reason_type
501+
496502
else:
497503
# Regular content
498504
if delta or not (
@@ -865,7 +871,7 @@ async def _process_tool_call_stream(
865871
choices=[choice_data],
866872
model=request.model,
867873
)
868-
yield f"data: {chunk.model_dump_json()}\n\n"
874+
yield f"data: {chunk.model_dump_json()}\n\n", finish_reason_type
869875

870876
# Yield tool calls
871877
for call_item in calls:
@@ -920,4 +926,7 @@ async def _process_tool_call_stream(
920926
choices=[choice_data],
921927
model=request.model,
922928
)
923-
yield f"data: {chunk.model_dump_json()}\n\n"
929+
yield f"data: {chunk.model_dump_json()}\n\n", finish_reason_type
930+
931+
if finish_reason_type == "stop":
932+
yield None, "tool_calls"

test/srt/openai_server/function_call/test_openai_function_calling.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,13 @@ def test_function_calling_streaming_simple(self):
159159
"Target function name 'get_current_weather' was not found in the streaming chunks",
160160
)
161161

162+
finish_reason = chunks[-1].choices[0].finish_reason
163+
self.assertEqual(
164+
finish_reason,
165+
"tool_calls",
166+
"Final response of function calling should have finish_reason 'tool_calls'",
167+
)
168+
162169
def test_function_calling_streaming_args_parsing(self):
163170
"""
164171
Test: Whether the function call arguments returned in streaming mode can be correctly concatenated into valid JSON.

0 commit comments

Comments
 (0)