Instructions to use froggeric/Qwen-Fixed-Chat-Templates with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- MLX
How to use froggeric/Qwen-Fixed-Chat-Templates with MLX:
# Download the model from the Hub pip install huggingface_hub[hf_xet] huggingface-cli download --local-dir Qwen-Fixed-Chat-Templates froggeric/Qwen-Fixed-Chat-Templates
- Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- LM Studio
- Atomic Chat
qwen3x系列模型的工具调用异常情况 调研分析报告
1、qwen3x系列模型,天然亲睐XML格式工具调用,无论流式/非流式,anthropic/openai均如此。temperature温度从0-1.0均是如此。
2、Json格式的非流式调用,存在问题,temperature温度大于0.3之后,流式有极大概率调用失败,非流式依然有较高调用成功率。
3、基于v22 chat-template模板,前面两点问题依然存在,故v22模板优先默认就是XML格式的工具调用。
那么,该情况如何解决?
1、后端工具解析引擎一定要同时支持XML/JSON真流式增量工具执行逻辑解析。
2、前端助手工具不需要特殊处理,按照现有方式直接使用即可。
3、chat-template模板需要modify,在模板中给XML/JSON两种格式的工具调用,尤其是write/edit两种做后训练加强,正常调用格式和非正常都要投喂。
完成后,流式对话、流式工具增量均正常,temperature温度也无所谓了。
实际完成后测试效果
连续不停,包含几乎所有类型工具调用,2000长文本、500内短文本,总轮次超过200,测试执行到完成,中途不会出现工具执行失败、异常停止、不调用,参数错误概率极低(不超过千分之五)。
反复执行5次完整双格式工具调用测试,整体工具执行成功率不低于99.6%(包含参数错误自动重试)。
至此XML/JSON双格式工具调用执行 流式/非流式,均达到生产级别水平。
Thank you for the report and suggestion on how to resolve it. I will look into it.
请问下所以使用froggeric/Qwen-Fixed-Chat-Templates是很有必要的对吗?然后后端需要增加xml的解析?
Thanks for starting this discussion. I hope I understood it correctly through the translation :-)
Qwen models are natively pre-trained on XML tool formatting (<function=...><parameter=...>). In testing across agentic benchmarks, XML tool calling produces significantly fewer escaping errors and better multi-parameter handling than raw JSON.
Because of this, the template uses canonical XML by default. However, if your harness strictly requires Hermes JSON (such as certain agent orchestrators), you can pass "tool_call_format": "json" in kwargs.
In the latest v22.2 release, the template also safely handles stringified JSON arguments when rendering assistant history, so upstream OpenAI API clients passing serialized argument strings will not crash or corrupt the format.
Thanks for starting this discussion. I hope I understood it correctly through the translation :-)
Qwen models are natively pre-trained on XML tool formatting (<function=...><parameter=...>). In testing across agentic benchmarks, XML tool calling produces significantly fewer escaping errors and better multi-parameter handling than raw JSON.
Because of this, the template uses canonical XML by default. However, if your harness strictly requires Hermes JSON (such as certain agent orchestrators), you can pass
"tool_call_format": "json"in kwargs.In the latest v22.2 release, the template also safely handles stringified JSON arguments when rendering assistant history, so upstream OpenAI API clients passing serialized argument strings will not crash or corrupt the format.
我的意思是,qwen3x系列模型,天然亲睐XML格式工具调用,同时更少的出错概率。
但是实际XML/JSON作为主流的两种工具调用格式,被众多助手分别使用。
而qwen3x系列模型的json格式工具调用是有更多的概率出现异常,包括且不限于、无法调用、调用后不执行、执行过程中异常停止、循环调用、调用参数出错。
为了尽可能降低json格式工具调用的出错概率,需要在chat-template模板中对工具调用特定情况进行后训练,用于让模型先学习调用的正确、错误集后加强模型能正确执行的倾向即可。
Hi @Zeaxion ,
Thank you for clarifying and sharing your testing data.
Few-shot examples (showing positive and negative tool call samples) definitely help guide the model toward correct JSON output. However, embedding hardcoded few-shot examples directly into the universal Jinja template creates a few trade-offs for a general-purpose repository:
Token overhead: Adding few-shot examples to the template increases the system prompt token count on every request, which raises time-to-first-token (TTFT) and inference cost for all users.
Context interference: Hardcoded tool examples can conflict with custom user tool schemas or domain-specific system prompts.
Separation of concerns: The template is designed to handle structural formatting and engine compatibility, while semantic few-shot steering is usually best handled at the application or agent prompt level.
That said, in the new v22.3 update, I added several improvements to make JSON tool calling safer and more reliable:
- Slicing limiters (
max_tool_response_chars) are automatically bypassed whentool_call_format="json"is active so JSON payloads are never truncated into invalid syntax. - Scalar and list arguments are cleanly serialised with standard JSON formatting across all formats.
- Serialised JSON strings in assistant history are parsed safely without syntax errors or mutating the KV cache.
If your harness requires JSON tool calling, passing tool_call_format: "json" in kwargs combined with a few-shot example in your agent's system prompt gives the best balance of reliability without forcing extra tokens onto everyone.
Hi @Zeaxion ,
Thank you for clarifying and sharing your testing data.
Few-shot examples (showing positive and negative tool call samples) definitely help guide the model toward correct JSON output. However, embedding hardcoded few-shot examples directly into the universal Jinja template creates a few trade-offs for a general-purpose repository:
Token overhead: Adding few-shot examples to the template increases the system prompt token count on every request, which raises time-to-first-token (TTFT) and inference cost for all users.
Context interference: Hardcoded tool examples can conflict with custom user tool schemas or domain-specific system prompts.
Separation of concerns: The template is designed to handle structural formatting and engine compatibility, while semantic few-shot steering is usually best handled at the application or agent prompt level.
That said, in the new v22.3 update, I added several improvements to make JSON tool calling safer and more reliable:
- Slicing limiters (
max_tool_response_chars) are automatically bypassed whentool_call_format="json"is active so JSON payloads are never truncated into invalid syntax.- Scalar and list arguments are cleanly serialised with standard JSON formatting across all formats.
- Serialised JSON strings in assistant history are parsed safely without syntax errors or mutating the KV cache.
If your harness requires JSON tool calling, passing
tool_call_format: "json"in kwargs combined with a few-shot example in your agent's system prompt gives the best balance of reliability without forcing extra tokens onto everyone.
json 的问题是,模型本身就直接不输出任何内容,所以必须要后训练提示词才能正常工作。而且只有流式会话存在该情况,而非流式会话基本上趋近正常可用的水平。
