{#- Bailing V3 unified chat template -#}
{#- Supports: multimodal content, enable_thinking, preserved thinking, tool calling, add_vision_id -#}
{#- ==================== thinking normalization ==================== -#}
{%- set enable_thinking = enable_thinking if enable_thinking is defined else true %}
{%- if enable_thinking %}
{%- set thinking_instruction = 'detailed thinking on' %}
{%- else %}
{%- set thinking_instruction = 'detailed thinking off' %}
{%- endif %}
{#- ==================== preserved thinking normalization ==================== -#}
{%- if preserve_thinking is not defined %}
{%- set preserve_thinking = true %}
{%- endif %}
{#- ==================== vision / video counters ==================== -#}
{%- set image_count = namespace(value=0) %}
{%- set video_count = namespace(value=0) %}
{#- ==================== render_content macro ==================== -#}
{%- 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 %}
{%- set item_type = item.type if item.type is defined else '' %}
{%- if item_type == 'image' or (not item_type and ('image' in item or 'image_url' in item)) %}
{%- 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 item_type == 'audio' or (not item_type and ('audio' in item or 'audio_url' in item)) %}
{{- '<|audio_start|><|audio_pad|><|audio_end|>' }}
{%- elif item_type == 'video' or (not item_type and 'video' in item) %}
{%- 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 %}
{{- '<|video_start|><|video_pad|><|video_end|>' }}
{%- elif item_type == 'text' or (not item_type and 'text' in item) %}
{{- item.text }}
{%- else %}
{{- raise_exception('Unexpected item type in content list: each item must have type image/audio/video/text.') }}
{%- endif %}
{%- endfor %}
{%- elif content is none or content is undefined %}
{{- '' }}
{%- else %}
{{- raise_exception('Unexpected content type: expected string, list, or none.') }}
{%- endif %}
{%- endmacro %}
{#- ==================== messages guard ==================== -#}
{%- if not messages %}
{{- raise_exception('No messages provided.') }}
{%- endif %}
{#- ==================== system message ==================== -#}
{{- 'SYSTEM' }}
{%- set has_thinking_instruction = false %}
{%- if messages[0].role == 'system' %}
{%- set system_content = render_content(messages[0].content, false, true) %}
{%- if 'detailed thinking on' in system_content or 'detailed thinking off' in system_content %}
{%- set has_thinking_instruction = true %}
{%- endif %}
{{- system_content }}
{%- if (tools or not has_thinking_instruction) and system_content and system_content[-1] != '\n' %}
{{- '\n' }}
{%- endif %}
{%- endif %}
{%- if 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" }}
{%- for tool in tools %}
{{- "\n" }}
{{- tool | tojson }}
{%- endfor %}
{{- "\n\n\nIf none of the functions can be used, point it out. If the given question lacks the parameters required by the function, also point it out.\nIf you need to use a function, for each function call, output the function name and arguments within the following XML format:\n{function-name}\n{arg-key-1}\n{arg-value-1}\n{arg-key-2}\n{arg-value-2}\n...\n\n" }}
{%- endif %}
{%- if not has_thinking_instruction %}
{{- thinking_instruction }}
{%- endif %}
{{- '<|role_end|>' }}
{#- ==================== multi_step_tool: find last real user query ==================== -#}
{%- 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 user_content = render_content(message.content, false) %}
{%- if not(user_content.startswith('') and user_content.endswith('')) %}
{%- set ns.multi_step_tool = false %}
{%- set ns.last_query_index = index %}
{%- endif %}
{%- endif %}
{%- endfor %}
{#- ==================== message loop ==================== -#}
{%- for message in messages %}
{%- if message.role == "user" %}
{%- set content = render_content(message.content, true) %}
{{- 'HUMAN' + content + '<|role_end|>' }}
{%- elif message.role == "system" %}
{%- if not loop.first %}
{%- set content = render_content(message.content, true, true) %}
{{- 'SYSTEM' + content + '<|role_end|>' }}
{%- endif %}
{%- elif message.role == "assistant" %}
{%- set content = render_content(message.content, true) %}
{%- set reasoning_content = '' %}
{%- if message.reasoning_content is string and message.reasoning_content != '' %}
{%- set reasoning_content = message.reasoning_content %}
{%- else %}
{%- if '' in content %}
{%- set reasoning_content = content.split('')[0].rstrip('\n').split('')[-1].lstrip('\n') %}
{%- set content = content.split('')[-1].lstrip('\n') %}
{%- endif %}
{%- endif %}
{%- if preserve_thinking or loop.index0 > ns.last_query_index %}
{%- if reasoning_content != '' %}
{{- 'ASSISTANT' + '\n' + reasoning_content.strip('\n') + '' + content.lstrip('\n') }}
{%- else %}
{{- 'ASSISTANT\n' + content }}
{%- endif %}
{%- else %}
{{- 'ASSISTANT\n' + content }}
{%- endif %}
{%- if message.tool_calls %}
{%- for tool_call in message.tool_calls %}
{%- if (loop.first and content) or (not loop.first) %}
{{- '\n' }}
{%- endif %}
{%- set tc = tool_call %}
{%- if tool_call.function %}
{%- set tc = tool_call.function %}
{%- endif %}
{{- '' + tc.name }}
{%- set _args = tc.arguments %}
{%- for k, v in _args.items() %}
{{- '\n' + k + '' }}
{{- '\n' }}
{%- if v is string %}
{{- v }}
{%- else %}
{{- v | tojson(ensure_ascii=False) }}
{%- endif %}
{{- '' }}
{%- endfor %}
{{- '\n' }}
{%- endfor %}
{%- endif %}
{{- '<|role_end|>' }}
{%- elif message.role == "tool" %}
{%- set content = render_content(message.content, true) %}
{%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}
{{- 'OBSERVATION' }}
{%- endif %}
{{- '\n\n' }}
{{- content }}
{{- '\n' }}
{%- if loop.last or (messages[loop.index0 + 1].role != "tool") %}
{{- '<|role_end|>' }}
{%- endif %}
{%- else %}
{%- set content = render_content(message.content, true) %}
{{- '' + message.role + '' + content + '<|role_end|>' }}
{%- endif %}
{%- endfor %}
{#- ==================== generation prompt ==================== -#}
{%- if add_generation_prompt %}
{{- 'ASSISTANT' }}
{%- if enable_thinking %}
{{- '\n' }}
{%- else %}
{{- '\n' }}
{%- endif %}
{%- endif %}