[Bugfix] Fix Gemma4 streaming tool parser stale state between requests#39214
Closed
tysonmcnulty wants to merge 1 commit intovllm-project:mainfrom
Closed
[Bugfix] Fix Gemma4 streaming tool parser stale state between requests#39214tysonmcnulty wants to merge 1 commit intovllm-project:mainfrom
tysonmcnulty wants to merge 1 commit intovllm-project:mainfrom
Conversation
The Gemma4ToolParser instance is reused across API requests in a multi-turn conversation, but _reset_streaming_state() is only called in __init__. After N prior tool calls across previous requests, current_tool_id is N-1. When a new request starts, _extract_streaming increments current_tool_id again, and _handle_tool_call_end does all_matches[current_tool_id] — which is out of range because the current response only has matches starting from index 0. The result is that parsed arguments are silently dropped and raw <|"|> tokens leak through as content text. Fix: detect the first delta of a new streaming response via empty previous_token_ids and call _reset_streaming_state() + clear the delta buffer. Signed-off-by: Tyson McNulty <tyson.mcnulty@gmail.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Devin AI <devin@cognition.ai> Signed-off-by: Tyson McNulty <tyson.mcnulty1@dell.com>
1 task
Contributor
There was a problem hiding this comment.
Code Review
This pull request fixes a bug in the Gemma4ToolParser where streaming state was incorrectly persisted across multiple requests in a multi-turn conversation. By resetting the streaming state and buffered text when previous_token_ids is empty, the parser now correctly handles tool calls in subsequent requests without indexing errors. A new test case has been added to verify this fix. I have no feedback to provide.
Author
|
Closing — the stale-state hypothesis was incorrect (parser is instantiated per-request). See #39043 (comment) for details. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Purpose
Fix multi-turn streaming tool call failures where
Gemma4ToolParsersilently drops parsed arguments after the first few tool calls in a conversation, causing raw<|"|>tokens to leak through as content text.Fixes the root cause described in #39043 (comment)
Not duplicating an existing PR. Checked #39027, #39081, #39070, #39114 — none address per-request streaming state reset.
The Bug
Gemma4ToolParseris instantiated once and reused across API requests._reset_streaming_state()is only called in__init__, socurrent_tool_idaccumulates across requests. After N prior tool calls,_handle_tool_call_endindexesall_matches[N]against the current response's regex matches (which start at 0), silently returningNoneand dropping parsed arguments.The Fix
Detect the start of a new streaming response via empty
previous_token_idsand call_reset_streaming_state()+ clear the delta buffer. 3 lines inextract_tool_calls_streaming.Test Plan
New test
test_streaming_state_reset_between_requestssimulates three consecutive requests through the same parser instance and verifies the third request's arguments parse correctly. All 38 tests pass; all pre-commit hooks pass.AI Assistance Disclosure
AI assistance (Claude Opus 4.6, Devin) was used for root cause analysis, writing the fix and test, and drafting this PR. All changes were reviewed and verified by a human.