Skip to content

Add Qwen4 support with shared Qwen3.5 XML tool-call format - #208

Open
SearchSavior wants to merge 3 commits into
claude/openvino-nightly-indexfrom
ccr-768908a6-4w3kid
Open

SearchSavior wants to merge 3 commits into
claude/openvino-nightly-indexfrom
ccr-768908a6-4w3kid

Conversation

@SearchSavior

@SearchSavior SearchSavior commented Dec 4, 2026 •

Copy link
Copy Markdown
Owner

Depends on #209 (OpenVINO nightly stack). This PR is stacked on that branch; merge #209 first, then retarget this PR to main (or delete the claude/openvino-nightly-index branch, which retargets it automatically).

Summary

This PR adds support for Qwen4 models (Qwen4-MoE and Qwen4-Exp) which share the Qwen3.5 XML tool-call format but mark protocol tags as special tokens. The key challenge is that Qwen4 tokenizers mark <tool_call>, </tool_call>, <think>, and </think> as special=True, causing standard decode to drop them. This PR introduces infrastructure to preserve these tags during decoding while still stripping true specials like <|im_end|>.

Key Changes

  • Protocol tag preservation: Added protocol_tag_ids() and decode_keeping_tags() functions in qwen35.py to detect and restore protocol tag text by token ID, working for both Qwen3.5 (special=False) and Qwen4 (special=True) tokenizers.

  • Streamer updates: Modified ChunkStreamer to use decode_keeping_tags() for qwen35-parser requests, and updated Qwen35ToolCallStreamer to use the new protocol tag detection instead of hardcoding tool boundary IDs.

  • VLM support: Added TokenCollector streamer for VLM non-streaming requests and updated both llm.py and vlm.py to use decode_keeping_tags() for qwen35 parser output.

  • Thinking: No Qwen4-specific handling. Per the model card, enable_thinking=False is equivalent to reasoning_effort="off", so Qwen4 uses the qwen35 parser and OpenArc reads only enable_thinking, as for every other parser. reasoning_effort passes through to the template untouched.

  • Model registration: Updated resolve_vlm_type.py and cli/utils.py to recognize Qwen4 architectures and map them to the qwen35 tool-call parser.

  • Documentation: Added Qwen4-MoE model documentation including export instructions, tool-call/thinking configuration, and MTP speculative decoding support.

Implementation Details

The solution preserves protocol tag text by intercepting tokens at the ID level before decoding. When a protocol tag token is encountered, its text is inserted directly rather than decoded, preventing UTF-8 boundary issues. This approach works transparently for both Qwen3.5 and Qwen4 since the tag IDs are identical across both versions.

https://claude.ai/code/session_011SRNHAAfJZyJdP14L19u1c


Generated by Claude Code

openvino.genai master maps model_type qwen4_moe onto the existing
QWEN4_EXP pipeline (9b9c388). OpenArc changes needed on top of that:

- Requires the OpenVINO nightly stack (2027.0.0 dev) from the parent
  commit; 2026.5 only knows qwen4_exp.
- Vision token + default tool parser for Qwen4MoeForConditionalGeneration
  and Qwen4ExpForConditionalGeneration (Qwen3.5 vocab, Qwen3-VL processor,
  Qwen3.5 XML tool-call format).
- Qwen4's tokenizer marks <tool_call>, </tool_call>, <think>, </think> as
  special=True (special=False in Qwen3.5), so every plain decode dropped
  them and reasoning/tool calls could not be parsed. Add
  qwen35.decode_keeping_tags() and use it on all qwen35 decode paths:
  Qwen35ToolCallStreamer (now intercepts the think tags by ID as well),
  ChunkStreamer, non-streaming LLM, and non-streaming VLM (token IDs
  collected with a TokenCollector streamer). Output is unchanged for
  Qwen3.5.
- Thinking detection honours Qwen4's reasoning_effort="off" (the template
  pre-closes <think></think>); centralised in is_thinking_enabled().
- Docs: Qwen4-MoE export/usage notes.

Verified end to end on a tiny random qwen4_moe export (optimum-intel main,
transformers 5.27.dev) through OVGenAI_VLM with images, tools, streaming,
non-streaming and MTP speculative decoding.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011SRNHAAfJZyJdP14L19u1c
reasoning_effort is read only by the Qwen4-MoE chat template ("off"
pre-closes <think></think>). Qwen3.5, Qwen3/Nemotron (hermes) and Gemma4
templates ignore it and keep thinking, so treating reasoning_effort="off"
as "thinking off" for every model disabled reasoning splitting while the
model was still reasoning (reasoning streamed as content).

- New ToolCallParser "qwen4": same XML parsing as qwen35 (served by the
  qwen35 module, QWEN_XML_PARSERS); only its thinking switch differs.
- is_thinking_enabled(kwargs, tool_call_parser) honours reasoning_effort
  only for qwen4; every other parser reads enable_thinking alone.
- Qwen4MoeForConditionalGeneration defaults to qwen4.
  Qwen4ExpForConditionalGeneration (Qwen3.8-Flash-Next) stays qwen35: its
  reasoning_effort (xhigh|medium|low) only tunes the system prompt.
- CLI choices, route/contract text and docs list qwen4.
- Tests: parser-aware thinking matrix, streamer + route coverage showing
  reasoning_effort="off" splits reasoning for qwen35 but not for qwen4.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011SRNHAAfJZyJdP14L19u1c
@SearchSavior
SearchSavior changed the base branch from main to claude/openvino-nightly-index December 4, 2026 06:50
Per the Qwen4 model card, enable_thinking=False is equivalent to
reasoning_effort="off", so there is no Qwen4-specific thinking switch to
track. OpenArc should not interpret template kwargs beyond what it does
for every other parser (enable_thinking), so:

- Remove the qwen4 tool_call_parser (enum, CLI choice, route mapping,
  QWEN_XML_PARSERS) and the parser-aware is_thinking_enabled() helper;
  thinking detection is back to main's inline enable_thinking reads.
- Qwen4MoeForConditionalGeneration defaults to qwen35.
- Docs: turn thinking off with enable_thinking=false; reasoning_effort is
  passed through to the template untouched.

decode_keeping_tags() stays: Qwen4 marks <think>/<tool_call> special=True,
so qwen35 decode paths still need it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011SRNHAAfJZyJdP14L19u1c

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants