Upload 7 files
Browse filesFiles changed (5)
- chat_template.jinja+88-46
- config.json+17-17
- model.safetensors+2-2
- processor_config.json+7-11
- tokenizer_config.json+47
1234561789101161213141555 unmodified lines717273687475767744 unmodified lines12212312411912512612712812913049 unmodified lines1801811821831751841851861871881891791901911811921831931941951961871881891971981992002012022032042052061922072082092 unmodified lines21221321420121521621710 unmodified lines2282292302182192202212222232242252262272282292302312322332342352362372382372392402412422432412422442452462472483 unmodified lines2522532542522532552562572582592602612622632643 unmodified lines2682692702632642712722732742757 unmodified lines28328428527827928628728828929010 unmodified lines3013023033043053063073083093103113123133143153 unmodified lines319320321305322323324325326327328311329330313331332333334335336319337338321322339340324325341342327343344345346347348349350351352353354355356357358359360361362363364365366367368369334370371372373374375376377378379380381382383384385344386{# Template: Google Gemma 4 Canonical Chat Template Author: Google Gemma Engineering Team Published: 2026-07-09 Context: Fixed tool-calling loops, turn closures, and thinking content-ordering.#}{%- macro format_parameters(properties, required) -%}{%- macro format_parameters(properties, required, filter_keys=false) -%} {%- set standard_keys = ['description', 'type', 'properties', 'required', 'nullable'] -%} {%- set ns = namespace(found_first=false) -%} {%- for key, value in properties | dictsort -%} {%- set add_comma = false -%} {%- if key not in standard_keys -%} {%- if not filter_keys or key not in standard_keys -%} {%- if ns.found_first %},{% endif -%} {%- set ns.found_first = true -%} {{ key }}:{55 unmodified lines {%- elif value is mapping -%} {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%} properties:{ {{- format_parameters(value, value['required'] | default([])) -}} {{- format_parameters(value, value['required'] | default([]), filter_keys=true) -}} } {%- endif -%} {%- if value['required'] -%}44 unmodified lines }{%- endmacro -%}{%- macro format_argument(argument, escape_keys=True) -%} {%- if argument is string -%} {%- if argument is none -%} {{- 'null' -}} {%- elif argument is string -%} {{- '<|"|>' + argument + '<|"|>' -}} {%- elif argument is boolean -%} {{- 'true' if argument else 'false' -}}49 unmodified lines {{- '<tool_response|>' -}}{%- endmacro -%}
{#- ===== SETUP ===== -#}{%- set ns = namespace(prev_message_type=None) -%}{%- set ns = namespace(prev_message_type=None, prev_non_tool_role=None) -%}{%- set loop_messages = messages -%}{%- set enable_thinking = enable_thinking | default(false) -%}{%- set preserve_thinking = preserve_thinking | default(false) -%}{{- bos_token -}}{#- Handle System/Tool Definitions Block -#}{%- if (enable_thinking is defined and enable_thinking) or tools or messages[0]['role'] in ['system', 'developer'] -%}{%- if enable_thinking or tools or (messages and messages[0]['role'] in ['system', 'developer']) -%} {{- '<|turn>system\n' -}}
{#- Inject Thinking token at the very top of the FIRST system turn -#} {%- if enable_thinking is defined and enable_thinking -%} {%- if enable_thinking -%} {{- '<|think|>\n' -}} {%- set ns.prev_message_type = 'think' -%} {%- endif -%}
{%- if messages[0]['role'] in ['system', 'developer'] -%} {{- messages[0]['content'] | trim -}} {%- if messages and messages[0]['role'] in ['system', 'developer'] -%} {%- if messages[0]['content'] is string -%} {{- messages[0]['content'] | trim -}} {%- elif messages[0]['content'] is sequence -%} {%- for item in messages[0]['content'] -%} {{- item['text'] | trim + ' '-}} {%- endfor -%} {%- endif -%} {%- set loop_messages = messages[1:] -%} {%- endif -%}
{%- if tools -%} {%- for tool in tools %} {{- '<|tool>' -}}2 unmodified lines {%- endfor %} {%- set ns.prev_message_type = 'tool' -%} {%- endif -%}
{{- '<turn|>\n' -}}{%- endif %}
10 unmodified lines {%- if message['role'] != 'tool' -%} {%- set ns.prev_message_type = None -%} {%- set role = 'model' if message['role'] == 'assistant' else message['role'] -%} {#- Detect continuation: suppress duplicate <|turn>model when previous non-tool message was also assistant -#} {%- set prev_nt = namespace(role=None, found=false) -%} {%- if loop.index0 > 0 -%} {%- for j in range(loop.index0 - 1, -1, -1) -%} {%- if not prev_nt.found -%} {%- if loop_messages[j]['role'] != 'tool' -%} {%- set prev_nt.role = loop_messages[j]['role'] -%} {%- set prev_nt.found = true -%} {%- endif -%} {%- endif -%} {%- endfor -%} {%- endif -%} {%- set continue_same_model_turn = (role == 'model' and prev_nt.role == 'assistant') -%} {#- Detect continuation using tracked state — O(1) instead of O(n) backward scan -#} {%- set continue_same_model_turn = (role == 'model' and ns.prev_non_tool_role == 'assistant') -%} {%- if not continue_same_model_turn -%} {{- '<|turn>' + role + '\n' }} {%- endif -%}
{#- Render reasoning/reasoning_content as thinking channel -#} {%- set thinking_text = message.get('reasoning') or message.get('reasoning_content') -%} {%- if thinking_text and loop.index0 > ns_turn.last_user_idx and message.get('tool_calls') -%} {%- set thinking_gate = (loop.index0 > ns_turn.last_user_idx) or (preserve_thinking and message.get('tool_calls')) -%} {%- if thinking_text and thinking_gate -%} {{- '<|channel>thought\n' + thinking_text + '\n<channel|>' -}} {%- endif -%}
{%- if message['tool_calls'] -%} {%- for tool_call in message['tool_calls'] -%} {%- if message.get('tool_calls') -%} {%- for tool_call in message.get('tool_calls') -%} {%- set function = tool_call['function'] -%} {{- '<|tool_call>call:' + function['name'] + '{' -}} {%- if function['arguments'] is mapping -%}3 unmodified lines {%- set ns_args.found_first = true -%} {{- key -}}:{{- format_argument(value, escape_keys=False) -}} {%- endfor -%} {%- elif function['arguments'] is string -%} {{- function['arguments'] -}} {%- elif function['arguments'] is none -%} {%- else -%} {{- raise_exception( "chat_template: tool_calls[].function.arguments must be a " "JSON object (mapping), not a string. Deserialize arguments " "before passing to the template." ) -}} {%- endif -%} {{- '}<tool_call|>' -}} {%- endfor -%}3 unmodified lines {%- set ns_tr_out = namespace(flag=false) -%} {%- if message.get('tool_responses') -%} {#- Legacy: tool_responses embedded on the assistant message (Google/Gemma native) -#} {%- for tool_response in message['tool_responses'] -%} {{- format_tool_response_block(tool_response['name'] | default('unknown'), tool_response['response']) -}} {%- for tool_response in message.get('tool_responses') -%} {{- format_tool_response_block(tool_response['name'] | default('unknown', true), tool_response['response']) -}} {%- set ns_tr_out.flag = true -%} {%- set ns.prev_message_type = 'tool_response' -%} {%- endfor -%}7 unmodified lines {%- else -%} {%- set follow = loop_messages[k] -%} {#- Resolve tool_call_id to function name -#} {%- set ns_tname = namespace(name=follow.get('name') | default('unknown')) -%} {%- for tc in message['tool_calls'] -%} {%- set ns_tname = namespace(name=follow.get('name') or 'unknown') -%} {%- for tc in message.get('tool_calls') -%} {%- if tc.get('id') == follow.get('tool_call_id') -%} {%- set ns_tname.name = tc['function']['name'] -%} {%- endif -%}10 unmodified lines {%- endif -%} {%- endfor -%} {{- format_tool_response_block(ns_tname.name, ns_txt.s) -}} {%- for part in tool_body -%} {%- if part.get('type') in ['image', 'image_url'] -%} {{- '<|image|>' -}} {%- elif part.get('type') in ['audio', 'input_audio'] -%} {{- '<|audio|>' -}} {%- elif part.get('type') == 'video' -%} {{- '<|video|>' -}} {%- endif -%} {%- endfor -%} {%- else -%} {{- format_tool_response_block(ns_tname.name, tool_body) -}} {%- endif -%}3 unmodified lines {%- endfor -%} {%- endif -%}
{%- if message['content'] is string -%} {%- set captured_content -%} {%- if message.get('content') is string -%} {%- if role == 'model' -%} {{- strip_thinking(message['content']) -}} {%- else -%} {{- message['content'] | trim -}} {%- endif -%} {%- elif message['content'] is sequence -%} {%- elif message.get('content') is sequence -%} {%- for item in message['content'] -%} {%- if item['type'] == 'text' -%} {%- if item.get('type') == 'text' -%} {%- if role == 'model' -%} {{- strip_thinking(item['text']) -}} {%- else -%} {{- item['text'] | trim -}} {%- endif -%} {%- elif item['type'] == 'image' -%} {%- elif item.get('type') in ['image', 'image_url'] -%} {{- '<|image|>' -}} {%- set ns.prev_message_type = 'image' -%} {%- elif item['type'] == 'audio' -%} {%- elif item.get('type') in ['audio', 'input_audio'] -%} {{- '<|audio|>' -}} {%- set ns.prev_message_type = 'audio' -%} {%- elif item['type'] == 'video' -%} {%- elif item.get('type') == 'video' -%} {{- '<|video|>' -}} {%- set ns.prev_message_type = 'video' -%} {%- endif -%} {%- endfor -%} {%- endif -%} {%- endset -%}
{{- captured_content -}} {%- set has_content = captured_content | trim | length > 0 -%}
{#- Forward-scan: find next non-tool message role for continuation detection -#} {%- set next_nt = namespace(role=None, found=false) -%} {%- for j in range(loop.index0 + 1, loop_messages | length) -%} {%- if not next_nt.found -%} {%- if loop_messages[j]['role'] != 'tool' -%} {%- set next_nt.role = loop_messages[j]['role'] -%} {%- set next_nt.found = true -%} {%- endif -%} {%- endif -%} {%- endfor -%}
{%- set continues_into_next = ( role == 'model' and next_nt.role == 'assistant' and (not message.get('tool_calls') or ns_tr_out.flag) ) -%}
{%- if ns.prev_message_type == 'tool_call' and not ns_tr_out.flag -%} {{- '<|tool_response>' -}} {%- elif not (ns_tr_out.flag and not message.get('content')) -%} {%- elif continues_into_next -%} {%- elif not (ns_tr_out.flag and not has_content and not next_nt.found) -%} {{- '<turn|>\n' -}} {%- endif -%}
{#- Track previous non-tool role for next iteration (avoids O(n) backward scan) -#} {%- set ns.prev_non_tool_role = message['role'] -%} {%- endif -%}{%- endfor -%}
{%- if add_generation_prompt -%} {%- if ns.prev_message_type != 'tool_response' and ns.prev_message_type != 'tool_call' -%} {{- '<|turn>model\n' -}} {%- elif ns.prev_message_type == 'tool_response' and enable_thinking -%} {{- '<|channel>thought\n' -}} {%- endif -%}{%- endif -%}{%- endif -%}11 unmodified lines1213141515161718181920215 unmodified lines2728293030313233343435363728 unmodified lines66676869706970717272737475757677782 unmodified lines8182838484858687888889909127 unmodified lines1191201211221221231241241251261261271281291301311321321331341351 unmodified line13713813914014014114214214314414514514614714811 unmodified lines "attention_logit_cap": 50.0, "chunk_size_feed_forward": 0, "conv_kernel_size": 5, "dtype": "bfloat16", "dtype": "float32", "gradient_clipping": 10000000000.0, "hidden_act": "silu", "hidden_size": 8, "hidden_size": 32, "id2label": { "0": "LABEL_0", "1": "LABEL_1"5 unmodified lines "LABEL_1": 1 }, "model_type": "gemma4_audio", "num_attention_heads": 2, "num_attention_heads": 4, "num_hidden_layers": 1, "output_attentions": false, "output_hidden_states": false, "output_proj_dims": 8, "output_proj_dims": 32, "problem_type": null, "residual_weight": 0.5, "return_dict": true,28 unmodified lines "eos_token_id": 1, "expert_intermediate_size": null, "final_logit_softcapping": 30.0, "global_head_dim": 4, "head_dim": 4, "global_head_dim": 8, "head_dim": 8, "hidden_activation": "gelu_pytorch_tanh", "hidden_size": 8, "hidden_size": 32, "hidden_size_per_layer_input": 1, "initializer_range": 0.02, "intermediate_size": 32, "intermediate_size": 64, "layer_types": [ "sliding_attention", "full_attention",2 unmodified lines "max_position_embeddings": 131072, "model_type": "gemma4_text", "moe_intermediate_size": null, "num_attention_heads": 2, "num_attention_heads": 4, "num_experts": null, "num_global_key_value_heads": null, "num_hidden_layers": 3, "num_key_value_heads": 1, "num_key_value_heads": 2, "num_kv_shared_layers": 1, "pad_token_id": 0, "rms_norm_eps": 1e-06,27 unmodified lines "attention_dropout": 0.0, "chunk_size_feed_forward": 0, "default_output_length": 70, "dtype": "bfloat16", "dtype": "float32", "global_head_dim": 64, "head_dim": 4, "head_dim": 8, "hidden_activation": "gelu_pytorch_tanh", "hidden_size": 8, "hidden_size": 32, "id2label": { "0": "LABEL_0", "1": "LABEL_1" }, "initializer_range": 0.02, "intermediate_size": 32, "intermediate_size": 64, "is_encoder_decoder": false, "label2id": { "LABEL_0": 0,1 unmodified line }, "max_position_embeddings": 131072, "model_type": "gemma4_vision", "num_attention_heads": 2, "num_attention_heads": 4, "num_hidden_layers": 1, "num_key_value_heads": 2, "num_key_value_heads": 4, "output_attentions": false, "output_hidden_states": false, "patch_size": 2, "patch_size": 16, "pooling_kernel_size": 3, "position_embedding_size": 10240, "problem_type": null,12323version https://git-lfs.github.com/spec/v1oid sha256:feaf3cfe37e063524c5a46b94a145b50aeb7ea07d6b983070dda965ed0248c86size 11997808oid sha256:c2645ac53c3989c11a75141407c3016e1c732a38eb306ea75fb9b53d7b64b191size 4001735212 unmodified lines131415161617181913 unmodified lines3334353636373839404142434243444546474849504647524849505113 unmodified lines656667726869707112 unmodified lines "max_frequency": 8000.0, "mel_floor": 0.001, "min_frequency": 0.0, "padding_side": "right", "padding_side": "left", "padding_value": 0.0, "per_bin_mean": null, "per_bin_stddev": null,13 unmodified lines 0.0 ], "image_processor_type": "Gemma4ImageProcessor", "image_seq_length": 70, "image_seq_length": 280, "image_std": [ 1.0, 1.0, 1.0 ], "max_soft_tokens": 70, "patch_size": 2, "max_soft_tokens": 280, "patch_size": 16, "pooling_kernel_size": 3, "resample": 3, "rescale_factor": 0.00392156862745098, "size": { "height": 32, "width": 32 } "rescale_factor": 0.00392156862745098 }, "image_seq_length": 70, "image_seq_length": 280, "processor_class": "Gemma4Processor", "video_processor": { "do_convert_rgb": true,13 unmodified lines ], "max_soft_tokens": 70, "num_frames": 32, "patch_size": 2, "patch_size": 16, "pooling_kernel_size": 3, "resample": 3, "rescale_factor": 0.00392156862745098,83 unmodified lines848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351 unmodified line13713813914014114283 unmodified lines "type": "object", "x-regex": "(\\<\\|channel\\>thought\\n(?P<thinking>.*?)\\<channel\\|\\>)?(?P<tool_calls>\\<\\|tool_call\\>.*\\<tool_call\\|\\>)?(?P<content>(?:(?!\\<turn\\|\\>)(?!\\<\\|tool_response\\>).)+)?(?:\\<turn\\|\\>|\\<\\|tool_response\\>)?" }, "response_template": { "defaults": { "role": "assistant" }, "fields": { "content": { "close": [ "<turn|>", "<|tool_response>", "<eos>" ], "content": "text" }, "thinking": { "close": "<channel|>", "content": "text", "open": "<|channel>thought\n" }, "tool_calls": { "close": "<tool_call|>", "content": "json", "content_args": { "string_delims": [ [ "<|\"|>", "<|\"|>" ] ], "unquoted_keys": true }, "open_pattern": "<\\|tool_call>call:(?P<name>\\w+)", "repeats": true, "transform": { "function": { "arguments": "{content}", "name": "{name}" }, "type": "function" } } }, "start_anchor": [ "<|turn>model\n", "<tool_response|>" ] }, "soc_token": "<|channel>", "sot_token": "<|turn>", "stc_token": "<|tool_call>",1 unmodified line "str_token": "<|tool_response>", "think_token": "<|think|>", "tokenizer_class": "GemmaTokenizer", "truncation_side": "left", "unk_token": "<unk>"}chat_template.jinja+88-46
config.json+17-17
model.safetensors+2-2
processor_config.json+7-11
tokenizer_config.json+47