Looper Documentation
Looper Cookbook

Message formatting

Looper trains and samples token ids, not text. A chat model also expects every conversation in its own format, with special tokens that mark where each message starts and ends, and who wrote it. For example, Qwen3.5 sees the question "What is 6 times 7?" as <|im_start|>user\nWhat is 6 times 7?<|im_end|>, followed by the start of its own reply.

A message formatter is the cookbook code that converts between your conversations and the model's tokens. The cookbook has one for each model family, and it works in both directions:

Use a formatter with the same settings to train, sample and evaluate a model. The model is then sampled in exactly the format it was trained in.

With the cookbook, you build the formatter once and pass it to ChatDataset for SFT, or to the RL loops. The cookbook then calls its methods for you. You call them yourself when you write your own loop, or want to check your data.

This page covers:

Words used on this page:

Word Meaning
Message One entry in a conversation, such as the user's question or the model's reply.
Turn One message from the assistant, which is what the model writes in one go.
Prompt The token ids the sampler is given. They hold the conversation so far, then the start of the next assistant turn.
Datum One training example, made of token ids plus a weight for each. See Prepare data.
Target A token the model is trained to write, with weight 1. The rest of a datum is context, with weight 0.
End token The special token that ends a turn, such as `<
Reasoning The thinking a reasoning model writes before its answer. It's kept apart from the answer as reasoning_content.

Build your model's formatter

Build a formatter with message_formatter, from the model's name and its Hugging Face tokenizer. The tokenizer runs on your machine, and only supplies the vocabulary, so no model weights are loaded.

from transformers import AutoTokenizer

from looper_cookbook.message_formatters.models import message_formatter

tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen3.5-9B")
formatter = message_formatter("Qwen/Qwen3.5-9B", tokenizer, enable_thinking=False)

message_formatter(model, tokenizer, *, enable_thinking=True) builds the formatter for a model Looper serves. Name the model as your training run names it. enable_thinking sets whether a Qwen model reasons before it answers (see Thinking).

Each model family's formatter is also a class in looper_cookbook.message_formatters. You can build it directly when you need the family's other settings:

Models Formatter Settings
Qwen/Qwen3.5-4B, Qwen/Qwen3.5-9B, Qwen/Qwen3.5-35B-A3B qwen3_5.Qwen3_5MessageFormatter enable_thinking
Qwen/Qwen3.8-27B qwen3_8.Qwen3_8MessageFormatter enable_thinking, reasoning_effort, preserve_thinking
meta-models/Muse-Glimmer-30B muse_glimmer.MuseGlimmerMessageFormatter reasoning_strength, current_date
Qwen3's thinking models, such as Qwen/Qwen3-8B qwen3.Qwen3MessageFormatter enable_thinking

Each formatter is written by hand from its model's chat template, and writes the same text the template does. It takes the model's own tokenizer, and refuses another model's.

Every formatter has the same calls:

Call What it gives you
build_generation_prompt(messages, tools=None) A prompt, made of the conversation as token ids, then the start of the next assistant turn. The model writes that turn.
build_supervised_examples(messages, tools=None, *, max_seq_len) One datum for each assistant turn in the conversation.
parse_response(tokens, tools=None) The assistant message in a sampled turn, with its reasoning and tool calls, and whether the turn ended on its end token.
stop_token_ids The end token, for the sampler to stop on.
targets(datum) The text a datum trains on, so you can check your data before you train.

Messages are chat messages. Each has a role of system, user, assistant or tool, and a text content. An assistant message can also have reasoning_content and tool_calls.

Prompts for a sampler

To have the model answer a conversation, turn it into a prompt with build_generation_prompt. The prompt holds the conversation so far, then the start of the next assistant turn, so the model's next tokens are its reply. Tell the sampler to stop on the formatter's end token, so it stops when the reply ends:

from looper_sdk import SamplingParams

prompt = formatter.build_generation_prompt([{"role": "user", "content": "What is 6 times 7?"}])
params = SamplingParams(max_tokens=256, stop_token_ids=formatter.stop_token_ids)
print(repr(tokenizer.decode(prompt)))
'<|im_start|>user\nWhat is 6 times 7?<|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\n'

The prompt ends with <|im_start|>assistant, where the model's reply begins. The empty <think> block is there because this formatter was built with thinking off.

Sampling shows how to send the prompt and its parameters to a sampler.

Datums for a run

To train on a conversation, turn it into datums with build_supervised_examples. You get one datum for each assistant turn, in order. Each datum is that turn's prompt, exactly as the model would see it when sampled, followed by the turn itself:

This conversation about foxes has two assistant turns, so it gives two datums:

conversation = [
    {"role": "user", "content": "Tell me about the red fox."},
    {"role": "assistant", "content": "Habitat: forests, grasslands and cities."},
    {"role": "user", "content": "And the Arctic fox?"},
    {"role": "assistant", "content": "Habitat: the tundra and sea ice."},
]
for datum in formatter.build_supervised_examples(conversation, max_seq_len=2048):
    print(len(datum.tokens), formatter.targets(datum))
31 ['Habitat: forests, grasslands and cities.<|im_end|>']
57 ['Habitat: the tundra and sea ice.<|im_end|>']

The second datum is longer, because its context includes the first exchange. targets shows that each datum trains only its own turn.

Why one datum per turn. Some formats write a turn differently once later turns follow it. For example, Qwen3.5 keeps a turn's reasoning only after the last user message, so an earlier turn loses its reasoning once the conversation goes on. If the whole conversation were trained as one sequence, each early turn would follow a prompt the model never sees when it's sampled. One datum per turn trains each turn after exactly the prompt it will be sampled with. The cost is that a conversation with T assistant turns gives T datums, each holding the turns before it as context.

A file of conversations. read_conversations(path), in looper_cookbook.datasets.conversations, reads a JSONL file with one conversation per line, as {"messages": [...]}, with optional "tools". Supervised learning trains on such a file.

Replies read back

When the sampler returns a turn, it's token ids. parse_response reads them back as an assistant message, {"role": "assistant", "content": ...}, and also says whether the turn ended on its end token.

This reads one full reply, and the same reply cut off after 4 tokens:

from looper_cookbook.message_formatters.qwen3_5 import Qwen3_5MessageFormatter

thinking = Qwen3_5MessageFormatter(tokenizer)
reply = tokenizer.encode("6 times 7 is 42.\n</think>\n\n42<|im_end|>", add_special_tokens=False)
print(thinking.parse_response(reply))
print(thinking.parse_response(reply[:4]))
({'role': 'assistant', 'reasoning_content': '6 times 7 is 42.', 'content': '42'}, True)
({'role': 'assistant', 'reasoning_content': '6 times 7', 'content': ''}, False)

Tools

A model can call tools, such as a calculator, while it answers. You describe the tools as JSON schemas, in the tools format that chat APIs use, and pass them to the formatter. The formatter lists them in the prompt where the model's format expects them, and tells the model how to call one.

When the model calls a tool, parse_response returns the calls as the message's tool_calls, each with its arguments as a mapping. Your code runs the tool, and then continues the prompt with the tool's reply, so the model can go on.

This example gives the model a multiply tool, reads a call the model made, and adds the tool's reply to the prompt:

tools = [
    {
        "type": "function",
        "function": {
            "name": "multiply",
            "description": "Multiply two numbers.",
            "parameters": {
                "type": "object",
                "properties": {"a": {"type": "number"}, "b": {"type": "number"}},
                "required": ["a", "b"],
            },
        },
    }
]
messages = [{"role": "user", "content": "What is 6 times 7?"}]
prompt = formatter.build_generation_prompt(messages, tools)

# A turn the model sampled after that prompt, written here by hand.
sampled = tokenizer.encode(
    "<tool_call>\n<function=multiply>\n<parameter=a>\n6\n</parameter>\n"
    "<parameter=b>\n7\n</parameter>\n</function>\n</tool_call><|im_end|>",
    add_special_tokens=False,
)
message, ended = formatter.parse_response(sampled, tools)
print(message["tool_calls"])

# The next prompt: every token so far, then the tool's reply.
reply = {"role": "tool", "name": "multiply", "content": "42"}
continued = formatter.continue_prompt([*prompt, *sampled], [reply])
print(repr(tokenizer.decode(continued[len(prompt) + len(sampled) :])))
messages += [message, reply]
[{'type': 'function', 'function': {'name': 'multiply', 'arguments': {'a': 6, 'b': 7}}}]
'\n<|im_start|>user\n<tool_response>\n42\n</tool_response><|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\n'

In a rollout, continue the prompt. Don't format the whole conversation again. continue_prompt(transcript, replies) keeps the tokens so far exactly as they were sampled, and adds only the tools' replies and the start of the next turn. So an attempt stays one token sequence, and the log probabilities the sampler recorded still belong to the tokens you train on. The cookbook's episode session, AgentSession, does this for you.

A conversation with tool calls trains the same way as any other. Pass its tools to build_supervised_examples, and a turn that calls a tool becomes a datum whose targets are its calls:

messages.append({"role": "assistant", "content": "6 times 7 is 42."})
for datum in formatter.build_supervised_examples(messages, tools, max_seq_len=2048):
    print(formatter.targets(datum))
['<tool_call>\n<function=multiply>\n<parameter=a>\n6\n</parameter>\n<parameter=b>\n7\n</parameter>\n</function>\n</tool_call><|im_end|>']
['6 times 7 is 42.<|im_end|>']

Things to know:

Thinking

Reasoning models can think before they answer. Each family has its own settings for this. Whatever you choose, train and sample the model with the same settings.

Qwen3.8 at low effort puts its instruction in a system turn of its own, before the question:

from looper_cookbook.message_formatters.qwen3_8 import Qwen3_8MessageFormatter

question = [{"role": "user", "content": "What is 6 times 7?"}]
qwen3_8_tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen3.8-27B")
qwen3_8 = Qwen3_8MessageFormatter(qwen3_8_tokenizer, reasoning_effort="low")
print(repr(qwen3_8_tokenizer.decode(qwen3_8.build_generation_prompt(question))))
'<|im_start|>system\nReasoning effort is set to low. Keep your thinking brief and focused, moving directly to the conclusion without unnecessary elaboration.<|im_end|>\n<|im_start|>user\nWhat is 6 times 7?<|im_end|>\n<|im_start|>assistant\n<think>\n'

Muse Glimmer starts every conversation with a system turn. It also addresses each part of an assistant turn to a recipient. The reasoning goes to=self, then the answer goes to=user. The turn ends on <|eot|>, which is what its formatter stops on:

from looper_cookbook.message_formatters.muse_glimmer import MuseGlimmerMessageFormatter

muse_tokenizer = AutoTokenizer.from_pretrained("meta-models/Muse-Glimmer-30B")
muse = MuseGlimmerMessageFormatter(muse_tokenizer, current_date="2026-11-18")
print(repr(muse_tokenizer.decode(muse.build_generation_prompt(question))))

reasoned = [*question, {"role": "assistant", "reasoning_content": "6 * 7 = 42.", "content": "42"}]
[datum] = muse.build_supervised_examples(reasoned, max_seq_len=2048)
print(muse.targets(datum))
print("a turn ends on", muse_tokenizer.decode(muse.stop_token_ids))
'<|begin_of_text|><|start|>system<|message|>You are a helpful AI assistant.\nKnowledge cutoff: 2026-01-04.\nCurrent date: 2026-11-18.\n\nReasoning strength: high.\n\n# Valid recipients: "self", "user".<|eot|><|start|>user<|message|>What is 6 times 7?<|eot|><|start|>assistant'
[' to=self<|message|>6 * 7 = 42.<|eom|><|start|>assistant to=user<|message|>42<|eot|>']
a turn ends on <|eot|>

When a Muse Glimmer turn calls a tool, it addresses the call to the tool's name. Any text next to the call goes to the user first, and the turn goes on:

called = {
    "role": "assistant",
    "content": "Let me work it out.",
    "tool_calls": [{"type": "function", "function": {"name": "multiply", "arguments": {"a": 6, "b": 7}}}],
}
[datum] = muse.build_supervised_examples([*question, called], tools, max_seq_len=2048)
print(muse.targets(datum))
[' to=user<|message|>Let me work it out.<|eom|><|start|>assistant to=multiply<|message|><atem:function_calls>\n<atem:invoke name="multiply">\n<atem:parameter name="a">6</atem:parameter>\n<atem:parameter name="b">7</atem:parameter>\n</atem:invoke>\n</atem:function_calls><|eot|>']

What is refused

Some conversations can't be written in a model's format. Rather than build a wrong prompt or datum, the formatter raises an error that names the turn and says how to fix it:

Next steps