Skip to content

chat : fix gemma4 required tool grammar - #29115

Merged
aldehir merged 1 commit into
ggml-org:masterfrom
aldehir:fix-gemma4-required-grammar
Sep 19, 2026
Merged

aldehir merged 1 commit into
ggml-org:masterfrom
aldehir:fix-gemma4-required-grammar

Conversation

@aldehir

@aldehir aldehir commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

Overview

Fixes #29089. The parsing for awkward edge cases negatively impacts the grammar when tool_choice = required.

Requirements

@pwilkin

pwilkin commented Sep 19, 2026

Copy link
Copy Markdown
Member

He's right, though. An interaction of two untils is incorrect. His analysis basically shows that if you have two untils, you can have an incorrect parse where the second untils partial parse completes the string that's the delimiter for the first until, i.e.

(<|tool_call)(>something)

@pwilkin pwilkin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For required tool calls this is the right workaround, but we should think about a way to fix the "double until" behavior.

@aldehir

aldehir commented Sep 19, 2026

Copy link
Copy Markdown
Contributor Author

Well, that's why we remove them! It's only a problem in a CFG grammar. The PEG parser, being greedy, doesn't have the same issue. For grammars, ac() gives better guarantees at the cost of ergonomics since the rule consumes the delimiter.

@aldehir

aldehir commented Sep 19, 2026

Copy link
Copy Markdown
Contributor Author

but we should think about a way to fix the "double until" behavior.

This problem will mostly go away once we add token-aware parsing and we can construct grammars that operate on tokens.

@aldehir aldehir added the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Sep 19, 2026
@aldehir
aldehir merged commit f072b10 into ggml-org:master Sep 19, 2026
23 of 27 checks passed
LadislavSopko pushed a commit to 0ics-srls/llama.cpp that referenced this pull request Oct 5, 2026
gianni-cor pushed a commit to tetherto/qvac-fabric-llm.cpp that referenced this pull request Nov 10, 2026
Under tool_choice required the auto-parser grammar accepted any text
before the tool call, and it accepts end of generation only after a
call. A model that starts answering in prose can then keep writing until
the context fills and never call. Qwen3-1.7B does this on a second turn
of "Say hello.", ending with no tool call and no error.

The required grammar of the json_native, tag_json and tag_tagged
builders now accepts the reasoning block, whitespace and then the call,
as the Gemma 4 parser does since ggml-org#29115. Without
reasoning extraction the block is kept as content, so a thinking model
can still think before the call; a peek on the start tag keeps a partial
tag from streaming out as content. The whitespace allowance covers
templates whose reasoning end tag carries no newline, such as QwQ, whose
generation prompt opens the block. auto and none are unchanged.

Co-authored-by: Claude Opus 5.5 <[email protected]>
JenySadadia pushed a commit to tetherto/qvac-fabric-llm.cpp that referenced this pull request Nov 13, 2026
Under tool_choice required the auto-parser grammar accepted any text
before the tool call, and it accepts end of generation only after a
call. A model that starts answering in prose can then keep writing until
the context fills and never call. Qwen3-1.7B does this on a second turn
of "Say hello.", ending with no tool call and no error.

The required grammar of the json_native, tag_json and tag_tagged
builders now accepts the reasoning block, whitespace and then the call,
as the Gemma 4 parser does since ggml-org#29115. Without
reasoning extraction the block is kept as content, so a thinking model
can still think before the call; a peek on the start tag keeps a partial
tag from streaming out as content. The whitespace allowance covers
templates whose reasoning end tag carries no newline, such as QwQ, whose
generation prompt opens the block. auto and none are unchanged.

Co-authored-by: Claude Opus 5.5 <[email protected]>
(cherry picked from commit c407611)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PEG→GBNF: until() can stop mid-delimiter, leaving Gemma 4 tool calls unconstrained under tool_choice: required

3 participants