{#- Qwen4 Jinja chat template for compatibility only. Deliberate omissions: - tool-result ordering is limited to text-only runs with explicit tool_call_id. Media runs keep input order: HF collects media before rendering this template. Missing/null tool_call_id never falls back to id: newer HF versions strip null fields before rendering, so these cases cannot be distinguished here. For full Python ordering semantics, reorder messages before media collection. - tool_calls[].function.arguments is only read as a dict; other types cause exceptions. - no anti-injection: the encoder tokenizes data with allow_special=false, so message text can never become a control token. A flat string cannot express that, and re-tokenizing it does not reproduce the encoder's token ids -- use the Qwen4 tokenizer's apply_chat_template(tokenize=True) for exact ids. - the generation blocks (return_assistant_tokens_mask) cover a whole assistant body; the encoder additionally masks out media placeholders inside it. -#} {%- macro render_content(content) -%} {%- if content is string -%} {{- content -}} {%- elif content is iterable and content is not mapping -%} {%- for item in content -%} {%- if item is string -%} {{- item -}} {%- elif item.get('type') == 'text' -%} {{- item.get('text', '') | default('', true) -}} {%- elif item.get('type') in ('image', 'image_url') -%} {{- '<|vision_start|><|image_pad|><|vision_end|>' -}} {%- elif item.get('type') in ('video', 'video_url') -%} {{- '<|vision_start|><|video_pad|><|vision_end|>' -}} {%- elif item.get('type') in ('audio', 'audio_url') -%} {{- '<|audio_start|><|audio_pad|><|audio_end|>' -}} {%- else -%} {{- raise_exception('Unexpected content part type ' ~ item.get('type') ~ '. Supported types are text, image, image_url, video, video_url, audio and audio_url.') -}} {%- endif -%} {%- endfor -%} {%- elif content is not none -%} {{- content | tojson -}} {%- endif -%} {%- endmacro -%} {%- macro render_reasoning(reasoning) -%} {%- if reasoning is string -%} {{- reasoning -}} {%- elif reasoning is iterable and reasoning is not mapping -%} {%- for item in reasoning -%} {%- if item is string -%} {{- item -}} {%- elif item.get('type') in (none, 'text') -%} {{- item.get('text', '') | default('', true) -}} {%- else -%} {{- item | tojson -}} {%- endif -%} {%- endfor -%} {%- elif reasoning is not none -%} {{- reasoning | tojson -}} {%- endif -%} {%- endmacro -%} {%- macro render_tools(tools) -%} {{- '# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within XML tags:\n\n' -}} {%- for tool in tools -%} {%- if tool is not mapping -%} {{- raise_exception('Unexpected tool spec ' ~ tool ~ '. Expected a function schema dict.') -}} {%- elif tool.get('type') == 'function' and 'function' in tool -%} {{- tool | tojson -}} {%- elif 'function' in tool -%} {{- ({'type': 'function', 'function': tool['function']}) | tojson -}} {%- else -%} {{- ({'type': 'function', 'function': tool}) | tojson -}} {%- endif -%} {{- '\n' -}} {%- endfor -%} {{- '\n\nIf you choose to call a function, use exactly this format:\n' -}} {{- '\n\n\n{parameter-value}\n\n\n' -}} {%- endmacro -%} {%- macro render_tool_calls(tool_calls) -%} {%- for tool_call in tool_calls -%} {%- if tool_call.function is mapping -%} {%- set name = tool_call.function.get('name', '') -%} {%- set arguments = tool_call.function.get('arguments') -%} {%- else -%} {%- set name = tool_call.get('name', '') -%} {%- set arguments = tool_call.get('arguments') -%} {%- endif -%} {{- '\n\n' -}} {%- if arguments is mapping -%} {%- for arg_name, arg_value in arguments | items -%} {{- '\n' -}} {{- arg_value if arg_value is string else arg_value | tojson -}} {{- '\n\n' -}} {%- endfor -%} {%- elif arguments is not none and (arguments is not string or (arguments | trim) != '') -%} {{- raise_exception('Unexpected argument types. Expected dict.') -}} {%- endif -%} {{- '\n' -}} {%- endfor -%} {%- endmacro -%} {%- set enable_thinking = enable_thinking | default(none) -%} {%- if enable_thinking is not none and enable_thinking is not boolean -%} {{- raise_exception('Unexpected enable_thinking ' ~ enable_thinking ~ '. Expected true, false or none.') -}} {%- endif -%} {%- set reasoning_effort = reasoning_effort | default(none) -%} {%- if reasoning_effort is not none and reasoning_effort not in ('xhigh', 'high', 'low', 'off') -%} {{- raise_exception('Unexpected reasoning effort ' ~ reasoning_effort ~ '. Supported efforts are xhigh, high, low and off.') -}} {%- endif -%} {%- if enable_thinking is none -%} {%- set effort = reasoning_effort if reasoning_effort is not none else 'high' -%} {%- elif enable_thinking -%} {%- if reasoning_effort == 'off' -%} {{- raise_exception('Conflicting thinking settings: enable_thinking=true with reasoning_effort=off.') -}} {%- endif -%} {%- set effort = reasoning_effort if reasoning_effort is not none else 'high' -%} {%- else -%} {%- if reasoning_effort is not none and reasoning_effort != 'off' -%} {{- raise_exception('Conflicting thinking settings: enable_thinking=false with reasoning_effort=' ~ reasoning_effort ~ '.') -}} {%- endif -%} {%- set effort = 'off' -%} {%- endif -%} {%- set preserve_thinking = preserve_thinking | default(true) -%} {%- if preserve_thinking is not none and preserve_thinking is not boolean -%} {{- raise_exception('Unexpected preserve_thinking ' ~ preserve_thinking ~ '. Expected true, false or none.') -}} {%- endif -%} {%- set preserve_thinking = true if preserve_thinking is none else preserve_thinking -%} {{- '<|im_start|>system\n' + 'Reasoning effort is set to ' + effort + '.' -}} {%- if tools -%} {{- '\n\n' -}} {{- render_tools(tools) -}} {%- endif -%} {{- '<|im_end|>\n' -}} {#- Sort text-only tool-result runs only when every explicit tool_call_id resolves. -#} {%- set order = namespace(messages=[], call_ids=[], pending=[], unresolved=false) -%} {%- for message in messages + [none] -%} {%- if message is none or message.get('role') != 'tool' -%} {%- if not order.unresolved -%} {%- set order.pending = order.pending | sort(attribute='position') -%} {%- endif -%} {%- for item in order.pending -%} {%- set order.messages = order.messages + [item.message] -%} {%- endfor -%} {%- set order.pending = [] -%} {%- set order.unresolved = false -%} {%- if message is not none -%} {%- if message.get('role') == 'assistant' -%} {%- set order.call_ids = [] -%} {%- for call in message.get('tool_calls') or [] -%} {%- if call is mapping and call.get('id') is not none -%} {%- set order.call_ids = order.call_ids + [call.id | string] -%} {%- endif -%} {%- endfor -%} {%- endif -%} {%- set order.messages = order.messages + [message] -%} {%- endif -%} {%- else -%} {#- Do not reorder media independently of the processor's already-collected inputs. -#} {%- set content = message.get('content') -%} {%- if content is iterable and content is not string and content is not mapping -%} {%- for part in content -%} {%- if part is not string and (part is not mapping or part.get('type') != 'text') -%} {%- set order.unresolved = true -%} {%- endif -%} {%- endfor -%} {%- elif content is not none and content is not string -%} {%- set order.unresolved = true -%} {%- endif -%} {%- set cid = message.get('tool_call_id') -%} {%- set match = namespace(position=none) -%} {%- for call_id in order.call_ids -%} {%- if cid is not none and call_id == (cid | string) and match.position is none -%} {%- set match.position = loop.index0 -%} {%- endif -%} {%- endfor -%} {%- if match.position is none -%} {%- set order.unresolved = true -%} {%- endif -%} {%- set order.pending = order.pending + [{'position': match.position, 'message': message}] -%} {%- endif -%} {%- endfor -%} {%- set messages = order.messages -%} {#- with preserve_thinking=false, reasoning survives only on and after the last user turn -#} {%- set ns = namespace(last_user=-1) -%} {%- if not preserve_thinking -%} {%- for message in messages -%} {%- if message.get('role') == 'user' -%} {%- set ns.last_user = loop.index0 -%} {%- endif -%} {%- endfor -%} {%- endif -%} {%- for message in messages -%} {%- set role = 'system' if message.get('role') == 'developer' else message.get('role') -%} {%- if role in ('system', 'user') -%} {{- '<|im_start|>' + role + '\n' + render_content(message.get('content')) + '<|im_end|>\n' -}} {%- elif role == 'assistant' -%} {%- if not preserve_thinking and loop.index0 < ns.last_user -%} {%- set reasoning = '' -%} {%- elif 'reasoning_content' in message -%} {%- set reasoning = render_reasoning(message['reasoning_content']) | trim -%} {%- elif 'reasoning' in message -%} {%- set reasoning = render_reasoning(message['reasoning']) | trim -%} {%- else -%} {%- set reasoning = '' -%} {%- endif -%} {%- set content = render_content(message.get('content')) | trim -%} {{- '<|im_start|>assistant\n' -}} {#- Keep prefilled tags outside the assistant loss mask. -#} {%- if effort == 'off' and not reasoning -%} {{- '' -}} {%- set think_completion = '' -%} {%- else -%} {%- set think_completion = reasoning + '' -%} {%- endif -%} {%- generation -%} {{- think_completion + content -}} {%- if message.get('tool_calls') -%} {{- render_tool_calls(message['tool_calls']) -}} {%- endif -%} {{- '<|im_end|>' -}} {%- endgeneration -%} {{- '\n' -}} {%- elif role == 'tool' -%} {{- '<|im_start|>tool\n' + render_content(message.get('content')) + '\n<|im_end|>\n' -}} {%- else -%} {{- raise_exception('Unexpected message role ' ~ message.get('role') ~ '. Supported roles are system, developer, user, assistant and tool.') -}} {%- endif -%} {%- endfor -%} {%- if add_generation_prompt -%} {{- '<|im_start|>assistant\n' -}} {{- '' -}} {%- if effort == 'off' -%} {{- '' -}} {%- endif -%} {%- endif -%}