Improve tagged tool parsing with reasoning#23773
Draft
bartdeboer wants to merge 2 commits into
Draft
Conversation
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.
Overview
This PR fixes two tagged-tool parsing issues found while testing Qwen3.5 with reasoning enabled and XML-style tool calls.
Commit 1: Fix tagged tool calls inside reasoning
The current llama.cpp parser splits content parsing and reasoning parsing into separate paths. This means a
<tool_call>emitted inside a<think>...</think>block can be consumed asreasoning_contentinstead of being parsed as a tool call.This commit combines those paths for the
TAG_WITH_TAGGEDparser. Reasoning tags now act as text-mode switches:reasoning_contentcontenttool_callsin either modeThe lazy tagged-tool grammar was also adjusted so a triggered
<tool_call>...</tool_call>does not have to be the final suffix of the completion. This allows the full parser to handle following segments such as</think>or later content.Commit 2: Accept required tagged tool args in any order
Tagged parameters include their names in-band, for example
<parameter=old_string>. Requiring these parameters to appear in schema/property iteration order is stricter than necessary and caused valid Qwen-style tool calls to fail parsing.This commit keeps the existing grammar-based parser structure, but generates permutations for required tagged arguments so they can be accepted in any order. Optional arguments can still appear flexibly around them.
This is intended as a narrow, low-impact fix. A more scalable long-term design would be to parse tagged parameters as an unordered list, collect them by name, reject duplicates, check required parameters after parsing, and emit normalized arguments. That would avoid permutation growth, but would require a larger parser/mapper refactor.
To avoid pathological grammar growth, this commit caps the permutation path at six required parameters. Schemas with more than six required parameters keep the previous fixed-order behavior.
This fixes Qwen-style outputs such as:
Additional information
This was tested downstream with ctgbot and Qwen3.5 9B using reasoning enabled and XML-style tool calls.
A patched CUDA image used for downstream validation is available here:
Digest:
Tested locally with:
Related issues:
peg-nativechat format parser fails when model outputs text before<tool_call>(thinking model + tool calling) #20260Requirements