chat_template.jinja: missing `last_query_index` guard re-injects stale <think> into history (causes repeating tool-call loops)

#31
by pocharlies - opened

Hi, and thanks for releasing Ornith under MIT.

While debugging constant repeating tool-call loops with agentic CLIs (e.g. qwen-code) against Ornith-1.0-35B served on vLLM, we traced the behaviour to chat_template.jinja in this repo.

The defect. The template still computes ns.last_query_index (lines ~67 and ~74) but the {%- if %} that consumed it was removed, so <think> blocks of all previous assistant turns are always re-rendered into the prompt:

{#- this repo #}
{{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}

whereas the canonical Qwen3.6 template strips thinking from older turns:

{%- if (preserve_thinking is defined and preserve_thinking is true) or (loop.index0 > ns.last_query_index) %}
    {{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}
{%- else %}
    {{- '<|im_start|>' + message.role + '\n' + content }}
{%- endif %}

Verification (hashes). Qwen/Qwen3.6-35B-A3B and nvidia/Qwen3.6-35B-A3B-NVFP4 ship byte-identical templates (md5 52b6d51ae5b203cb67e64b648494dad2, 7764 B). This repo's is md5 fa5eb1eee497fc8f3bccc3fcf60d1a51 (7536 B). A diff shows the only functional difference is the removed conditional above (plus a missing trailing newline) — which looks like an unfinished edit rather than an intentional change.

Impact. Replaying stale reasoning makes the model re-read an old plan ("I should call tool X") and re-issue the same tool call after it has already returned, which presents as an infinite tool loop in agentic clients; it also inflates context on every turn.

Workaround for users: serve with vLLM --chat-template pointing at the canonical Qwen3.6 template.

Would you consider restoring the conditional (or confirming it was intentional, in which case the last_query_index computation is dead code and worth documenting)? Happy to open a PR with the one-block fix.

For context, we also grafted the missing mtp.* tensors from nvidia/Qwen3.6-35B-A3B-NVFP4 to re-enable MTP speculative decoding (this repo's config.json declares MTP but the safetensors index has no mtp.* weights) and re-quantized lm_head to 4-bit; on an NVIDIA GB10 that takes single-stream decode from ~63 to ~170 tok/s at 8K with ~100% MTP acceptance. Details and the fixed template: https://huggingface.co/pocharlies/Ornith-1.0-35B-NVFP4-MTP-graft

Sign up or log in to comment