Skip to content

[Bugfix] Fix Gemma4 streaming tool parser stale state between requests#39214

Closed
tysonmcnulty wants to merge 1 commit intovllm-project:mainfrom
tysonmcnulty:fix/gemma4-streaming-state-reset
Closed

[Bugfix] Fix Gemma4 streaming tool parser stale state between requests#39214
tysonmcnulty wants to merge 1 commit intovllm-project:mainfrom
tysonmcnulty:fix/gemma4-streaming-state-reset

Conversation

@tysonmcnulty
Copy link
Copy Markdown

Purpose

Fix multi-turn streaming tool call failures where Gemma4ToolParser silently 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

Gemma4ToolParser is instantiated once and reused across API requests. _reset_streaming_state() is only called in __init__, so current_tool_id accumulates across requests. After N prior tool calls, _handle_tool_call_end indexes all_matches[N] against the current response's regex matches (which start at 0), silently returning None and dropping parsed arguments.

The Fix

Detect the start of a new streaming response via empty previous_token_ids and call _reset_streaming_state() + clear the delta buffer. 3 lines in extract_tool_calls_streaming.

Test Plan

pytest tests/tool_parsers/test_gemma4_tool_parser.py -v

New test test_streaming_state_reset_between_requests simulates 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.

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>
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@tysonmcnulty
Copy link
Copy Markdown
Author

Closing — the stale-state hypothesis was incorrect (parser is instantiated per-request). See #39043 (comment) for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working tool-calling

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant