froggeric/Qwen-Fixed-Chat-Templates

Hugging Face Models Trending 工具

摘要

该仓库为 Qwen 3.5 和 3.6 提供了修复后的 Jinja 聊天模板,解决了官方模板在 LM Studio、llama.cpp 等引擎中的渲染错误、token 浪费和功能缺失问题。

标签: mlx, jinja, chat-template, qwen, qwen3.5, qwen3.6, lm-studio, llama.cpp, tool-calling, thinking, license:apache-2.0, region:us
查看原文
查看缓存全文

缓存时间: 2026/05/08 09:08

froggeric/Qwen-Fixed-Chat-Templates · Hugging Face 来源:https://huggingface.co/froggeric/Qwen-Fixed-Chat-Templates

https://huggingface.co/froggeric/Qwen-Fixed-Chat-Templates#fixed-chat-templates-for-qwen-35–36

Qwen 3.5 与 3.6 的修复版对话模板

2026-05-05 — 已对照社区合并的模板(allanchan339、fakezeta)进行审查。确认所有实用功能均已包含;from_json 字符串参数解析无法移植到 C++ 引擎。新增未闭合思考块的自动关闭功能,感谢 allanchan339。

即插即用的 Jinja 模板,修复了官方 Qwen 对话模板中的渲染错误、token 浪费和功能缺失问题。适用于 LM Studio、llama.cpp、vLLM、MLX、oMLX 以及任何支持 HuggingFace Jinja 模板的引擎。

https://huggingface.co/froggeric/Qwen-Fixed-Chat-Templates#why-you-need-this

为什么你需要这个

官方 Qwen 模板存在影响实际使用的 bug:

问题影响
C++ 引擎上工具调用失败developer 角色被拒绝
现代 API 会发送该角色;官方模板报错空思考块污染上下文
每个历史回合都被包裹在标签中,即使内部为空无法切换思考模式
只能使用模型默认设置Qwen 3.6:`` 幻觉
模型有时会生成错误的闭合标签;解析器失败无用户查询异常破坏工具调用
raise_exception 导致智能体循环崩溃,在 OpenClaw 等运行时中重置工具调用前思考块未闭合
模型开始推理后调用工具,但未闭合思考块 — 输出格式错误

以上七个问题均已在此修复,此外还提供了简洁的 <|think_on|>/<|think_off|> 切换标签,可插入到任何消息中。

https://huggingface.co/froggeric/Qwen-Fixed-Chat-Templates#quick-install

快速安装

https://huggingface.co/froggeric/Qwen-Fixed-Chat-Templates#lm-studio

LM Studio

  1. 在右侧面板中打开你的 Qwen 模型
  2. 滚动到 Prompt Template
  3. 将模板替换为 qwen3.5/chat_template.jinja 或 qwen3.6/chat_template.jinja 的内容
  4. 保存

https://huggingface.co/froggeric/Qwen-Fixed-Chat-Templates#llamacpp–koboldcpp

llama.cpp / koboldcpp

--jinja --chat-template-file qwen3.6/chat_template.jinja

https://huggingface.co/froggeric/Qwen-Fixed-Chat-Templates#vllm–textgen

vLLM / TextGen

将 tokenizer_config.json 中的 chat_template 字符串替换为文件内容。

https://huggingface.co/froggeric/Qwen-Fixed-Chat-Templates#omlx

oMLX

覆盖本地模型目录中的 chat_template.jinja。使用 --jinja 加载。移除任何 chat_template_kwargs 覆盖 — 模板内部处理所有逻辑。

https://huggingface.co/froggeric/Qwen-Fixed-Chat-Templates#which-file-do-i-use

该用哪个文件?

3.6 模板是超集 — 额外处理了 preserve_thinking、`` 幻觉恢复和中断的思考流。如果你使用 3.6,请用 3.6 文件。

https://huggingface.co/froggeric/Qwen-Fixed-Chat-Templates#thinking-toggle

思考切换

在系统提示或用户提示的任意位置插入 <|think_on|> 或 <|think_off|>。模板会拦截该标签,将其从上下文中移除使模型看不到,并切换模式。

快速回答,无需推理:

System: You are a coding assistant. <|think_off|>
User: What's 2+2?

深度推理:

System: You are a coding assistant. <|think_on|>
User: Implement a red-black tree in Rust.

标签语法(<|think_on|>、<|think_off|>)使用 Qwen 的控制 token 分隔符,因此不会与真实文本冲突。早期社区模板使用 /think,会破坏 cd /mnt/project/think 等合法路径。

https://huggingface.co/froggeric/Qwen-Fixed-Chat-Templates#pre-installed-models

预装模型

以下模型已内置这些模板:

  • froggeric/Qwen3.6-27B-MLX-8bit (https://huggingface.co/froggeric/Qwen3.6-27B-MLX-8bit)
  • froggeric/Qwen3.6-27B-MLX-4bit (https://huggingface.co/froggeric/Qwen3.6-27B-MLX-4bit)
  • froggeric/Qwen3.5-35B-A3B-Uncensored-FernflowerAI-MLX-8bit (https://huggingface.co/froggeric/Qwen3.5-35B-A3B-Uncensored-FernflowerAI-MLX-8bit)
  • froggeric/Qwen3.5-35B-A3B-Uncensored-FernflowerAI-MLX-4bit (https://huggingface.co/froggeric/Qwen3.5-35B-A3B-Uncensored-FernflowerAI-MLX-4bit)
  • froggeric/Qwen3.6-27B-Uncensored-Heretic-v2-MLX-8bit (https://huggingface.co/froggeric/Qwen3.6-27B-Uncensored-Heretic-v2-MLX-8bit)
  • froggeric/Qwen3.6-27B-Uncensored-Heretic-v2-MLX-6bit (https://huggingface.co/froggeric/Qwen3.6-27B-Uncensored-Heretic-v2-MLX-6bit)
  • froggeric/Qwen3.6-27B-Uncensored-Heretic-v2-MLX-4bit (https://huggingface.co/froggeric/Qwen3.6-27B-Uncensored-Heretic-v2-MLX-4bit)
  • froggeric/Qwen3.6-35B-A3B-Uncensored-Heretic-MLX-8bit (https://huggingface.co/froggeric/Qwen3.6-35B-A3B-Uncensored-Heretic-MLX-8bit)
  • froggeric/Qwen3.6-35B-A3B-Uncensored-Heretic-MLX-6bit (https://huggingface.co/froggeric/Qwen3.6-35B-A3B-Uncensored-Heretic-MLX-6bit)
  • froggeric/Qwen3.6-35B-A3B-Uncensored-Heretic-MLX-4bit (https://huggingface.co/froggeric/Qwen3.6-35B-A3B-Uncensored-Heretic-MLX-4bit)

如果你正在使用上述模型,则已拥有该模板。本仓库面向其他所有人。


技术细节 — 具体修复了什么问题

https://huggingface.co/froggeric/Qwen-Fixed-Chat-Templates#tool-calls-on-c-engines

C++ 引擎上的工具调用

官方模板使用 |items 遍历工具调用参数:

{%- for key, value in tool_call.arguments|items %}

Python 的 Jinja 支持 |items。C++ 运行时(LM Studio、llama.cpp、MLX)不支持 — 模板产生渲染错误而非输出。

本模板改用直接字典键查找:

{%- for args_name in tool_call.arguments %}
{%- set args_value = tool_call.arguments[args_name] %}

同时用 is iterable 替换 is sequence(更严格的 C++ 运行时需要),移除 |safe 包装器(同样仅 Python 支持),并处理以原始字符串而非对象返回的参数。

https://huggingface.co/froggeric/Qwen-Fixed-Chat-Templates#developer-role

developer 角色

OpenAI 兼容 API 规范为系统级指令发送 message.role == "developer"。官方 Qwen 模板只检查 "system",其他值均抛出异常。

此处的两个模板均接受 "developer" 并将其映射到 system 角色。

https://huggingface.co/froggeric/Qwen-Fixed-Chat-Templates#empty-thinking-blocks

空思考块

官方模板将每个历史 assistant 回合包裹在思考标签中:

<|im_start|>assistant
Here is the answer...

当没有推理内容时,这些标签是死重 — 浪费上下文 token 并破坏前缀缓存。

Qwen 3.5 模板在输出前检查 reasoning_content。Qwen 3.6 模板更进一步:尊重 preserve_thinking kwarg,检查 reasoning_content|trim|length > 0,并将历史可见性与 <|think_off|> 覆盖关联。

https://huggingface.co/froggeric/Qwen-Fixed-Chat-Templates#thinking-hallucination-qwen-36-only

`` 幻觉(仅 Qwen 3.6)

Qwen 3.6 模型有时会生成 而非预期的闭合标签。官方解析器只在 上分割并失败。

3.6 模板检测实际使用的闭合标签并据此分割:

{%- if '' in content %}
{%- set think_end_token = '' %}
{%- elif '' in content %}
{%- set think_end_token = '' %}

它还处理中断的生成(最大 token 数在思考中途触发),通过挽救不完整的流而非注入破损的标签对。

https://huggingface.co/froggeric/Qwen-Fixed-Chat-Templates#arguments-serialization

参数序列化

官方模板无条件使用 |tojson 序列化参数值,这能正确将 Python True 转为 JSON true,但当值已是字符串时会失败。

修复后的模板先检查类型 — 字符串原样通过,其他值走 |tojson。

https://huggingface.co/froggeric/Qwen-Fixed-Chat-Templates#auto-close-unclosed-thinking-before-tool-calls

工具调用前自动关闭未闭合的思考块

模型有时会开始一个思考块,然后立即调用工具而不输出闭合标签。官方模板不处理这种情况 — 未闭合的思考标签渗入工具调用,产生格式错误的输出。

两个修复模板均检测此模式并在工具调用边界前自动注入闭合标签。

https://huggingface.co/froggeric/Qwen-Fixed-Chat-Templates#no-user-query-exception

无用户查询异常

官方模板反向扫描消息列表以查找最后一个“真实“的用户查询(跳过工具结果包装器)。如果所有用户消息都是工具结果 — 或完全没有用户消息 — 它会触发 raise_exception('No user query found in messages.'),模板硬崩溃。

这破坏了实际使用:

  • 智能体工具调用链,对话以工具结果结束,没有新的用户查询
  • /reset 或 /new 后,在 OpenClaw 等运行时中,之前会话的工具结果残留而没有新用户消息
  • 纯系统上下文,没有用户消息

修复方案将异常替换为优雅的降级:{%- set ns.last_query_index = messages|length - 1 %}。思考显示逻辑随后自然降级 — 当 preserve_thinking 启用时,带有推理内容的 assistant 回合仍显示思考标签。


Qwen 3.5 模板对比

特性官方LuffyTheFoxmod-ellaryPneuny本模板
工具参数失败修复缺失修复修复
|safe 移除失败修复缺失修复修复
developer 角色缺失缺失缺失缺失新增
思考切换无无/think(仅系统)无**`<
历史中空思考块破损破损标签省略破损修复
文本安全性N/AN/A路径中含 /think 时崩溃N/A安全
简洁指令是是是注入 “I cannot call a tool”是
无用户查询崩溃崩溃崩溃崩溃崩溃优雅降级
工具调用前自动关闭思考未处理未处理未处理未处理自动注入闭合标签

Qwen 3.6 模板对比

特性官方本模板
工具参数失败(|items)修复
|safe 移除失败修复
developer 角色缺失新增
思考切换无**`<
preserve_thinking空块泛滥动态长度检查
`` 幻觉失败检测并处理
中断的流破损标签挽救
工具调用前自动关闭思考未处理自动注入闭合标签
无用户查询崩溃崩溃优雅降级

https://huggingface.co/froggeric/Qwen-Fixed-Chat-Templates#authorship

作者

角色作者
原始模型Alibaba Cloud (Qwen team)
模板修复froggeric (https://huggingface.co/froggeric)

https://huggingface.co/froggeric/Qwen-Fixed-Chat-Templates#license

许可

Apache-2.0,继承自 Qwen。

相似文章

peculiar-ragdoll/Qwen-Sharp-Chat-Templates

Hugging Face Models Trending

本文描述了一个针对Qwen AI模型的即插即用聊天模板修复方案,该方案提高了准确性,减少了token使用量,并加速了知识工作和编程任务的响应速度。

如果你在使用llama.cpp,请更新dsv4的聊天模板

Reddit r/LocalLLaMA

最近的llama.cpp提交破坏了旧版DeepSeek V4 gguf聊天模板中的preserve_thinking行为,导致在编码代理环境中出现问题。解决方法是使用--chat-template-file覆盖gguf模板,替换为新的模板。