{#- ============================================================================
Qwen Chat Template v1.2 (Ultimate Agentic Version)
FIXES APPLIED IN v1.2:
1. Added comprehensive Agentic rules to Section 7 (NO LAZINESS, BREAK LOOPS,
STRICT TOOLS, VERIFY) to prevent common agentic loops and lazy coding
behaviors in frameworks like Hermes and OpenHands.
FIXES APPLIED IN v1.1:
1. Added strict JSON escaping instructions to prevent Context Breakouts during
outbound code generation.
FIXES APPLIED IN v1.0:
1. Kept v0.9 Audio support and strict iterable type checks.
2. REMOVED URL text injection before vision/audio pads.
3. REMOVED Tool Call ID injection (violates Qwen's native JSON schema).
4. RE-APPLIED Context Breakout Prevention (tojson on tool responses).
5. RE-APPLIED Think-Mode Tool Call Prevention (Instruction in Section 7).
============================================================================ -#}
{%- macro raise_exception(message) -%}
{{- '\n[ERROR: ' ~ message ~ ']' -}}
{%- endmacro -%}
{#- ===== SECTION 1A: MACRO render_content ===== -#}
{%- macro render_content(content, count_vision=false, is_system_content=false) -%}
{%- if is_system_content and content is iterable and content is not mapping and content is not string and content is not none -%}
{%- for item in content -%}
{%- if item.type == 'image' or 'image' in item or 'image_url' in item -%}
{{- raise_exception('System message cannot contain images.') -}}
{%- endif -%}
{%- if item.type == 'video' or 'video' in item -%}
{{- raise_exception('System message cannot contain videos.') -}}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
{%- if content is none or content is defined == false -%}
{{- '' -}}
{%- elif content is string -%}
{{- content -}}
{%- elif content is not mapping and content is iterable and content is not string -%}
{%- for item in content -%}
{%- if item.type == 'image' or 'image' in item or 'image_url' in item -%}
{%- if count_vision -%}{%- set ns.image_count = ns.image_count + 1 -%}{%- endif -%}
{%- if add_vision_id is defined and add_vision_id -%}
{{- 'Picture ' ~ ns.image_count ~ ': ' -}}
{%- endif -%}
{{- '<|vision_start|><|image_pad|><|vision_end|>' -}}
{%- elif item.type == 'audio' or 'audio' in item or 'audio_url' in item -%}
{%- if add_vision_id is defined and add_vision_id -%}
{{- 'Audio ' ~ ns.image_count ~ ': ' -}}
{%- endif -%}
{{- '<|audio_pad|><|audio_end|>' -}}
{%- elif item.type == 'video' or 'video' in item -%}
{%- if count_vision -%}{%- set ns.video_count = ns.video_count + 1 -%}{%- endif -%}
{%- if add_vision_id is defined and add_vision_id -%}
{{- 'Video ' ~ ns.video_count ~ ': ' -}}
{%- endif -%}
{{- '<|vision_start|><|video_pad|><|vision_end|>' -}}
{%- elif item.type == 'text' or 'text' in item -%}
{{- item.text -}}
{%- else -%}
{{- raise_exception('Unexpected content type in message content.') -}}
{%- endif -%}
{%- endfor -%}
{%- elif content is not none and content is defined -%}
{{- raise_exception('Unexpected content type.') -}}
{%- endif -%}
{%- endmacro -%}
{#- ===== SECTION 1B: MACRO detect_tool_error ===== -#}
{%- macro detect_tool_error(content) -%}
{%- set content = content if content is string else '' -%}
{%- set content_lower = content | lower -%}
{%- set content_length = content | length -%}
{%- if content_length < 500
and '$ ' not in content
and 'took ' not in content_lower
and ('"error":' in content_lower or 'error:' in content_lower
or 'exception:' in content_lower or 'traceback' in content_lower
or 'command not found' in content_lower or 'invalid syntax' in content_lower
or 'failed to' in content_lower or 'permission denied' in content_lower) -%}
{%- set ns.last_tool_failed = true -%}
{%- set ns.consecutive_failures = ns.consecutive_failures + 1 -%}
{%- else -%}
{%- set ns.last_tool_failed = false -%}
{%- set ns.consecutive_failures = 0 -%}
{%- endif -%}
{%- endmacro -%}
{#- ===== SECTION 2: NAMESPACE INITIALISATION ===== -#}
{%- set ns = namespace(
enable_thinking=true,
preserve_thinking=true,
image_count=0,
video_count=0,
consecutive_failures=0,
last_tool_failed=false
) -%}
{%- if enable_thinking is defined -%}
{%- if enable_thinking -%}
{%- set ns.enable_thinking = true -%}
{%- else -%}
{%- set ns.enable_thinking = false -%}
{%- endif -%}
{%- endif -%}
{%- if preserve_thinking is defined -%}
{%- if not preserve_thinking -%}
{%- set ns.enable_thinking = false -%}
{%- set ns.preserve_thinking = false -%}
{%- else -%}
{%- set ns.preserve_thinking = true -%}
{%- endif -%}
{%- endif -%}
{#- ===== SECTION 3: PRE-SCAN ===== -#}
{%- for i in range(messages | length) -%}
{%- set _msg = messages[i] -%}
{%- if _msg.role == 'user' -%}
{%- set _u = _msg.content if _msg.content is string else '' -%}
{%- if _u.rstrip().endswith('/no_think') -%}
{%- set ns.enable_thinking = false -%}
{%- elif _u.rstrip().endswith('/think') -%}
{%- set ns.enable_thinking = true -%}
{%- endif -%}
{%- elif _msg.role == 'system' or _msg.role == 'developer' -%}
{%- set _s = _msg.content if _msg.content is string else '' -%}
{%- if '<|think_off|>' in _s -%}
{%- set ns.enable_thinking = false -%}
{%- elif '<|think_on|>' in _s -%}
{%- set ns.enable_thinking = true -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{#- ===== SECTION 4: VALIDATE MESSAGES ===== -#}
{%- if not messages -%}
{{- raise_exception('No messages provided.') -}}
{%- endif -%}
{#- ===== SECTION 5: COLLECT SYSTEM CONTENT ===== -#}
{%- set ns_sys = namespace(content='') -%}
{%- for msg in messages -%}
{%- if msg.role == 'system' or msg.role == 'developer' -%}
{%- set _c = render_content(msg.content | default(''), false, true) | trim -%}
{%- set _c = _c | replace('<|think_off|>', '') | replace('<|think_on|>', '') | trim -%}
{%- if _c -%}
{%- if ns_sys.content == '' -%}
{%- set ns_sys.content = _c -%}
{%- else -%}
{%- set ns_sys.content = ns_sys.content + '\n\n' + _c -%}
{%- endif -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{#- ===== SECTION 6: BUILD TOOLS LIST ===== -#}
{%- set _has_tools = tools is defined and tools -%}
{%- if _has_tools -%}
{%- set ns_tb = namespace(list=[]) -%}
{%- for tool in tools -%}
{%- if tool.function is defined -%}
{%- set ns_tb.list = ns_tb.list + [tool] -%}
{%- else -%}
{%- set ns_tb.list = ns_tb.list + [{"type": "function", "function": tool}] -%}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
{#- ===== SECTION 7: OUTPUT SYSTEM TURN ===== -#}
{%- if ns_sys.content or _has_tools -%}
{{- '<|im_start|>system\n' -}}
{%- if ns_sys.content -%}
{{- ns_sys.content -}}
{%- if _has_tools -%}{{- '\n\n' -}}{%- endif -%}
{%- endif -%}
{%- if _has_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 ns_tb.list -%}
{{- tool | tojson -}}
{%- if not loop.last -%}{{- '\n' -}}{%- endif -%}
{%- endfor -%}
{#- FIX v1.2: Added comprehensive agentic rules to the critical instructions -#}
{{- '\n\n\nFor each function call, return a json object with function name and arguments within XML tags:\n\n{"name": , "arguments": }\n\n\nCRITICAL INSTRUCTIONS:\n1. You MUST NOT output tags while inside tags. Complete your reasoning and output FIRST, then initiate your tool calls.\n2. When passing multi-line code or text containing quotes into a JSON argument, you MUST strictly escape all newlines as \\n and quotes as \\". Failure to escape will invalidate the JSON and crash the system.\n3. NO LAZINESS: Never use placeholders like "// existing code" or "/* rest of function */". You must output the complete, runnable code block required by the tool.\n4. BREAK LOOPS: If a tool call fails with an error, DO NOT repeat the exact same call. Analyze the error, change your approach, use alternative tools, or ask the user for clarification.\n5. STRICT TOOLS: Only use the exact function names listed in the block. Do not invent new tools or use colon-separated namespaces unless explicitly provided.\n6. VERIFY: After making file changes, always run tests or use terminal commands to verify your code compiles and works before responding to the user.' -}}
{%- endif -%}
{{- '<|im_end|>\n' -}}
{%- endif -%}
{#- ===== SECTION 8: MAIN MESSAGE LOOP ===== -#}
{%- for message in messages -%}
{%- if message.role == 'system' or message.role == 'developer' -%}
{%- elif message.role == 'user' -%}
{%- set _uc = render_content(message.content | default(''), true, false) -%}
{{- '<|im_start|>user\n' + _uc + '<|im_end|>\n' -}}
{%- elif message.role == 'assistant' -%}
{%- if message.content is defined and message.content is string -%}
{%- set _ac = message.content -%}
{%- elif message.content is defined and message.content is iterable and message.content is not mapping and message.content is not string -%}
{%- set _ac = render_content(message.content, false, false) -%}
{%- else -%}
{%- set _ac = '' -%}
{%- endif -%}
{%- if message.reasoning_content is defined and message.reasoning_content is string
and message.reasoning_content | trim
and '' not in _ac -%}
{%- set _ac = '\n' + message.reasoning_content | trim + '\n\n\n' + _ac -%}
{%- endif -%}
{%- set _tc = message.tool_calls if message.tool_calls is defined and message.tool_calls is iterable and message.tool_calls is not string else [] -%}
{%- if _tc and '' in _ac -%}
{%- set _ac = _ac.split('')[0] | trim -%}
{%- endif -%}
{%- set _show_think = false -%}
{%- if _tc -%}
{%- set _show_think = true -%}
{%- elif ns.preserve_thinking -%}
{%- set _show_think = true -%}
{%- elif loop.last -%}
{%- set _show_think = ns.enable_thinking -%}
{%- endif -%}
{%- if not _show_think -%}
{%- set _think_end = '' -%}
{%- if '' in _ac -%}
{%- set _think_end = '' -%}
{%- elif '' in _ac -%}
{%- set _think_end = '' -%}
{%- elif ' think>' in _ac -%}
{%- set _think_end = ' think>' -%}
{%- elif '' in _ac -%}
{%- set _think_end = '' -%}
{%- endif -%}
{%- if _think_end -%}
{%- set _ac = _ac.split(_think_end)[-1].lstrip('\n') -%}
{%- endif -%}
{%- elif not _tc and loop.last and '' not in _ac and not ns.enable_thinking -%}
{%- set _ac = '\n\n\n\n' + _ac -%}
{%- endif -%}
{{- '<|im_start|>assistant\n' -}}
{%- if _ac -%}
{{- _ac -}}
{%- if _tc -%}{{- '\n' -}}{%- endif -%}
{%- endif -%}
{%- if _tc -%}
{%- for tc in _tc -%}
{{- '\n' -}}
{{- '{"name": ' -}}{{- tc.function.name | tojson -}}
{%- if tc.function.arguments is string -%}
{{- ', "arguments": ' + tc.function.arguments -}}
{%- else -%}
{{- ', "arguments": ' -}}{{- tc.function.arguments | tojson -}}
{%- endif -%}
{{- '}' -}}
{%- if not loop.last -%}
{{- '\n\n' -}}
{%- else -%}
{{- '\n' -}}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
{{- '<|im_end|>\n' -}}
{#- 8d: Tool results -#}
{%- elif message.role == 'tool' -%}
{%- set _prev_role = messages[loop.index0 - 1].role if loop.index0 > 0 else '' -%}
{%- set _next_role = messages[loop.index0 + 1].role if not loop.last else '' -%}
{%- set _tool_content = message.content | default('') -%}
{{- detect_tool_error(_tool_content) -}}
{%- if _prev_role != 'tool' -%}
{{- '<|im_start|>user\n' -}}
{%- endif -%}
{{- '\n' -}}
{{- '{"result": ' ~ _tool_content | string | tojson ~ '}' -}}
{%- if ns.last_tool_failed -%}
{%- if ns.consecutive_failures >= 2 -%}
{{- '\n\n[SYSTEM WARNING: ' ~ ns.consecutive_failures ~ ' consecutive tool errors detected. Your previous approach is incorrect.]' -}}
{%- else -%}
{{- '\n\n[SYSTEM WARNING: The previous tool call returned an error. Diagnose the failure and retry with corrected arguments.]' -}}
{%- endif -%}
{%- endif -%}
{%- if _next_role == 'tool' -%}
{{- '\n\n' -}}
{%- else -%}
{{- '\n' -}}
{{- '<|im_end|>\n' -}}
{%- endif -%}
{%- else -%}
{{- raise_exception('Unexpected message role: ' + message.role) -}}
{%- endif -%}
{%- endfor -%}
{#- ===== SECTION 9: GENERATION PROMPT ===== -#}
{%- if add_generation_prompt -%}
{{- '<|im_start|>assistant\n' -}}
{%- if ns.enable_thinking -%}
{{- '\n' -}}
{%- else -%}
{{- '\n\n\n\n' -}}
{%- endif -%}
{%- endif -%}