{#- qwen38_flexible.jinja ===================================================================== Drop-in replacement chat template for Qwen/Qwen3.8-27B (and other Qwen3.8 models), forked from the upstream chat_template.jinja. Same wire format (<|im_start|>/<|im_end|>, blocks, tool_call XML), but without the rigid conversation-shape enforcement. RELAXED vs upstream: R1 No 'No user query found in messages.' raise. Assistant-first, user-less, or tool-response-only transcripts render fine. R2 System messages are allowed ANYWHERE in the transcript, not just at index 0. Mid-conversation system notes render as their own <|im_start|>system block instead of raising 'System message must be at the beginning.' R3 'developer' role accepted as an alias for 'system' (OpenAI Responses-API convention used by modern harnesses). R4 Unknown roles (narrator, char, example_user, ...) render verbatim as <|im_start|>{role} ... <|im_end|> instead of raising 'Unexpected message role.'. R5 continue_final_message support: when true and the last message is an assistant turn, that turn is left open (no <|im_end|>) for prefilling, and no generation prompt is appended. R6 No alternation assumptions anywhere: consecutive user turns, consecutive assistant turns, assistant-leading transcripts and trailing assistant turns all render as-is. KEPT from upstream (byte-compatible rendering): - render_content macro with vision counters - reasoning_effort (xhigh / medium / low) instruction injection - preserve_thinking (defaults TRUE on 3.8) + last_query_index walk - tool definitions block and / rendering - generation prompt with enable_thinking gate USAGE llama.cpp : llama-server -m qwen38-27b-*.gguf \ --chat-template-file qwen38_flexible.jinja vLLM : vllm serve Qwen/Qwen3.8-27B \ --chat-template qwen38_flexible.jinja \ --tool-call-parser qwen3_coder --reasoning-parser qwen3 SGLang : python -m sglang.launch_server --model-path Qwen/Qwen3.8-27B \ --chat-template qwen38_flexible.jinja \ --tool-call-parser qwen3_coder --reasoning-parser qwen3 Forked from Qwen/Qwen3.8-27B/chat_template.jinja (2026-08-13). License: Apache 2.0 (same as upstream). -#} {%- set image_count = namespace(value=0) %} {%- set video_count = namespace(value=0) %} {%- macro render_content(content, do_vision_count, is_system_content=false) %} {%- if content is string %} {{- content }} {%- elif content is iterable and content is not mapping %} {%- for item in content %} {%- if 'image' in item or 'image_url' in item or item.type == 'image' %} {%- if is_system_content %} {{- raise_exception('System message cannot contain images.') }} {%- endif %} {%- if do_vision_count %} {%- set image_count.value = image_count.value + 1 %} {%- endif %} {%- if add_vision_id is defined and add_vision_id %} {{- 'Picture ' ~ image_count.value ~ ': ' }} {%- endif %} {{- '<|vision_start|><|image_pad|><|vision_end|>' }} {%- elif 'video' in item or item.type == 'video' %} {%- if is_system_content %} {{- raise_exception('System message cannot contain videos.') }} {%- endif %} {%- if do_vision_count %} {%- set video_count.value = video_count.value + 1 %} {%- endif %} {%- if add_vision_id is defined and add_vision_id %} {{- 'Video ' ~ video_count.value ~ ': ' }} {%- endif %} {{- '<|vision_start|><|video_pad|><|vision_end|>' }} {%- elif 'text' in item %} {{- item.text }} {%- else %} {{- raise_exception('Unexpected item type in content.') }} {%- endif %} {%- endfor %} {%- elif content is none or content is undefined %} {{- '' }} {%- else %} {{- raise_exception('Unexpected content type.') }} {%- endif %} {%- endmacro %} {%- if not messages %} {{- raise_exception('No messages provided.') }} {%- endif %} {#- reasoning_effort instruction injection (identical to upstream) -#} {%- set reasoning_instructions = '' %} {%- if enable_thinking is undefined or enable_thinking is true %} {%- set resolved_reasoning_effort = reasoning_effort|default('xhigh') %} {%- if resolved_reasoning_effort not in ('xhigh', 'medium', 'low') %} {{- raise_exception('Unexpected reasoning effort ' ~ reasoning_effort ~ '. Supported types are xhigh (default), medium, and low.') }} {%- endif %} {%- if resolved_reasoning_effort == 'xhigh' %} {%- set reasoning_instructions = 'Reasoning effort is set to xhigh. Please think carefully through the task, validate key assumptions, consider plausible alternatives, and prioritize correctness, consistency, and clarity in the final answer.' %} {%- elif resolved_reasoning_effort == 'low' %} {%- set reasoning_instructions = 'Reasoning effort is set to low. Keep your thinking brief and focused, moving directly to the conclusion without unnecessary elaboration.' %} {%- endif %} {%- endif %} {#- R3: 'developer' at index 0 is treated exactly like 'system' -#} {%- set first_is_system = messages[0].role == 'system' or messages[0].role == 'developer' %} {%- if tools and tools is iterable and tools is not mapping %} {{- '<|im_start|>system\n' }} {%- if reasoning_instructions %} {{- reasoning_instructions + '\n\n' }} {%- endif %} {{- "# Tools\n\nYou have access to the following functions:\n\n" }} {%- for tool in tools %} {{- "\n" }} {%- if tool.function is defined %} {{- tool.function | tojson }} {%- else %} {{- tool | tojson }} {%- endif %} {%- endfor %} {{- "\n" }} {{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n\n\n\nvalue_1\n\n\nThis is the value for the second parameter\nthat can span\nmultiple lines\n\n\n\n\n\nReminder:\n- Function calls MUST follow the specified format: an inner block must be nested within XML tags\n- Required parameters MUST be specified\n- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after\n- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls\n' }} {%- if first_is_system %} {%- set content = render_content(messages[0].content, false, true)|trim %} {%- if content %} {{- '\n\n' + content }} {%- endif %} {%- endif %} {{- '<|im_end|>\n' }} {%- else %} {%- if first_is_system %} {%- set content = render_content(messages[0].content, false, true)|trim %} {%- if content %} {{- '<|im_start|>system\n' + (reasoning_instructions + '\n\n' if reasoning_instructions else '') + content + '<|im_end|>\n' }} {%- elif reasoning_instructions %} {{- '<|im_start|>system\n' + reasoning_instructions + '<|im_end|>\n' }} {%- endif %} {%- elif reasoning_instructions %} {{- '<|im_start|>system\n' + reasoning_instructions + '<|im_end|>\n' }} {%- endif %} {%- endif %} {#- last_query_index walk (identical to upstream logic) -#} {%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %} {%- for message in messages[::-1] %} {%- set index = (messages|length - 1) - loop.index0 %} {%- if ns.multi_step_tool and message.role == "user" %} {%- set content = render_content(message.content, false)|trim %} {%- if not(content.startswith('') and content.endswith('')) %} {%- set ns.multi_step_tool = false %} {%- set ns.last_query_index = index %} {%- endif %} {%- endif %} {%- endfor %} {#- R1: upstream raised 'No user query found in messages.' here when the transcript has no non-tool-response user turn (assistant-first, RP transcripts, pure continuations). We simply keep the default index: with preserve_thinking=true (3.8 default) the index is not consulted anyway, and with preserve_thinking=false it only affects which historical blocks survive. -#} {%- if ns.multi_step_tool %} {%- set ns.last_query_index = messages|length - 1 %} {%- endif %} {%- for message in messages %} {%- set content = render_content(message.content, true)|trim %} {%- if message.role == "system" or message.role == "developer" %} {#- R2/R3: index 0 was already merged into the header above. A system/developer note LATER in the transcript renders as its own block instead of raising. -#} {%- if not loop.first %} {{- '<|im_start|>system\n' + content + '<|im_end|>\n' }} {%- endif %} {%- elif message.role == "user" %} {{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }} {%- elif message.role == "assistant" %} {%- set reasoning_content = '' %} {%- if message.reasoning_content is string %} {%- set reasoning_content = message.reasoning_content %} {%- else %} {#- Rescue thinking embedded in content (harnesses that stuff into the content string). Handles and whitespace variants emitted by some quant runtimes. -#} {%- set think_end = '' %} {%- if '' in content %} {%- set think_end = '' %} {%- elif '' in content %} {%- set think_end = '' %} {%- elif '' in content %} {%- set think_end = '' %} {%- elif '' in content %} {%- set think_end = '' %} {%- endif %} {%- if think_end %} {%- set parts = content.split(think_end) %} {%- set reasoning_content = parts[0] %} {%- set content = parts[1:] | join(think_end) %} {%- if '' in reasoning_content %} {%- set reasoning_content = reasoning_content.split('')[1:] | join('') %} {%- endif %} {%- endif %} {%- endif %} {%- set reasoning_content = reasoning_content | trim %} {%- set content = content | trim %} {%- if preserve_thinking is undefined or preserve_thinking is true or loop.index0 > ns.last_query_index %} {{- '<|im_start|>' + message.role + '\n\n' + reasoning_content + '\n\n\n' + content }} {%- else %} {{- '<|im_start|>' + message.role + '\n' + content }} {%- endif %} {%- if message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %} {%- if '' in content %} {%- set content = content.split('')[0] | trim %} {%- endif %} {%- for tool_call in message.tool_calls %} {%- if tool_call.function is defined %} {%- set tool_call = tool_call.function %} {%- endif %} {%- if loop.first %} {%- if content|trim %} {{- '\n\n\n\n' }} {%- else %} {{- '\n\n' }} {%- endif %} {%- else %} {{- '\n\n\n' }} {%- endif %} {%- if tool_call.arguments is defined and tool_call.arguments != '' %} {%- if tool_call.arguments is mapping %} {%- for args_name, args_value in tool_call.arguments|items %} {{- '\n' }} {%- set args_value = args_value | string if args_value is string else args_value | tojson | safe %} {{- args_value }} {{- '\n\n' }} {%- endfor %} {%- elif tool_call.arguments is string %} {{- raise_exception("qwen38_flexible: tool_call.arguments must be a JSON object (mapping), got a string. Deserialize the JSON-encoded arguments once on ingest and store the resulting object.") }} {%- endif %} {%- endif %} {{- '\n' }} {%- endfor %} {%- endif %} {#- R5: leave the final assistant turn OPEN (no <|im_end|>) when prefilling via continue_final_message. -#} {%- if continue_final_message is defined and continue_final_message and loop.last %} {%- else %} {{- '<|im_end|>\n' }} {%- endif %} {%- elif message.role == "tool" %} {%- if loop.previtem and loop.previtem.role != "tool" %} {{- '<|im_start|>user' }} {%- endif %} {{- '\n\n' }} {{- content }} {{- '\n' }} {%- if not loop.last and loop.nextitem.role != "tool" %} {{- '<|im_end|>\n' }} {%- elif loop.last %} {{- '<|im_end|>\n' }} {%- endif %} {%- else %} {#- R4: unknown roles render verbatim instead of raising. Handy for narrators / multi-character / example transcripts. -#} {{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }} {%- endif %} {%- endfor %} {%- if add_generation_prompt and not (continue_final_message is defined and continue_final_message) %} {{- '<|im_start|>assistant\n' }} {%- if enable_thinking is defined and enable_thinking is false %} {{- '\n\n\n\n' }} {%- else %} {{- '\n' }} {%- endif %} {%- endif %}