File size: 36,214 Bytes
c12e388
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
{
  "add_prefix_space": false,
  "audio_bos_token": "<|audio_start|>",
  "audio_eos_token": "<|audio_end|>",
  "audio_token": "<|audio_pad|>",
  "backend": "tokenizers",
  "bos_token": null,
  "clean_up_tokenization_spaces": false,
  "eos_token": "<|im_end|>",
  "errors": "replace",
  "image_token": "<|image_pad|>",
  "is_local": true,
  "local_files_only": false,
  "model_max_length": 262144,
  "model_specific_special_tokens": {
    "audio_bos_token": "<|audio_start|>",
    "audio_eos_token": "<|audio_end|>",
    "audio_token": "<|audio_pad|>",
    "image_token": "<|image_pad|>",
    "video_token": "<|video_pad|>",
    "vision_bos_token": "<|vision_start|>",
    "vision_eos_token": "<|vision_end|>"
  },
  "pad_token": "<|endoftext|>",
  "padding_side": "left",
  "pretokenize_regex": "(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\\r\\n\\p{L}\\p{N}]?[\\p{L}\\p{M}]+|\\p{N}| ?[^\\s\\p{L}\\p{M}\\p{N}]+[\\r\\n]*|\\s*[\\r\\n]+|\\s+(?!\\S)|\\s+",
  "processor_class": "Qwen3VLProcessor",
  "split_special_tokens": false,
  "tokenizer_class": "Qwen2Tokenizer",
  "unk_token": null,
  "video_token": "<|video_pad|>",
  "vision_bos_token": "<|vision_start|>",
  "vision_eos_token": "<|vision_end|>",
  "added_tokens_decoder": {
    "248044": {
      "content": "<|endoftext|>",
      "single_word": false,
      "lstrip": false,
      "rstrip": false,
      "normalized": false,
      "special": true
    },
    "248045": {
      "content": "<|im_start|>",
      "single_word": false,
      "lstrip": false,
      "rstrip": false,
      "normalized": false,
      "special": true
    },
    "248046": {
      "content": "<|im_end|>",
      "single_word": false,
      "lstrip": false,
      "rstrip": false,
      "normalized": false,
      "special": true
    },
    "248047": {
      "content": "<|object_ref_start|>",
      "single_word": false,
      "lstrip": false,
      "rstrip": false,
      "normalized": false,
      "special": true
    },
    "248048": {
      "content": "<|object_ref_end|>",
      "single_word": false,
      "lstrip": false,
      "rstrip": false,
      "normalized": false,
      "special": true
    },
    "248049": {
      "content": "<|box_start|>",
      "single_word": false,
      "lstrip": false,
      "rstrip": false,
      "normalized": false,
      "special": true
    },
    "248050": {
      "content": "<|box_end|>",
      "single_word": false,
      "lstrip": false,
      "rstrip": false,
      "normalized": false,
      "special": true
    },
    "248051": {
      "content": "<|quad_start|>",
      "single_word": false,
      "lstrip": false,
      "rstrip": false,
      "normalized": false,
      "special": true
    },
    "248052": {
      "content": "<|quad_end|>",
      "single_word": false,
      "lstrip": false,
      "rstrip": false,
      "normalized": false,
      "special": true
    },
    "248053": {
      "content": "<|vision_start|>",
      "single_word": false,
      "lstrip": false,
      "rstrip": false,
      "normalized": false,
      "special": true
    },
    "248054": {
      "content": "<|vision_end|>",
      "single_word": false,
      "lstrip": false,
      "rstrip": false,
      "normalized": false,
      "special": true
    },
    "248055": {
      "content": "<|vision_pad|>",
      "single_word": false,
      "lstrip": false,
      "rstrip": false,
      "normalized": false,
      "special": true
    },
    "248056": {
      "content": "<|image_pad|>",
      "single_word": false,
      "lstrip": false,
      "rstrip": false,
      "normalized": false,
      "special": true
    },
    "248057": {
      "content": "<|video_pad|>",
      "single_word": false,
      "lstrip": false,
      "rstrip": false,
      "normalized": false,
      "special": true
    },
    "248058": {
      "content": "<tool_call>",
      "single_word": false,
      "lstrip": false,
      "rstrip": false,
      "normalized": false,
      "special": false
    },
    "248059": {
      "content": "</tool_call>",
      "single_word": false,
      "lstrip": false,
      "rstrip": false,
      "normalized": false,
      "special": false
    },
    "248060": {
      "content": "<|fim_prefix|>",
      "single_word": false,
      "lstrip": false,
      "rstrip": false,
      "normalized": false,
      "special": false
    },
    "248061": {
      "content": "<|fim_middle|>",
      "single_word": false,
      "lstrip": false,
      "rstrip": false,
      "normalized": false,
      "special": false
    },
    "248062": {
      "content": "<|fim_suffix|>",
      "single_word": false,
      "lstrip": false,
      "rstrip": false,
      "normalized": false,
      "special": false
    },
    "248063": {
      "content": "<|fim_pad|>",
      "single_word": false,
      "lstrip": false,
      "rstrip": false,
      "normalized": false,
      "special": false
    },
    "248064": {
      "content": "<|repo_name|>",
      "single_word": false,
      "lstrip": false,
      "rstrip": false,
      "normalized": false,
      "special": false
    },
    "248065": {
      "content": "<|file_sep|>",
      "single_word": false,
      "lstrip": false,
      "rstrip": false,
      "normalized": false,
      "special": false
    },
    "248066": {
      "content": "<tool_response>",
      "single_word": false,
      "lstrip": false,
      "rstrip": false,
      "normalized": false,
      "special": false
    },
    "248067": {
      "content": "</tool_response>",
      "single_word": false,
      "lstrip": false,
      "rstrip": false,
      "normalized": false,
      "special": false
    },
    "248068": {
      "content": "<think>",
      "single_word": false,
      "lstrip": false,
      "rstrip": false,
      "normalized": false,
      "special": false
    },
    "248069": {
      "content": "</think>",
      "single_word": false,
      "lstrip": false,
      "rstrip": false,
      "normalized": false,
      "special": false
    },
    "248070": {
      "content": "<|audio_start|>",
      "single_word": false,
      "lstrip": false,
      "rstrip": false,
      "normalized": false,
      "special": true
    },
    "248071": {
      "content": "<|audio_end|>",
      "single_word": false,
      "lstrip": false,
      "rstrip": false,
      "normalized": false,
      "special": true
    },
    "248072": {
      "content": "<tts_pad>",
      "single_word": false,
      "lstrip": false,
      "rstrip": false,
      "normalized": false,
      "special": true
    },
    "248073": {
      "content": "<tts_text_bos>",
      "single_word": false,
      "lstrip": false,
      "rstrip": false,
      "normalized": false,
      "special": true
    },
    "248074": {
      "content": "<tts_text_eod>",
      "single_word": false,
      "lstrip": false,
      "rstrip": false,
      "normalized": false,
      "special": true
    },
    "248075": {
      "content": "<tts_text_bos_single>",
      "single_word": false,
      "lstrip": false,
      "rstrip": false,
      "normalized": false,
      "special": true
    },
    "248076": {
      "content": "<|audio_pad|>",
      "single_word": false,
      "lstrip": false,
      "rstrip": false,
      "normalized": false,
      "special": true
    }
  },
  "chat_template": "{%- set template_version = \"qwen3.8-safe-v3.1\" %}\n{#- -------------------------------------------------------------------------\n    Whitespace contract: every tag opener in this file carries a leading minus,\n    so it strips the whitespace and newlines that precede it. All emitted text\n    comes from string literals, which makes the rendered prompt byte-exact and\n    free of incidental newlines. Comment tags need the minus just as much as\n    block tags do; a plain comment tag breaks the strip chain and leaks the\n    surrounding indentation into the prompt.\n    ------------------------------------------------------------------------- -#}\n{#- The message list has to exist before anything else can inspect it. -#}\n{%- if not messages %}\n    {{- raise_exception('No messages provided.') }}\n{%- endif %}\n{#- -------------------------------------------------------------------------\n    Configuration\n    ------------------------------------------------------------------------- -#}\n{%- set _requested_tool_format =\n    tool_call_format if tool_call_format is defined else 'xml'\n%}\n{%- if _requested_tool_format not in ('auto', 'xml', 'json') %}\n    {{- raise_exception(\n        'Unexpected tool_call_format ' ~ _requested_tool_format ~\n        '. Supported values are xml (default), auto, and json.'\n    ) }}\n{%- endif %}\n{%- set add_vision_id = add_vision_id if add_vision_id is defined else false %}\n{%- set enable_thinking = enable_thinking if enable_thinking is defined else true %}\n{%- set auto_disable_thinking_with_tools =\n    auto_disable_thinking_with_tools\n    if auto_disable_thinking_with_tools is defined\n    else false\n%}\n{%- if preserve_reasoning is defined\n       and preserve_reasoning is not none\n       and preserve_thinking is defined\n       and preserve_thinking is not none\n       and preserve_reasoning != preserve_thinking %}\n    {{- raise_exception(\n        'preserve_reasoning and preserve_thinking were both provided with conflicting values.'\n    ) }}\n{%- endif %}\n{%- if preserve_reasoning is defined and preserve_reasoning is not none %}\n    {%- set _preserve_thinking = preserve_reasoning %}\n{%- elif preserve_thinking is defined and preserve_thinking is not none %}\n    {%- set _preserve_thinking = preserve_thinking %}\n{%- else %}\n    {%- set _preserve_thinking = true %}\n{%- endif %}\n{%- set max_tool_arg_chars =\n    max_tool_arg_chars if max_tool_arg_chars is defined else 0\n%}\n{%- set max_tool_response_chars =\n    max_tool_response_chars if max_tool_response_chars is defined else 0\n%}\n{%- if max_tool_arg_chars is not number or max_tool_arg_chars < 0 %}\n    {{- raise_exception('max_tool_arg_chars must be a non-negative number.') }}\n{%- endif %}\n{%- if max_tool_response_chars is not number or max_tool_response_chars < 0 %}\n    {{- raise_exception('max_tool_response_chars must be a non-negative number.') }}\n{%- endif %}\n{%- if tools is defined and tools is not none and tools %}\n    {%- if tools is string or tools is mapping or tools is not iterable %}\n        {{- raise_exception('tools must be an iterable list of tool definitions.') }}\n    {%- endif %}\n    {%- set _has_tools = true %}\n{%- else %}\n    {%- set _has_tools = false %}\n{%- endif %}\n{#- -------------------------------------------------------------------------\n    Resolve the tool-call wire format.\n\n    OpenAI-compatible servers hand assistant tool calls back with\n    `function.arguments` as a JSON *string*, not a mapping. The XML parameter\n    form needs the individual key/value pairs, and a chat template cannot\n    decode a JSON string: the standard chat-template environment provides\n    `tojson` but no inverse, and no codepoint-to-character primitive, so a\n    hand-rolled scanner could not decode \\uXXXX escapes correctly. Guessing\n    would silently corrupt tool arguments, which is worse than any error.\n\n    The deployment-safe default is XML, matching Qwen's native tool-call wire\n    format and the qwen3_coder tool parser. A caller can explicitly request\n    tool_call_format='auto' to render the whole prompt in JSON form whenever a\n    historical tool call carries string arguments. That path is lossless,\n    needs no decoding, and keeps the system-prompt instructions and rendered\n    history in one consistent format. Histories whose arguments are all\n    mappings still render as XML, byte-identically to tool_call_format='xml'.\n\n    A caller that uses the default XML mode (or explicitly pins 'xml') still\n    gets a hard error for non-empty string arguments, because that request\n    cannot be honoured faithfully in the native XML parameter representation.\n    ------------------------------------------------------------------------- -#}\n{%- set format_scan = namespace(has_json_string_arguments=false) %}\n{%- for message in messages %}\n    {%- if message.role == 'assistant'\n           and message.tool_calls is defined\n           and message.tool_calls is not none\n           and message.tool_calls\n           and message.tool_calls is not string\n           and message.tool_calls is not mapping\n           and message.tool_calls is iterable %}\n        {%- for tool_call in message.tool_calls %}\n            {%- if tool_call.function is defined\n                   and tool_call.function is not none %}\n                {%- set _tc = tool_call.function %}\n            {%- else %}\n                {%- set _tc = tool_call %}\n            {%- endif %}\n            {%- if _tc.arguments is defined\n                   and _tc.arguments is not none\n                   and _tc.arguments is string\n                   and _tc.arguments | trim\n                   and _tc.arguments | trim != '{}' %}\n                {%- set format_scan.has_json_string_arguments = true %}\n            {%- endif %}\n        {%- endfor %}\n    {%- endif %}\n{%- endfor %}\n{%- if _requested_tool_format == 'auto' %}\n    {%- if format_scan.has_json_string_arguments %}\n        {%- set _tool_format = 'json' %}\n    {%- else %}\n        {%- set _tool_format = 'xml' %}\n    {%- endif %}\n{%- else %}\n    {%- set _tool_format = _requested_tool_format %}\n{%- endif %}\n{%- set _thinking_enabled = enable_thinking %}\n{%- if auto_disable_thinking_with_tools and _has_tools %}\n    {%- set _thinking_enabled = false %}\n{%- endif %}\n{#- Normalize and validate reasoning effort even when thinking is disabled.\n    Explicit null follows the template default; OpenAI-style `minimal` maps to\n    low, while `none` disables thinking for this render. -#}\n{%- if reasoning_effort is undefined or reasoning_effort is none %}\n    {%- set _effort_raw = 'medium' %}\n{%- else %}\n    {%- set _effort_raw = reasoning_effort %}\n{%- endif %}\n{%- if _effort_raw == 'none' %}\n    {%- set _thinking_enabled = false %}\n    {%- set _reasoning_effort = 'xhigh' %}\n{%- elif _effort_raw == 'high' or _effort_raw == 'xhigh' %}\n    {%- set _reasoning_effort = 'xhigh' %}\n{%- elif _effort_raw == 'medium' %}\n    {%- set _reasoning_effort = 'medium' %}\n{%- elif _effort_raw == 'minimal' or _effort_raw == 'low' %}\n    {%- set _reasoning_effort = 'low' %}\n{%- else %}\n    {{- raise_exception(\n        'Unexpected reasoning effort ' ~ _effort_raw ~\n        '. Supported values are none, minimal/low, medium, and high/xhigh; null uses the medium default.'\n    ) }}\n{%- endif %}\n{%- set reasoning_instructions = '' %}\n{%- if _thinking_enabled %}\n    {%- if _reasoning_effort == 'xhigh' %}\n        {%- set reasoning_instructions =\n            '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.'\n        %}\n    {%- elif _reasoning_effort == 'low' %}\n        {%- set reasoning_instructions =\n            'Reasoning effort is set to low. Keep your thinking brief and focused, moving directly to the conclusion without unnecessary elaboration.'\n        %}\n    {%- endif %}\n{%- endif %}\n{#- -------------------------------------------------------------------------\n    Content rendering\n    ------------------------------------------------------------------------- -#}\n{%- set image_count = namespace(value=0) %}\n{%- set video_count = namespace(value=0) %}\n{%- macro render_content(content, do_vision_count, is_system_content=false) %}\n    {%- if content is string %}\n        {{- content }}\n    {%- elif content is iterable and content is not mapping %}\n        {%- for item in content %}\n            {%- if item is not mapping %}\n                {{- raise_exception(\n                    'Content-list items must be mappings with a supported text, image, or video type.'\n                ) }}\n            {%- endif %}\n            {%- set _item_type = item.type if 'type' in item else none %}\n            {%- if _item_type is not none\n                   and _item_type not in ('text', 'image', 'image_url', 'video') %}\n                {{- raise_exception(\n                    'Unexpected content item type ' ~ _item_type ~ '.'\n                ) }}\n            {%- endif %}\n            {%- set _is_image =\n                _item_type == 'image'\n                or _item_type == 'image_url'\n                or 'image' in item\n                or 'image_url' in item\n            %}\n            {%- set _is_video =\n                _item_type == 'video'\n                or 'video' in item\n            %}\n            {%- if _is_image and _is_video %}\n                {{- raise_exception(\n                    'A content item cannot contain both image and video data.'\n                ) }}\n            {%- elif _item_type == 'text' and (_is_image or _is_video) %}\n                {{- raise_exception(\n                    'A text content item cannot also contain image or video data.'\n                ) }}\n            {%- elif _is_image %}\n                {%- if is_system_content %}\n                    {{- raise_exception(\n                        'System and developer messages cannot contain images.'\n                    ) }}\n                {%- endif %}\n                {%- if do_vision_count %}\n                    {%- set image_count.value = image_count.value + 1 %}\n                {%- endif %}\n                {%- if add_vision_id %}\n                    {{- 'Picture ' ~ image_count.value ~ ': ' }}\n                {%- endif %}\n                {{- '<|vision_start|><|image_pad|><|vision_end|>' }}\n            {%- elif _is_video %}\n                {%- if is_system_content %}\n                    {{- raise_exception(\n                        'System and developer messages cannot contain videos.'\n                    ) }}\n                {%- endif %}\n                {%- if do_vision_count %}\n                    {%- set video_count.value = video_count.value + 1 %}\n                {%- endif %}\n                {%- if add_vision_id %}\n                    {{- 'Video ' ~ video_count.value ~ ': ' }}\n                {%- endif %}\n                {{- '<|vision_start|><|video_pad|><|vision_end|>' }}\n            {%- elif _item_type == 'text' or 'text' in item %}\n                {%- if 'text' not in item or item.text is not string %}\n                    {{- raise_exception(\n                        'Text content items must contain a string text field.'\n                    ) }}\n                {%- endif %}\n                {{- item.text }}\n            {%- else %}\n                {{- raise_exception(\n                    'Unexpected item in content list. Expected text, image, or video content.'\n                ) }}\n            {%- endif %}\n        {%- endfor %}\n    {%- elif content is none or content is undefined %}\n        {{- '' }}\n    {%- else %}\n        {{- raise_exception('Unexpected content type.') }}\n    {%- endif %}\n{%- endmacro %}\n{#- -------------------------------------------------------------------------\n    Validate and merge leading system/developer messages\n    ------------------------------------------------------------------------- -#}\n{%- set head = namespace(count=0, seen_non_system=false) %}\n{%- for message in messages %}\n    {%- set _is_system =\n        message.role == 'system' or message.role == 'developer'\n    %}\n    {%- if _is_system %}\n        {%- if head.seen_non_system %}\n            {{- raise_exception(\n                'System and developer messages must appear before all user, assistant, and tool messages.'\n            ) }}\n        {%- endif %}\n        {%- set head.count = head.count + 1 %}\n    {%- else %}\n        {%- set head.seen_non_system = true %}\n    {%- endif %}\n{%- endfor %}\n{%- set system_state = namespace(content='') %}\n{%- for message in messages[:head.count] %}\n    {%- set _part =\n        render_content(message.content, false, true) | trim\n    %}\n    {%- if _part %}\n        {%- if system_state.content %}\n            {%- set system_state.content =\n                system_state.content ~ '\\n\\n' ~ _part\n            %}\n        {%- else %}\n            {%- set system_state.content = _part %}\n        {%- endif %}\n    {%- endif %}\n{%- endfor %}\n{%- set _system_content = system_state.content %}\n{%- set _msgs = messages[head.count:] %}\n{#- -------------------------------------------------------------------------\n    System prompt and tool instructions\n    ------------------------------------------------------------------------- -#}\n{%- if _has_tools %}\n    {{- '<|im_start|>system\\n' }}\n    {%- if reasoning_instructions %}\n        {{- reasoning_instructions ~ '\\n\\n' }}\n    {%- endif %}\n    {{- '# Tools\\n\\nYou have access to the following functions:\\n\\n<tools>' }}\n    {%- for tool in tools %}\n        {{- '\\n' }}\n        {{- tool | tojson }}\n    {%- endfor %}\n    {{- '\\n</tools>' }}\n    {%- if _tool_format == 'json' %}\n        {{- '\\n\\nIf you choose to call a function ONLY reply in the following format with NO suffix:\\n\\n<tool_call>\\n{\"name\": \"example_function_name\", \"arguments\": {\"example_parameter_1\": \"value_1\", \"example_parameter_2\": \"This is the value for the second parameter\"}}\\n</tool_call>\\n\\n<IMPORTANT>\\nReminder:\\n- Function calls MUST follow the specified format: a single JSON object with \"name\" and \"arguments\" keys must be nested within <tool_call></tool_call> 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</IMPORTANT>' }}\n    {%- else %}\n        {{- '\\n\\nIf you choose to call a function ONLY reply in the following format with NO suffix:\\n\\n<tool_call>\\n<function=example_function_name>\\n<parameter=example_parameter_1>\\nvalue_1\\n</parameter>\\n<parameter=example_parameter_2>\\nThis is the value for the second parameter\\nthat can span\\nmultiple lines\\n</parameter>\\n</function>\\n</tool_call>\\n\\n<IMPORTANT>\\nReminder:\\n- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> 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</IMPORTANT>' }}\n    {%- endif %}\n    {%- if _system_content %}\n        {{- '\\n\\n' ~ _system_content }}\n    {%- endif %}\n    {{- '<|im_end|>\\n' }}\n{%- else %}\n    {%- if _system_content %}\n        {{- '<|im_start|>system\\n'\n            ~ (reasoning_instructions ~ '\\n\\n' if reasoning_instructions else '')\n            ~ _system_content\n            ~ '<|im_end|>\\n'\n        }}\n    {%- elif reasoning_instructions %}\n        {{- '<|im_start|>system\\n'\n            ~ reasoning_instructions\n            ~ '<|im_end|>\\n'\n        }}\n    {%- endif %}\n{%- endif %}\n{#- -------------------------------------------------------------------------\n    Find the last real user query\n    ------------------------------------------------------------------------- -#}\n{%- set ns = namespace(\n    multi_step_tool=true,\n    last_query_index=(_msgs | length) - 1\n) %}\n{%- for message in _msgs[::-1] %}\n    {%- set index = (_msgs | length - 1) - loop.index0 %}\n    {%- if ns.multi_step_tool and message.role == 'user' %}\n        {%- set _rendered_user =\n            render_content(message.content, false) | trim\n        %}\n        {%- if not (\n            _rendered_user.startswith('<tool_response>')\n            and _rendered_user.endswith('</tool_response>')\n        ) %}\n            {%- set ns.multi_step_tool = false %}\n            {%- set ns.last_query_index = index %}\n        {%- endif %}\n    {%- endif %}\n{%- endfor %}\n{%- if ns.multi_step_tool %}\n    {{- raise_exception('No user query found in messages.') }}\n{%- endif %}\n{#- -------------------------------------------------------------------------\n    Conversation rendering\n    ------------------------------------------------------------------------- -#}\n{%- set history_state = namespace(previous_role='') %}\n{%- for message in _msgs %}\n    {%- set content =\n        render_content(message.content, true) | trim\n    %}\n    {%- if message.role == 'user' %}\n        {{- '<|im_start|>user\\n'\n            ~ content\n            ~ '<|im_end|>\\n'\n        }}\n    {%- elif message.role == 'assistant' %}\n        {%- set reasoning_content = '' %}\n        {#- Prefer current vLLM's `reasoning` field, then retain compatibility\n            with older `reasoning_content` and other servers' `thinking`. -#}\n        {%- if message.reasoning is defined\n               and message.reasoning is not none %}\n            {%- if message.reasoning is string %}\n                {%- set reasoning_content = message.reasoning %}\n            {%- else %}\n                {%- set reasoning_content =\n                    message.reasoning | string\n                %}\n            {%- endif %}\n        {%- elif message.reasoning_content is defined\n                 and message.reasoning_content is not none %}\n            {%- if message.reasoning_content is string %}\n                {%- set reasoning_content = message.reasoning_content %}\n            {%- else %}\n                {%- set reasoning_content =\n                    message.reasoning_content | string\n                %}\n            {%- endif %}\n        {%- elif message.thinking is defined\n                 and message.thinking is not none %}\n            {%- if message.thinking is string %}\n                {%- set reasoning_content = message.thinking %}\n            {%- else %}\n                {%- set reasoning_content =\n                    message.thinking | string\n                %}\n            {%- endif %}\n        {%- endif %}\n        {%- set reasoning_content = reasoning_content | trim %}\n        {#- Preserve the original invariant: when thinking is preserved,\n            always reconstruct the thinking block, even when it is empty. -#}\n        {%- if _preserve_thinking\n               or loop.index0 > ns.last_query_index %}\n            {{- '<|im_start|>assistant\\n<think>\\n'\n                ~ reasoning_content\n                ~ '\\n</think>\\n\\n'\n                ~ content\n            }}\n        {%- else %}\n            {{- '<|im_start|>assistant\\n' ~ content }}\n        {%- endif %}\n        {%- if message.tool_calls is defined\n               and message.tool_calls is not none\n               and message.tool_calls %}\n            {%- if message.tool_calls is string\n                   or message.tool_calls is mapping\n                   or message.tool_calls is not iterable %}\n                {{- raise_exception(\n                    'assistant.tool_calls must be an iterable list of tool calls.'\n                ) }}\n            {%- endif %}\n            {%- for tool_call in message.tool_calls %}\n                {%- if tool_call.function is defined\n                       and tool_call.function is not none %}\n                    {%- set tc = tool_call.function %}\n                {%- else %}\n                    {%- set tc = tool_call %}\n                {%- endif %}\n                {%- if tc.name is not defined\n                       or tc.name is none\n                       or tc.name is not string\n                       or not tc.name %}\n                    {{- raise_exception(\n                        'Every tool call must have a non-empty string name.'\n                    ) }}\n                {%- endif %}\n                {%- set tc_name = tc.name %}\n                {%- if _tool_format == 'json' %}\n                    {%- if loop.first %}\n                        {%- if content | trim %}\n                            {{- '\\n\\n<tool_call>\\n' }}\n                        {%- else %}\n                            {{- '<tool_call>\\n' }}\n                        {%- endif %}\n                    {%- else %}\n                        {{- '\\n<tool_call>\\n' }}\n                    {%- endif %}\n                    {%- set _json_arguments = '{}' %}\n                    {%- if tc.arguments is defined\n                           and tc.arguments is not none %}\n                        {%- if tc.arguments is mapping %}\n                            {%- set _json_arguments =\n                                tc.arguments | tojson\n                            %}\n                        {%- elif tc.arguments is string %}\n                            {%- set _raw_arguments =\n                                tc.arguments | trim\n                            %}\n                            {%- if _raw_arguments %}\n                                {%- if not (\n                                    _raw_arguments.startswith('{')\n                                    and _raw_arguments.endswith('}')\n                                ) %}\n                                    {{- raise_exception(\n                                        'JSON tool-call arguments must be a JSON object string.'\n                                    ) }}\n                                {%- endif %}\n                                {%- set _json_arguments =\n                                    _raw_arguments\n                                %}\n                            {%- endif %}\n                        {%- else %}\n                            {{- raise_exception(\n                                'JSON tool-call arguments must be a mapping or a JSON object string.'\n                            ) }}\n                        {%- endif %}\n                    {%- endif %}\n                    {{- '{\"name\": ' }}\n                    {{- tc_name | tojson }}\n                    {{- ', \"arguments\": ' }}\n                    {{- _json_arguments }}\n                    {{- '}\\n</tool_call>' }}\n                {%- else %}\n                    {%- if loop.first %}\n                        {%- if content | trim %}\n                            {{- '\\n\\n<tool_call>\\n<function='\n                                ~ tc_name\n                                ~ '>\\n'\n                            }}\n                        {%- else %}\n                            {{- '<tool_call>\\n<function='\n                                ~ tc_name\n                                ~ '>\\n'\n                            }}\n                        {%- endif %}\n                    {%- else %}\n                        {{- '\\n<tool_call>\\n<function='\n                            ~ tc_name\n                            ~ '>\\n'\n                        }}\n                    {%- endif %}\n                    {%- if tc.arguments is defined\n                           and tc.arguments is not none %}\n                        {%- if tc.arguments is mapping %}\n                            {%- for args_name, args_value\n                                   in tc.arguments | items %}\n                                {%- set _argument_name =\n                                    args_name | string\n                                %}\n                                {{- '<parameter='\n                                    ~ _argument_name\n                                    ~ '>\\n'\n                                }}\n                                {#- Preserve strings verbatim. Serialize every\n                                    other type as JSON so booleans and null stay\n                                    true/false/null instead of True/False/None. -#}\n                                {%- if args_value is string %}\n                                    {%- set _argument_value =\n                                        args_value\n                                    %}\n                                {%- else %}\n                                    {%- set _argument_value =\n                                        args_value | tojson\n                                    %}\n                                {%- endif %}\n                                {%- if max_tool_arg_chars > 0\n                                       and _argument_value | length\n                                           > max_tool_arg_chars %}\n                                    {{- _argument_value[:max_tool_arg_chars] }}\n                                    {{- '\\n[TRUNCATED — original length '\n                                        ~ (_argument_value | length | string)\n                                        ~ ' chars]'\n                                    }}\n                                {%- else %}\n                                    {{- _argument_value }}\n                                {%- endif %}\n                                {{- '\\n</parameter>\\n' }}\n                            {%- endfor %}\n                        {%- elif tc.arguments is string %}\n                            {%- set _raw_arguments =\n                                tc.arguments | trim\n                            %}\n                            {%- if _raw_arguments\n                                   and _raw_arguments != '{}' %}\n                                {{- raise_exception(\n                                    'tool_call_format=\"xml\" requires tool-call arguments to be mappings, but received a JSON string. Parse the JSON string into a mapping before applying the template, or set tool_call_format=\"auto\" (or \"json\") to use the lossless JSON tool-call representation.'\n                                ) }}\n                            {%- endif %}\n                        {%- else %}\n                            {{- raise_exception(\n                                'XML tool-call arguments must be a mapping. Parse JSON-string arguments before applying the template.'\n                            ) }}\n                        {%- endif %}\n                    {%- endif %}\n                    {{- '</function>\\n</tool_call>' }}\n                {%- endif %}\n            {%- endfor %}\n        {%- endif %}\n        {{- '<|im_end|>\\n' }}\n    {%- elif message.role == 'tool' %}\n        {%- if history_state.previous_role != 'tool' %}\n            {{- '<|im_start|>user' }}\n        {%- endif %}\n        {%- if max_tool_response_chars > 0\n               and content | length > max_tool_response_chars %}\n            {%- set content =\n                content[:max_tool_response_chars]\n                ~ '\\n[TRUNCATED — original length '\n                ~ (content | length | string)\n                ~ ' chars]'\n            %}\n        {%- endif %}\n        {{- '\\n<tool_response>\\n' }}\n        {{- content }}\n        {{- '\\n</tool_response>' }}\n        {%- if loop.last %}\n            {{- '<|im_end|>\\n' }}\n        {%- elif _msgs[loop.index0 + 1].role != 'tool' %}\n            {{- '<|im_end|>\\n' }}\n        {%- endif %}\n    {%- elif message.role == 'system'\n             or message.role == 'developer' %}\n        {{- raise_exception(\n            'System and developer messages must appear only at the beginning of the conversation.'\n        ) }}\n    {%- else %}\n        {{- raise_exception(\n            'Unexpected message role ' ~ message.role ~ '.'\n        ) }}\n    {%- endif %}\n    {%- set history_state.previous_role = message.role %}\n{%- endfor %}\n{#- -------------------------------------------------------------------------\n    Generation prompt\n    ------------------------------------------------------------------------- -#}\n{%- if add_generation_prompt %}\n    {{- '<|im_start|>assistant\\n' }}\n    {%- if not _thinking_enabled %}\n        {{- '<think>\\n\\n</think>\\n\\n' }}\n    {%- else %}\n        {{- '<think>\\n' }}\n    {%- endif %}\n{%- endif %}"
}