Skip to content

Commit 5093cb4

Browse files
committed
Avoid off-by-one when streaming ongoing tool call arguments
1 parent c7aa1f4 commit 5093cb4

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
@@ -810,6 +810,7 @@ async def chat_completion_stream_generator(
810810
tool_messages = []
811811

812812
# Calculate base_index once before the loop
813+
# This represents the number of completed tool calls
813814
base_index = 0
814815
for msg in harmony_parser.messages:
815816
if (msg.channel == "commentary"
@@ -818,6 +819,9 @@ async def chat_completion_stream_generator(
818819
"functions.")):
819820
base_index += 1
820821

822+
# next_tool_index tracks the index for the next NEW tool call
823+
next_tool_index = base_index
824+
821825
for group in groups:
822826
group_channel = group['channel']
823827
group_recipient = group['recipient']
@@ -832,6 +836,7 @@ async def chat_completion_stream_generator(
832836
and group_recipient.startswith("functions.")):
833837

834838
if prev_recipient != group_recipient:
839+
# New tool call - emit the opening message
835840
tool_name = group_recipient.split(
836841
"functions.", 1)[1]
837842
tool_messages.append(DeltaToolCall(
@@ -841,15 +846,19 @@ async def chat_completion_stream_generator(
841846
name=tool_name,
842847
arguments="",
843848
),
844-
index=base_index,
849+
index=next_tool_index,
845850
))
846851
prev_recipient = group_recipient
847-
# Increment index for next tool call
848-
base_index += 1
852+
# Increment for any subsequent new tool calls in this chunk
853+
next_tool_index += 1
849854

850855
if group_text:
856+
# Stream arguments for the ongoing tool call
857+
# The current call index is next_tool_index - 1 if we just
858+
# opened it, OR base_index if continuing from prev chunk
859+
tool_call_index = next_tool_index - 1 if next_tool_index > base_index else base_index
851860
tool_messages.append(DeltaToolCall(
852-
index=base_index - 1, # Use the index of the current tool call
861+
index=tool_call_index,
853862
function=DeltaFunctionCall(
854863
arguments=group_text),
855864
))

0 commit comments

Comments
 (0)