{%- if not messages %}{{ raise_exception('A conversation must contain at least one message') }}{% endif -%} {%- if tools -%} {{- '<|im_start|>system\nAvailable tools are described by the following JSON schemas. To call a tool, emit {"id":"call_0","name":"tool_name","arguments":{}}. Use a unique id for every call. Wait for matching tool results before continuing. Call tools only when needed.\n' -}} {{- tools | tojson(sort_keys=true) -}} {{- '<|im_end|>\n' -}} {%- endif -%} {%- set state = namespace(pending={}, seen=[]) -%} {%- for message in messages -%} {%- set mi = loop.index0 -%} {%- set role = message['role'] -%} {%- if role not in ['system', 'developer', 'user', 'assistant', 'tool'] -%} {{- raise_exception('Unknown message role') -}} {%- endif -%} {%- if state.pending and role != 'tool' -%} {{- raise_exception('Unresolved tool calls before next conversation turn') -}} {%- endif -%} {%- set content = message.get('content') if message.get('content') is not none else '' -%} {%- if content is not string -%}{{ raise_exception('Text content required') }}{%- endif -%} {%- if role != 'assistant' and (message.get('reasoning_content') or message.get('tool_calls')) -%} {{- raise_exception('Only assistant can contain reasoning/tool_calls') -}} {%- endif -%} {%- set header = role -%} {%- if role == 'tool' -%} {%- set call_id = message.get('tool_call_id') -%} {%- if call_id not in state.pending -%}{{ raise_exception('Orphan or duplicate tool result') }}{%- endif -%} {%- set name = state.pending[call_id] -%} {%- if message.get('name', name) != name -%}{{ raise_exception('Tool result name mismatch') }}{%- endif -%} {%- set state.pending = dict(state.pending.items() | rejectattr('0', 'equalto', call_id)) -%} {%- set header = 'tool name=' + name + ' tool_call_id=' + call_id -%} {%- endif -%} {{- '<|im_start|>' + header + '\n' -}} {%- if role == 'assistant' -%} {% generation %} {%- if message.get('reasoning_content') -%} {{- '' + message['reasoning_content'] + '' -}} {%- endif -%} {{- content -}} {%- for call in message.get('tool_calls') or [] -%} {%- set name = call['function']['name'] -%} {%- set arguments = call['function'].get('arguments', {}) -%} {%- set call_id = call.get('id', 'call_' + mi|string + '_' + loop.index0|string) -%} {%- if arguments is not mapping -%}{{ raise_exception('Normalize arguments to an object before templating') }}{%- endif -%} {%- if name is not string or call_id is not string or not name or not call_id -%} {{- raise_exception('Function name and call id must be nonempty strings') -}} {%- endif -%} {%- if name|length > 128 or call_id|length > 64 or name[0] not in 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_' -%} {{- raise_exception('Invalid function name or call id') -}} {%- endif -%} {%- for char in name -%} {%- if char not in 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.:-' -%} {{- raise_exception('Invalid function name') -}} {%- endif -%} {%- endfor -%} {%- for char in call_id -%} {%- if char not in 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-' -%} {{- raise_exception('Invalid tool call id') -}} {%- endif -%} {%- endfor -%} {%- if call_id in state.seen -%}{{ raise_exception('Duplicate tool call id') }}{%- endif -%} {%- set state.seen = state.seen + [call_id] -%} {%- set state.pending = dict(state.pending, **{call_id: name}) -%} {{- '' -}} {{- {'id': call_id, 'name': name, 'arguments': arguments} | tojson(sort_keys=true) -}} {{- '' -}} {%- endfor -%} {{- '<|im_end|>' -}} {% endgeneration %} {%- else -%} {{- content + '<|im_end|>' -}} {%- endif -%} {{- '\n' -}} {%- endfor -%} {%- if add_generation_prompt -%} {%- if state.pending -%}{{ raise_exception('Cannot generate before tool results arrive') }}{%- endif -%} {{- '<|im_start|>assistant\n' -}} {%- if enable_thinking | default(false) -%}{{ '' }}{%- endif -%} {%- endif -%}