Skip to content

fix(gemma4 tool calls): block the bare <|channel> token before a tool call (#886) - #908

Merged
tawei-amd merged 1 commit into
mainfrom
fix/gemma4-channel-opener-886
Nov 1, 2026
Merged

tawei-amd merged 1 commit into
mainfrom
fix/gemma4-channel-opener-886

Conversation

@tawei-amd

Copy link
Copy Markdown
Collaborator

Fixes #886.

Problem

With gemma4-it:12b, tools in the request and thinking off, the model often opened a tool call with the bare <|channel> token instead of <|tool_call>. The grammar only excluded the two-token reasoning opener <|channel>thought from the free text before a call, so <|channel> alone could still be sampled. The call then leaked into content with finish_reason: stop, or the model ran on until max_tokens.

Change

src/common/chat/chat_grammar.cpp: for the GEMMA4 format only, the free-text exclusion before an auto tool call also blocks <|channel>. reasoning_start is untouched, so reasoning-block detection and the reasoning prefix are unchanged. Other chat formats are unaffected.

Test

Built from this branch (rebased on main at 0ce8a71), using the script from the issue (max_tokens 512, n=5 per prompt, 3 for thinking on):

Model / case Before After
12b, English, tool named, thinking off 0/5 parsed 5/5
12b, Japanese, tool named, thinking off 1/5 5/5
12b, Japanese, tool not named, thinking off n/a 5/5
12b, English, thinking on n/a 3/3
e4b: English, Japanese, datetime, thinking on n/a all parsed
nemotron3.5-bolt:e4b: same four n/a all parsed

Not tested: multi-turn requests, tool_choice: required, bolt-dflash, Windows. No unit test added.

🤖 Generated with Claude Code

… call (#886)

With thinking off, only the two-token opener <|channel>thought was excluded from
the free text before a call. The model could still sample <|channel> alone and
use it in place of <|tool_call>, leaking the call into content or running away.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@tawei-amd
tawei-amd requested a review from zaneni6 November 1, 2026 01:39
@tawei-amd
tawei-amd marked this pull request as ready for review November 1, 2026 02:13
@tawei-amd
tawei-amd merged commit a2b0b6f into main Nov 1, 2026
38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant