Skip to content

Commit cdf8fed

Browse files
committed
Avoid off-by-one when streaming ongoing tool call arguments
1 parent d1fc90c commit cdf8fed

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

vllm/entrypoints/openai/serving_chat.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,7 @@ async def chat_completion_stream_generator(
735735
tool_messages = []
736736

737737
# Calculate base_index once before the loop
738+
# This represents the number of completed tool calls
738739
base_index = 0
739740
for msg in harmony_parser.messages:
740741
if (msg.channel == "commentary"
@@ -743,6 +744,9 @@ async def chat_completion_stream_generator(
743744
"functions.")):
744745
base_index += 1
745746

747+
# next_tool_index tracks the index for the next NEW tool call
748+
next_tool_index = base_index
749+
746750
for group in groups:
747751
group_channel = group['channel']
748752
group_recipient = group['recipient']
@@ -757,6 +761,7 @@ async def chat_completion_stream_generator(
757761
and group_recipient.startswith("functions.")):
758762

759763
if prev_recipient != group_recipient:
764+
# New tool call - emit the opening message
760765
tool_name = group_recipient.split(
761766
"functions.", 1)[1]
762767
tool_messages.append(DeltaToolCall(
@@ -766,15 +771,19 @@ async def chat_completion_stream_generator(
766771
name=tool_name,
767772
arguments="",
768773
),
769-
index=base_index,
774+
index=next_tool_index,
770775
))
771776
prev_recipient = group_recipient
772-
# Increment index for next tool call
773-
base_index += 1
777+
# Increment for any subsequent new tool calls in this chunk
778+
next_tool_index += 1
774779

775780
if group_text:
781+
# Stream arguments for the ongoing tool call
782+
# The current call index is next_tool_index - 1 if we just
783+
# opened it, OR base_index if continuing from prev chunk
784+
tool_call_index = next_tool_index - 1 if next_tool_index > base_index else base_index
776785
tool_messages.append(DeltaToolCall(
777-
index=base_index - 1, # Use the index of the current tool call
786+
index=tool_call_index,
778787
function=DeltaFunctionCall(
779788
arguments=group_text),
780789
))

0 commit comments

Comments
 (0)