[tool call] Fix prev_tool_call_arr management in base_format_detector.py #11367
+23
−17
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
There is a bug in the current
prev_tool_call_arr
management inbase_format_detector.py
.When a tool call is complete at L258,
self.current_tool_call_id
increments by 1.NOTE: the bug itself is not very harmless because it is hard to encounter (or notice).
1. Wrong
current_tool_call_id
sglang/python/sglang/srt/function_call/base_format_detector.py
Lines 267 to 271 in 84768d1
However, at L301,
current_tool_call_id
is used to update inprev_tool_call_arr
.sglang/python/sglang/srt/function_call/base_format_detector.py
Lines 301 to 306 in 84768d1
This would result a tool call that has completed, for instance, 0, be put in
self.prev_tool_call_arr[1]
. See the screenshot below. The current tool call 0 is completed, but the result is saved inself.prev_tool_call_arr[1]
.This may cause a defensive check in
_check_for_unstreamed_tool_args
. As at this moment, the length ofprev_tool_call_arr
is inconsistent withstreamed_args_for_tool
sglang/python/sglang/srt/entrypoints/openai/serving_chat.py
Lines 1116 to 1119 in 84768d1
2. Empty
prev_tool_call_arr
A second issue in the logic is that, when a tool call completes, base_format_detector.py#L267-L271 (see above) tries to clear the
prev_tool_call_arr[current_tool_id]
. However,prev_tool_call_arr
is supposed to keep all the tool call data, so in_check_for_unstreamed_tool_args
, it could do:sglang/python/sglang/srt/entrypoints/openai/serving_chat.py
Lines 1121 to 1124 in 84768d1
If we clear the tool call when it is complete, this check above will always get
{}
.3. Only update
streamed_args_for_tool
whenis_complete
Same as the above reason,
streamed_args_for_tool
should be updated as well even whenis_complete=True
Modifications
self.prev_tool_call_arr[self.current_tool_id]
withcurrent_tool_call
.self.prev_tool_call_arr[self.current_tool_id]
whenis_complete
.Accuracy Tests
Benchmarking and Profiling
Checklist