New Tool Radar

Local AI

How to Use a Custom Chat Template with an MLX Language Model

Write your own Jinja chat template, set it on the tokenizer's chat_template attribute, and render messages with mlx-lm's apply_chat_template before generating.

Schematic diagram with three steps: print the model's current chat template, write a Jinja template and set it on the tokenizer, and render messages and generate with the new template.

An instruct-tuned model expects its conversation wrapped in a specific format — markers for each role, a marker for where the assistant’s reply starts — and mlx-lm normally uses the template that ships with the model. But sometimes you want a different wrapping: a plain format for debugging, or markers that match another system. The tokenizer lets you supply your own. A chat template is a Jinja template stored in the tokenizer’s chat_template attribute, and the Hugging Face docs put the core move in one sentence: write a Jinja template and set tokenizer.chat_template. This guide is about supplying that custom template; it is different from the mlx_lm.chat guide, which chats in the REPL with the model’s own template, and from the one-off generate guide, which sends a plain prompt without a template.

Load the model and print its current template

Install mlx-lm if you have not already — the README gives pip install mlx-lm — then load a model and look at the template it ships with. The README’s default model is mlx-community/Llama-3.2-3B-Instruct-4bit, and its Python API example loads a model and renders messages like this:

from mlx_lm import load, generate
model, tokenizer = load("mlx-community/Llama-3.2-3B-Instruct-4bit")
print(tokenizer.chat_template)

The docs advise starting from an existing template: use print(tokenizer.chat_template) on any chat model to see the template it is using. Read what it prints before you change anything — it shows the role markers and special tokens the model was trained with, which is the format your custom template is replacing.

Write your own template and set it

Write a Jinja template as a string and assign it to the tokenizer’s chat_template attribute. The Hugging Face docs give this example template, which prints each message’s role and content, followed by the end-of-sequence token, and adds an assistant header when add_generation_prompt is true:

template = '''{%- for message in messages %}
    {{- '<|' + message['role'] + '|>\n' }}
    {{- message['content'] + eos_token }}
{%- endfor %}
{%- if add_generation_prompt %}
    {{- '<|assistant|>\n' }}
{%- endif %}'''
tokenizer.chat_template = template

Once set, the template is used whenever you call apply_chat_template(). The docs also note that the template is saved with the tokenizer whenever save_pretrained() or push_to_hub() is called, in a chat_template.jinja file — so if you want to keep your custom template, save the tokenizer; otherwise the change lasts only for the current session. Use the - in the Jinja line syntax, as in the example above, to trim whitespace: the docs warn that extra whitespace printed into the prompt was not present during model training and can harm performance.

Render messages and generate with the new template

Render a message list with the custom template, print the rendered prompt to check it, and then generate from it. The README’s Python API example renders messages and generates like this:

messages = [{"role": "user", "content": "How tall is Mt Everest?"}]
prompt = tokenizer.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
print(prompt)
print(generate(model, tokenizer, prompt=prompt, max_tokens=20, verbose=False))

The printed prompt should show your template’s markers: the user message wrapped with the role header, the content, and the end-of-sequence token, followed by the assistant header at the end because add_generation_prompt is true. Compare that printed prompt with the one the model’s own template produced in the first step — the difference is exactly what your custom template changed. To go back to the model’s own template, load the model again, which restores the template that shipped with it.

Try it: four checks

Check What you should see If you do not see it
Printed print(tokenizer.chat_template) shows the model’s own template text Load the model first with load("mlx-community/Llama-3.2-3B-Instruct-4bit") — the template comes from the tokenizer
Set Printing tokenizer.chat_template again shows your Jinja template Assign the string to tokenizer.chat_template — writing the template alone does not install it
Rendered The rendered prompt shows your role markers and ends with the assistant header Pass add_generation_prompt=True to apply_chat_template — without it, no assistant header is added
Generated generate returns text continuing from the rendered prompt Feed the rendered prompt string to generate — do not pass the raw message list

Expected result. We checked the mlx-lm README for the pip install mlx-lm command, the default model mlx-community/Llama-3.2-3B-Instruct-4bit, the load, apply_chat_template, and generate Python API example, and the mlx_lm.chat REPL; and the Hugging Face chat-template writing docs for a chat template being a Jinja template stored in the tokenizer’s chat_template attribute, writing your own Jinja template and setting tokenizer.chat_template, the template being used whenever you call apply_chat_template(), the example template with role headers, message content, eos_token, and the add_generation_prompt assistant header, printing tokenizer.chat_template to see the model’s template, trimming whitespace with -, saving with save_pretrained() or push_to_hub() into chat_template.jinja, and the warning that the template should match the format the model was trained with. We did not run mlx-lm or generate any text.

Limits

The docs warn that the chat template should always match the format the model was trained with, so a custom template that changes the markers is an experiment, not an upgrade — the model can answer worse, or ramble past the end of its reply, when the format differs from its training. The example template here is the docs’ own simple one, meant to show the mechanism, not a format tuned for any particular model. Setting chat_template in memory lasts only for the current session unless you save the tokenizer, and reloading the model restores the template that shipped with it. We did not run mlx-lm, so no rendered prompt here is a recorded result.

Sources

See all