Investigation Reveals Chat Template Serialization Causes Qwen Tool-Calling Loops
Key point
A type-aware chat template patch resolved rendering failures in Qwen-based coding agents without degrading model performance, highlighting that tool loops often stem from serialization errors rather than model behavior.
Details
Root Cause: Serialization Mismatch
Repeated tool calls in Qwen-based coding agents are frequently misattributed to model repetition. An investigation found that many apparent "model loops" were actually caused by chat templates corrupting previously executed tool calls. The core issue arises when OpenAI-compatible tool histories represent function arguments as serialized JSON strings, while Qwen-compatible chat templates expect structured mappings. When the template attempts to iterate over a string using tool_call.arguments | items, it either raises a TypeError or silently renders the history as empty/corrupted. If the model receives a reconstructed conversation where previous commands are invisible, it may re-issue the same tool call, mimicking a native model loop.
Patch and A/B Testing Results
To isolate template behavior from model behavior, researchers applied a type-aware patch to the chat template that handles arguments based on their runtime type (preserving JSON strings vs. serializing mappings). The fix was tested on a 27B model using weight-identical A/B comparisons (all 866 tensor SHA-256 hashes matched). The patched template eliminated reproduced rendering failures:
- Hugging Face rendering: Improved from 3/10 to 10/10.
- llama.cpp/minja rendering: Improved from 9/10 to 10/10.
- Performance: Median decode speed remained stable at 30.53 tok/s (vs 29.92 tok/s old), and MTP acceptance was 76.81% (vs 76.41% old), indicating no measurable regression.
Model Sensitivity and Native Grammar
The fix is not universally applicable to all Qwen models. Testing on a smaller Qwen3.5-9B model showed that replacing the native tagged/XML tool format with direct JSON caused behavioral regressions, including 12-turn repetition loops. Smaller models appear more strongly conditioned on the native tool-call representation. Consequently, a hybrid design was adopted for the Qwopus setup: retaining the native XML-style format for normal mappings while adding JSON-preserving handling for serialized string histories. Current official Qwen3.6 and Qwen3.8 templates still contain mapping-oriented code paths, requiring normalization at the API boundary to avoid breaking the model's trained tool grammar.
Broader Implications for Agent Debugging
The study emphasizes that "model loops" are a symptom of multiple distinct failure modes across the agent stack, not just model behavior. These include:
- Corrupted tool history: Arguments disappear during prompt rendering.
- Parser failures: Misinterpretation of delimiters in native tool tags.
- Agent-contract failures: Harnesses continuing execution after a terminal tool succeeds.
- Runtime differences: Inconsistent argument normalization between HF Jinja, minja, and other servers.
Reliable tool use requires evaluating the entire pipeline—model, chat template, parser, and agent harness—rather than assuming the model is at fault. The recommended approach is to normalize wire formats before template rendering and preserve the specific tool grammar the model was trained to generate.
This summary was generated automatically by AI. Check the original for the author's claims and context. Copyright belongs to the original author.
Our guide explains how the AI works. Report summary errors, attribution issues, or removal requests via Contact.