Skip to content

Improve tagged tool parsing with reasoning#23773

Draft
bartdeboer wants to merge 2 commits into
ggml-org:masterfrom
bartdeboer:qwen-tools-reasoning-fixes
Draft

Improve tagged tool parsing with reasoning#23773
bartdeboer wants to merge 2 commits into
ggml-org:masterfrom
bartdeboer:qwen-tools-reasoning-fixes

Conversation

@bartdeboer
Copy link
Copy Markdown

@bartdeboer bartdeboer commented May 27, 2026

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 as reasoning_content instead of being parsed as a tool call.

This commit combines those paths for the TAG_WITH_TAGGED parser. Reasoning tags now act as text-mode switches:

  • text inside reasoning tags is emitted as reasoning_content
  • text outside reasoning tags is emitted as normal content
  • tagged tool calls are parsed as tool_calls in either mode

The 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:

<think>
Reasoning...

<tool_call>
<function=edit>
<parameter=file>...</parameter>
<parameter=old_string>...</parameter>
<parameter=new_string>...</parameter>
</function>
</tool_call>
</think>

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:

ghcr.io/bartdeboer/llama-cpp:server-cuda-ctgbot-patches-a03afef

Digest:

sha256:a952fea7e739130000e9bca0f4f886a76ba0bb95a932fe23d56b929ef8f1255e

Tested locally with:

./build/bin/test-chat --suppress-debug

Related issues:

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES - AI assistance was used for investigation and prototyping. I have a software engineering background, but I am not primarily a C++/llama.cpp contributor. I designed the code shape together with Codex, reviewed the changes, tested them locally, and am responsible for the submitted code.

@github-actions github-actions Bot added the testing Everything test related label May 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

testing Everything test related

Projects

None yet

1 participant