Skip to content

Instantly share code, notes, and snippets.

@mostlygeek
Created November 30, 2025 07:54
Show Gist options
  • Select an option

  • Save mostlygeek/6fe263bad8026dca73cb6f5470dfdb0d to your computer and use it in GitHub Desktop.

Select an option

Save mostlygeek/6fe263bad8026dca73cb6f5470dfdb0d to your computer and use it in GitHub Desktop.
qwen3-coder-30B jinja template, works with Claude Code
{# ============================================================================
MACRO: render_extra_keys
Renders any JSON keys not already handled into XML-style tags
Adapted from: https://huggingface.co/ggml-org/Qwen3-Coder-30B-A3B-Instruct-Q8_0-GGUF/blob/main/qwen3-coder-30b-a3b-instruct-q8_0.gguf
============================================================================ #}
{% macro render_extra_keys(json_dict, handled_keys) %}
{%- if json_dict is mapping %}
{%- for json_key in json_dict if json_key not in handled_keys %}
{%- if json_dict[json_key] is mapping %}
{{- '\n<' ~ json_key ~ '>' ~ (json_dict[json_key] | tojson | safe) ~ '</' ~ json_key ~ '>' }}
{%- else %}
{{- '\n<' ~ json_key ~ '>' ~ (json_dict[json_key] | string) ~ '</' ~ json_key ~ '>' }}
{%- endif %}
{%- endfor %}
{%- endif %}
{% endmacro %}
{# ============================================================================
INITIALIZATION: Extract system message and set up loop variables
============================================================================ #}
{#- Check if first message is a system message -#}
{%- if messages[0]["role"] == "system" %}
{%- set system_message = messages[0]["content"] %}
{%- set loop_messages = messages[1:] %}
{%- else %}
{%- set loop_messages = messages %}
{%- endif %}
{#- Initialize tools if not defined -#}
{%- if not tools is defined %}
{%- set tools = [] %}
{%- endif %}
{# ============================================================================
SYSTEM MESSAGE SECTION
============================================================================ #}
{%- if system_message is defined %}
{{- "<|im_start|>system\n" + system_message }}
{%- else %}
{%- if tools is iterable and tools | length > 0 %}
{{- "<|im_start|>system\nYou are Qwen, a helpful AI assistant that can interact with a computer to solve tasks." }}
{%- endif %}
{%- endif %}
{# ============================================================================
TOOLS DEFINITION SECTION
============================================================================ #}
{%- if tools is iterable and tools | length > 0 %}
{{- "\n\nYou have access to the following functions:\n\n" }}
{{- "<tools>" }}
{#- Iterate through each tool -#}
{%- for tool in tools %}
{%- if tool.function is defined %}
{%- set tool = tool.function %}
{%- endif %}
{{- "\n<function>\n<name>" ~ tool.name ~ "</name>" }}
{#- Tool description -#}
{%- if tool.description is defined %}
{{- '\n<description>' ~ (tool.description | trim) ~ '</description>' }}
{%- endif %}
{{- '\n<parameters>' }}
{#- Tool parameters -#}
{%- if tool.parameters is defined
and tool.parameters is mapping
and tool.parameters.properties is defined
and tool.parameters.properties is mapping %}
{%- for param_name, param_fields in tool.parameters.properties | items %}
{{- '\n<parameter>' }}
{{- '\n<name>' ~ param_name ~ '</name>' }}
{%- if param_fields.type is defined %}
{{- '\n<type>' ~ (param_fields.type | string) ~ '</type>' }}
{%- endif %}
{%- if param_fields.description is defined %}
{{- '\n<description>' ~ (param_fields.description | trim) ~ '</description>' }}
{%- endif %}
{%- set handled_keys = ['name', 'type', 'description'] %}
{{- render_extra_keys(param_fields, handled_keys) }}
{{- '\n</parameter>' }}
{%- endfor %}
{%- endif %}
{%- set handled_keys = ['type', 'properties'] %}
{{- render_extra_keys(tool.parameters, handled_keys) }}
{{- '\n</parameters>' }}
{%- set handled_keys = ['type', 'name', 'description', 'parameters'] %}
{{- render_extra_keys(tool, handled_keys) }}
{{- '\n</function>' }}
{%- endfor %}
{{- "\n</tools>" }}
{#- Tool usage instructions -#}
{{- '\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>' }}
{%- endif %}
{# ============================================================================
CLOSE SYSTEM MESSAGE
============================================================================ #}
{%- if system_message is defined %}
{{- '<|im_end|>\n' }}
{%- else %}
{%- if tools is iterable and tools | length > 0 %}
{{- '<|im_end|>\n' }}
{%- endif %}
{%- endif %}
{# ============================================================================
MESSAGE LOOP: Process all conversation messages
============================================================================ #}
{%- for message in loop_messages %}
{#- ========== ASSISTANT MESSAGE WITH TOOL CALLS ========== -#}
{%- if message.role == "assistant"
and message.tool_calls is defined
and message.tool_calls is iterable
and message.tool_calls | length > 0 %}
{{- '<|im_start|>' + message.role }}
{#- Include assistant content if present -#}
{%- if message.content is defined
and message.content is string
and message.content | trim | length > 0 %}
{{- '\n' + message.content | trim + '\n' }}
{%- endif %}
{#- Process each tool call -#}
{%- for tool_call in message.tool_calls %}
{%- if tool_call.function is defined %}
{%- set tool_call = tool_call.function %}
{%- endif %}
{{- '\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
{#- Tool call arguments -#}
{%- if tool_call.arguments is defined %}
{%- for args_name, args_value in tool_call.arguments | items %}
{{- '<parameter=' + args_name + '>\n' }}
{%- set args_value = args_value | tojson | safe if args_value is mapping else args_value | string %}
{{- args_value }}
{{- '\n</parameter>\n' }}
{%- endfor %}
{%- endif %}
{{- '</function>\n</tool_call>' }}
{%- endfor %}
{{- '<|im_end|>\n' }}
{#- ========== STANDARD MESSAGES (user/system/assistant) ========== -#}
{%- elif message.role == "user"
or message.role == "system"
or message.role == "assistant" %}
{{- '<|im_start|>' + message.role + '\n' + message.content + '<|im_end|>' + '\n' }}
{#- ========== TOOL RESPONSE MESSAGES ========== -#}
{%- elif message.role == "tool" %}
{#- Start user block if this is the first tool response -#}
{%- if loop.previtem and loop.previtem.role != "tool" %}
{{- '<|im_start|>user\n' }}
{%- endif %}
{{- '<tool_response>\n' }}
{{- message.content }}
{{- '\n</tool_response>\n' }}
{#- Close user block if this is the last tool response -#}
{%- if not loop.last and loop.nextitem.role != "tool" %}
{{- '<|im_end|>\n' }}
{%- elif loop.last %}
{{- '<|im_end|>\n' }}
{%- endif %}
{#- ========== FALLBACK FOR OTHER ROLES ========== -#}
{%- else %}
{{- '<|im_start|>' + message.role + '\n' + message.content + '<|im_end|>\n' }}
{%- endif %}
{%- endfor %}
{# ============================================================================
GENERATION PROMPT: Add assistant prompt if requested
============================================================================ #}
{%- if add_generation_prompt %}
{{- '<|im_start|>assistant\n' }}
{%- endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment