From 30ea4c462bd000e86832a9ab110a868d8f055f87 Mon Sep 17 00:00:00 2001 From: Chang Su Date: Wed, 15 Oct 2025 09:51:51 -0700 Subject: [PATCH] [tool call] Fix prev_tool_call_arr management in base_format_detector.py (#11367) Co-authored-by: Xinyuan Tong <115166877+JustinTong0323@users.noreply.github.com> --- .../srt/function_call/base_format_detector.py | 32 +++++++++---------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/python/sglang/srt/function_call/base_format_detector.py b/python/sglang/srt/function_call/base_format_detector.py index 02a75c389..4bc62e9c2 100644 --- a/python/sglang/srt/function_call/base_format_detector.py +++ b/python/sglang/srt/function_call/base_format_detector.py @@ -265,12 +265,6 @@ class BaseFormatDetector(ABC): # Only remove the processed portion, keep unprocessed content self._buffer = current_text[start_idx + end_idx :] - if self.current_tool_id < len(self.prev_tool_call_arr): - self.prev_tool_call_arr[self.current_tool_id].clear() - self.current_tool_name_sent = False - self.streamed_args_for_tool[self.current_tool_id] = "" - self.current_tool_id += 1 - # If the tool is still being parsed, send incremental changes elif prev_arguments: prev_args_json = json.dumps(prev_arguments) @@ -278,6 +272,20 @@ class BaseFormatDetector(ABC): prefix = _find_common_prefix(prev_args_json, cur_args_json) argument_diff = prefix[sent:] + # Update prev_tool_call_arr with current state + if self.current_tool_id >= 0: + # Ensure prev_tool_call_arr is large enough + while len(self.prev_tool_call_arr) <= self.current_tool_id: + self.prev_tool_call_arr.append({}) + self.prev_tool_call_arr[self.current_tool_id] = ( + current_tool_call + ) + + # Advance to next tool if complete + if is_current_complete: + self.current_tool_name_sent = False + self.current_tool_id += 1 + # Send the argument diff if there's something new if argument_diff is not None: # Use the correct tool_index: completing_tool_id for completed tools, current_tool_id for ongoing @@ -294,17 +302,7 @@ class BaseFormatDetector(ABC): ) ], ) - if not is_current_complete: - self.streamed_args_for_tool[ - self.current_tool_id - ] += argument_diff - - # Update prev_tool_call_arr with current state - if self.current_tool_id >= 0: - # Ensure prev_tool_call_arr is large enough - while len(self.prev_tool_call_arr) <= self.current_tool_id: - self.prev_tool_call_arr.append({}) - self.prev_tool_call_arr[self.current_tool_id] = current_tool_call + self.streamed_args_for_tool[tool_index_to_use] += argument_diff return res