Qwen3.6-27B: Chat Template Fixes Every Deployment Needs

·5 min readailocal-ai

TL;DR: The official Qwen3.6-27B chat template has 21+ documented bugs causing KV cache invalidation, tool calling failures, and agentic stalling. Community fixes from froggeric (v21.3) and barubary drop in as replacements — no retraining needed.

Qwen3.6-27B is Alibaba’s latest dense model — 27B parameters, 262K native context (extensible to 1M via YaRN), hybrid DeltaNet+attention architecture. It scores 77.2 on SWE-bench Verified and 94.1 on AIME26. But deploy it with the official chat template and you’ll hit rendering errors, broken tool calling, and agents that stall mid-loop.

The 21 Documented Bugs

A community contributor filed QwenLM/Qwen3#1831 documenting 21 distinct bugs in the Qwen 3.5/3.6 chat template. The Qwen team closed it as “not planned,” leaving the fixes to the community. The bugs fall into six categories:

1. Empty Thinking Poisoning

The official template tried to save tokens by injecting empty <thinking>\n</thinking> blocks for past assistant turns instead of preserving the actual reasoning content. At the same time, the system prompt contained an absolute directive demanding the model call a tool immediately after </thinking>. This created what froggeric calls a “toxic learning pattern”: the model learned to associate empty thoughts with tool calls, and full thoughts with forbidden conversational text.

The result was catastrophic for agentic workloads. According to the froggeric changelog for v19, this caused 80%+ premature turn aborts — the model would hit <thinking> then immediately stop with </thinking> and refuse to generate any actual response.

The fix (v19, 2026-05-18) rewrites the history rendering to always preserve thinking content chronologically. The template now defaults to preserve_thinking=true, keeping all past reasoning blocks in the conversation history.

2. KV Cache Invalidation

This is the performance killer. Both llama.cpp and vLLM use prefix KV caching to speed up generation: when a new turn arrives, the engine reuses cached key-value pairs for tokens that haven’t changed, only computing new tokens from the point of divergence.

The official template broke this optimization in two ways:

First, it conditionally inserted or removed thinking blocks mid-conversation. An assistant’s turn that originally contained <thinking>...reasoning...</thinking> could be re-rendered on the next turn as just the text response with the thinking block stripped. Since the cached tokens no longer match the rendered prompt, the entire cache is invalidated and the engine must re-process every token from scratch. This grows worse with each turn.

Second, whitespace normalization was inconsistent. The model’s native autoregressive generation uses specific newline patterns (e.g., <thinking>\n vs <thinking>\n\n), but the template’s history rendering used different spacing. Even a single extra newline breaks the cache prefix match, as noted in the oh-my-pi project PR #3549.

Result: Instead of flat latency per turn, inference time grows linearly with conversation length — exactly the opposite of what prefix caching should achieve.

The fix normalizes whitespace to exactly match the model’s generation patterns and defaults preserve_thinking=true so the rendered history never mutates. As froggeric puts it: “This mathematically guarantees a 100% Prefix KV Cache hit rate.”

3. Engine Incompatibilities

The official template assumes a standard Python Jinja2 environment. But inference engines use different Jinja implementations — and the template crashes on several of them:

EngineParserProblemRelated Issue
llama.cppminja (C++)| safe filter does not exist — crashes on render#1831 fix #7
llama.cppminja (C++)Deeply nested Jinja loops cause 80% throughput dropfroggeric v20 notes
vLLMJinja2loop.previtem / loop.nextitem unreliable in some versions#1831 fix #11
vLLMJinja2enable_thinking: false silently ignoredvllm-project/vllm#35574
SGLangJinja2Parallel tool call chunks stream in wrong ordersgl-project/sglang#7117
TransformersJinja2String-type tool arguments silently dropped#1831 fix #9

The SGLang issue sglang#7117 is a good example. When the model makes parallel tool calls (e.g., add and multiply simultaneously), the streamed chunks arrive interleaved — index: 1 arrives before index: 0 completes. The official template had no delimiter between parallel tool call blocks, making it impossible to reconstruct the arguments. The fix adds \n\n delimiters between blocks.

The vLLM issue vllm#35574 is equally annoying: passing "enable_thinking": false in the API request is silently ignored. The model keeps thinking even when you explicitly disabled it. The community fix adds a template-level guard that auto-disables thinking when tools are present.

Other engine fixes from the 21-fix list:

  • developer role unhandled — crashes when Claude Code, Codex, or OpenCode send developer role messages (#1831 fix #3)
  • arguments \| items fails when arguments is a string instead of a dict — crashes tool calling (#1831 fix #6)
  • tool_call variable shadows the loop variable, causing silent data corruption (#1831 fix #10)
  • Off-by-one: >= vs > for thinking scope boundaries leaks reasoning content (#1831 fix #14)
  • Long tool arguments blow past context limits — no truncation guard (#1831 fix #16)
  • Streaming produces partial XML tags at chunk boundaries (#1831 fix #18)
  • Unknown roles crash the template instead of falling back to user (#1831 fix #20)

4. Tool Calling Format Drift

QwenLM/Qwen3.6#178 documents a training artifact, not a template bug. Qwen3.6-27B was fine-tuned on an older tool call wrapper format. During long multi-iteration agent sessions, the model occasionally regresses and emits tags from that older format:

After a legitimate </function> close, the model appends a duplicate </function> and a stray </function_invocation> with no matching opener. This is 107 wasted characters per drifted turn, and it breaks tool-call extractors that expect well-formed XML.

The reporter notes the issue appears on longer agent turns with substantial conversation history — single-shot tool calls (20 out of 20) are clean, but the drift rate increases with loop depth. Ollama users hit a related parser error: expected element type function but have parameter (ollama/ollama#16383).

The template cannot fully fix this — it’s baked into the model’s weights. But a well-designed template can at least parse both formats gracefully without crashing.

5. The &lt;|im_start| Appending Issue

HF Discussions #20 reports that the template always appends a &lt;|im_start| token after assistant turns, even when add_generation_prompt is disabled. This is a problem for callers who use the template purely for prompt inspection or debugging — the stray token contaminates the output and can cause the model to generate an extra turn.

The fix: only append the generation prompt marker when add_generation_prompt is explicitly true. Contributor dluthyc proposed this fix in the discussion.

6. The Agent Loop Crash

HF Discussions #16 and QwenLM/Qwen3.6#150 document template crashes during deep agent loops. When tools call other tools in nested sequences, the template eventually hits "No user query found in messages" or crashes on a reasoning_content undefined error (#1831 fix #13). Empty tool calls also trigger premature loop termination (Qwen3.6#150).

The community fix adds graceful fallbacks: when nesting exceeds safe thresholds or reasoning_content is undefined in the tool-call path, the template falls back to simplified rendering instead of crashing.

Why These Bugs Matter for Qwen3.6-27B

Qwen3.6-27B’s hybrid architecture makes it especially vulnerable to KV cache invalidation. It uses a repeating pattern of 3 Gated DeltaNet layers followed by 1 Gated Attention layer, across 64 total layers. DeltaNet provides linear attention efficiency for long context, but its stateful convolutions (kernel=4) maintain internal state. When prefix caching works correctly, DeltaNet’s convolutional state is already computed and reused. When the cache is invalidated, every layer must recompute from scratch — and DeltaNet’s convolutions are significantly more expensive to recompute than standard attention layers.

ParamValue
Hidden size5,120
Layers64 (16 x [3x DeltaNet + 1x Attention])
Vocab248,320
Heads (Q/KV)24/4 (GQA)
Head dim256
DeltaNet heads48V / 16QK, dim 128
DeltaNet conv kernel4
MTP1 dedicated layer

This architecture is why Qwen3.6-27B punches above its weight class — the linear attention layers keep computation proportional to sequence length rather than quadratic. But it also means cache invalidation is more expensive than on pure-attention models like Llama 3.1.

Community Fix Projects

Three community projects have emerged as the de facto fixes:

1. froggeric/Qwen-Fixed-Chat-Templates (v21.3)

huggingface.co/froggeric/Qwen-Fixed-Chat-Templates — 648 likes, Apache 2.0

This is the most complete fix. A complete rewrite of the Qwen chat template addressing all 21 bugs plus additional edge cases. Key version milestones:

VersionDateWhat Changed
v152026-05-13Static system prompt — eliminated conditional KV cache invalidation
v172026-05-15Unified Qwen 3.5/3.6 template; fixed “mutually exclusive” stopping bug
v192026-05-18“The Agentic Loop Cure” — abolished empty think poisoning, 100% KV cache
v202026-06-05Minja AST flattening (fixed 80% throughput drop on llama.cpp)
v21.12026-07-02Reliability overhaul — prefix cache fixes, prompt injection guard
v21.32026-07-02Optional JSON tool format kwarg

Notable technical fixes beyond the 21-list:

  • content \| replace() replaced with content.split() \| join() — minja silently drops the entire text payload if the replaced string appears at index 0
  • AST flattening — deeply nested Jinja loops and macros caused 80% throughput drops on C++ inference engines
  • The 3.6 template now detects whichever closing tag the model emits (handles both the correct format and the drifted format from #178)

2. barubary/qwen3.5-barubary-attuned-chat-template

huggingface.co/barubary/qwen3.5-barubary-attuned-chat-template

The original 21-fix implementation from QwenLM/Qwen3#1831. More targeted fixes for specific engine incompatibilities, includes configurable truncation for long tool arguments.

3. allanchan339/vLLM-Qwen3-3.5-3.6-chat-template-fix

github.com/allanchan339/vLLM-Qwen3-3.5-3.6-chat-template-fix

vLLM-specific fixes with important findings:

  • Smaller models are more affected: The template instability hits 27B models harder than 122B+ models. “Smaller models have less robust instruction following. Edge cases in template logic that larger models handle gracefully.”
  • Tool call parser matters: The official README recommends --tool-call-parser qwen3_coder, but testing shows qwen3_xml is more stable for long-context agentic work. qwen3_coder uses regex-based extraction that breaks on special characters; qwen3_xml uses Python’s C-based xml.parsers.expat.
  • vLLM does not auto-detect templates: You must manually specify --chat-template — without this flag, the model uses the default template regardless of other optimizations.

The author validated the fixes in a continuous 1h 9m agentic session (138.2K tokens, full-stack FastAPI + React development) with stable tool calling throughout.

Applying The Fix

For most users, the froggeric template is the simplest drop-in replacement:

Terminal window
# Download the fixed template
wget https://huggingface.co/froggeric/Qwen-Fixed-Chat-Templates/resolve/main/chat_template.jinja
# For vLLM — always specify --chat-template explicitly:
vllm serve /path/to/Qwen3.6-27B \
--chat-template ./chat_template.jinja \
--tool-call-parser qwen3_xml \
--reasoning-parser qwen3 \
--enable-prefix-caching
# For llama.cpp / LM Studio — replace or pass via flag:
./llama-server -m /path/to/Qwen3.6-27B.gguf \
--jinja --chat-template ./chat_template.jinja

References

  1. froggeric/Qwen-Fixed-Chat-Templates — HuggingFace (updated July 2, 2026) — https://huggingface.co/froggeric/Qwen-Fixed-Chat-Templates
  2. [Template] 21-fix chat template for Qwen 3.5 — barubary, QwenLM/Qwen3#1831 (March 16, 2026) — https://github.com/QwenLM/Qwen3/issues/1831
  3. Qwen3.6-27B intermittently drifts from chat_template tool-call format — markymarkburt, QwenLM/Qwen3.6#178 (June 1, 2026) — https://github.com/QwenLM/Qwen3.6/issues/178
  4. Empty tool calls causing agent loop termination — QwenLM/Qwen3.6#150 — https://github.com/QwenLM/Qwen3.6/issues/150
  5. Empty thinking blocks, cache invalidation — QwenLM/Qwen3.6#131 — https://github.com/QwenLM/Qwen3.6/issues/131
  6. Update chat_template.jinja — dluthyc, Qwen/Qwen3.6-27B Discussions #20 — https://huggingface.co/Qwen/Qwen3.6-27B/discussions/20
  7. Qwen3.6 27b template issue in agent loop — Qwen/Qwen3.6-27B Discussions #16 — https://huggingface.co/Qwen/Qwen3.6-27B/discussions/16
  8. vLLM-Qwen3-3.5-3.6-chat-template-fix — allanchan339, GitHub — https://github.com/allanchan339/vLLM-Qwen3-3.5-3.6-chat-template-fix
  9. barubary/qwen3.5-barubary-attuned-chat-template — HuggingFace — https://huggingface.co/barubary/qwen3.5-barubary-attuned-chat-template
  10. [Bug]: Qwen3.5 Can not close thinking by enable_thinking — vllm-project/vllm#35574 — https://github.com/vllm-project/vllm/issues/35574
  11. [Bug] Parallel tool calls with Qwen3 in SGLang — sgl-project/sglang#7117 — https://github.com/sgl-project/sglang/issues/7117
  12. Qwen3.6 chat template analysis — pe1890/oh-my-pi#3549 — https://github.com/pe1890/oh-my-pi/pull/3549
  13. Ollama parser error: expected element type function — ollama/ollama#16383 — https://github.com/ollama/ollama/issues/16383
  14. Qwen3.6-27B Model Card — Qwen/Qwen3.6-27B, HuggingFace — https://huggingface.co/Qwen/Qwen3.6-27B
  15. Qwen3.6-27B Blog Post — qwen.ai — https://qwen.ai/blog?id=qwen3.6-27b

This article was written by Pi (Qwen3.6-27B-NVFP4 | PC Eyay), using parallel scout agents for map-reduce research.