Skip to content

qwen3-coder tool call parser - #16755

Closed
marceldev89 wants to merge 24 commits into
ggml-org:masterfrom
marceldev89:qwen3-coder_tool_call_parser
Closed

qwen3-coder tool call parser#16755
marceldev89 wants to merge 24 commits into
ggml-org:masterfrom
marceldev89:qwen3-coder_tool_call_parser

Conversation

@marceldev89

@marceldev89 marceldev89 commented Oct 24, 2025

Copy link
Copy Markdown

Note

Original work and PR by bold84 @ #15019

This pull request resolves #15012 and introduces comprehensive support for the Qwen3-Coder model family's XML-based tool-calling format. It includes a new, robust XML parser and updated chat template detection logic to ensure reliable function calling.

Key Changes:

  1. New XML Parser (common/chat-parser.cpp):

    • A dedicated, non-streaming XML parser has been implemented to handle the Qwen3-Coder's specific output format.
    • Features include robust attribute parsing, improved error reporting, and efficient function lookups using a hash set.
  2. Chat Template Detection (common/chat.h, common/chat.cpp):

    • The chat template detection logic has been updated to correctly identify Qwen3-Coder models, preventing conflicts with other formats like Hermes 2.
    • Ensures the QWEN3_CODER_XML format is applied consistently, even when no tools are explicitly provided in the request.
  3. Comprehensive tests (tests/test-chat.cpp):

    • Comprehensive tests for the parser logic has been implemented.

Known issues:

  • The model (Qwen3-Coder-30B-A3B-Instruct-UD-Q*_K_XL.gguf) occasionally stops prefixing tool calls with the proper <tool_call>. This seems to be an issue with the model itself(?).

bold84 and others added 23 commits August 2, 2025 02:02
…r_edit

Fix grammar, hide tool_call from output
Add missing closing brace to terminate test_template_output_parsers() function. This resolves compilation errors that prevented successful build of the test-chat target.
Co-authored-by: Kashyap Jois <kjois@iprdgroup.com>
Co-authored-by: Kashyap Jois <kjois@iprdgroup.com>
Co-authored-by: Marcel de Vries <marceldev89@gmail.com>
Co-authored-by: Marcel de Vries <marceldev89@gmail.com>
…ranches; add tests

- chat-parser: support schema.type as array (e.g. ["number","null"]) in convert_qwen3_param_value()
- chat: resolve $refs; allow unions including "string" as freeform; sanitize empty {"not":{}} in anyOf/oneOf before add_schema
- tests: add Qwen3-Coder regression ensuring grammar builds with unions and ignores {"not":{}}
@github-actions github-actions Bot added the testing Everything test related label Oct 24, 2025
@coder543

Copy link
Copy Markdown
Contributor

Anecdotally, I observed that the previous PR (and presumably this PR too) essentially fixed tool calling for qwen3-coder. Although when trying to use it with codex, qwen3-coder absolutely refuses to use the apply_patch tool, opting to use sed instead, which is probably just a training issue?

It would be nice to get this PR merged in.

@marceldev89

marceldev89 commented Oct 24, 2025

Copy link
Copy Markdown
Author

Anecdotally, I observed that the previous PR (and presumably this PR too) essentially fixed tool calling for qwen3-coder. Although when trying to use it with codex, qwen3-coder absolutely refuses to use the apply_patch tool, opting to use sed instead, which is probably just a training issue?

It would be nice to get this PR merged in.

I guess you could test it through openrouter or something and check if you see the same behavior there as well. My guess would be that it's a model thing and not so much this PR. Or maybe even a codex thing since it's probably heavily optimized for GPT models in terms of system prompt and tool descriptions.

@MartyLake

Copy link
Copy Markdown

Hey, just to confirm that running this branch fixes the integration with Qwen3-Coder-30B-A3B.

Reproduction steps:

# Compile this branch
mkdir $HOME/bin; cd $HOME/bin
git clone https://github.com/marceldev89/llama.cpp.git llama.cpp-fork-sources && cd llama.cpp-fork-sources
cmake -Bbuild && cmake --build build --target llama-server --parallel

# Install qwen
brew install qwen-coder

# Launch model
$HOME/bin/llama.cpp-fork-sources/build/bin/llama-server --port 8012 --host 0.0.0.0 --jinja -ngl 99 -c 300000 -m $HOME/.lmstudio/models/hf.co/hf.co-unsloth-Qwen3-Coder-30B-A3B-Instruct-GGUF-UD-Q4-K-XL-GGUF/hf.co-unsloth-Qwen3-Coder-30B-A3B-Instruct-GGUF-UD-Q4-K-XL.gguf

# Launch qwen
OPENAI_API_KEY=no OPENAI_BASE_URL=http://localhost:8012/v1 OPENAI_MODEL=models/hf.co-unsloth-Qwen3-Coder-30B-A3B-Instruct-GGUF-UD-Q4-K-XL.gguf qwen

PS: I opened too many tabs to figure it out, and I can’t find the sources any more to properly source them. I invented nothing here, credits goes to whoever wrote the pieces first.

@grigio

grigio commented Nov 6, 2025

Copy link
Copy Markdown

@MartyLake can you try also opencode if it works well? anomalyco/opencode#1890

@iwauo

iwauo commented Nov 12, 2025

Copy link
Copy Markdown

I've confirmed that this branch also fixes the issue on opencode.ai.
I tested with unsloth/Qwen3-Coder-30B-A3B-Instruct-GGUF:Q4_K_M.

ggml-org:b7018
Screenshot_20251111_143319

marceldev89:qwen3-coder_tool_call_parser
(tool-call:todowrite is handled properly)
Screenshot_20251111_141841

@przutto

przutto commented Nov 12, 2025

Copy link
Copy Markdown

This feature is really needed, please merge this PR as soon as possible.

@MartyLake

Copy link
Copy Markdown

@grigio

can you try also opencode if it works well?

can confirm it does work well with opencode too.

@svalaskevicius

svalaskevicius commented Nov 18, 2025

Copy link
Copy Markdown

Hi, I'm using

TAG="llama.cpp-b6652.amd0_rocm7.0.0_ubuntu24.04"
MODEL="Qwen3-Coder-30B-A3B-Instruct-Q4_K_M.gguf"
docker image "rocm/llama.cpp:${TAG}_server"

and I'm getting incomplete tags from LLM responses like below - would this PR help?

Let me look for any files that might contain the pattern we're looking for, perhaps using a different approach:
<tool_call>
<function=mcphub__list_directory
<parameter=path
/home/sarunas/dev/subdir
</parameter>
</function>
</tool_call>

trying to figure if I should try to find out how to build it myself as I've seen reports how it helped elsewhere :)

@svalaskevicius

Copy link
Copy Markdown

sorry for the silly question there above.

this PR does help - thanks!

@marceldev89

Copy link
Copy Markdown
Author

Seems like another PR (#16932) was merged a few days ago that makes this obsolete. Giving it a quick test with Qwen3 Coder to see if it actually works.

@marceldev89

Copy link
Copy Markdown
Author

Alright, closing this in favor of the merged #16932. Seems to be working well. I'll keep this branch around just in case. :)

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

Development

Successfully merging this pull request may close these issues.

Feature Request: Qwen3-Coder Tool Call Parser

8 participants