skip to content

In TRL's SFTTrainer, how do conversational and prompt-completion datasets differ, and which SFTConfig flags decide which tokens the loss trains on?

level: seniorimportance: should knowfreq 14%

answer

  1. column names choose the path
  2. messages versus prompt and completion
  3. None follows the dataset type
  4. an assistant flag for chat rows only
  5. templates need generation markers

basics

~10 s

In TRL 1.14 a conversational row holds a messages list; a prompt-completion row holds separate prompt and completion. completion_only_loss=None trains only completions on prompt-completion data, while messages data trains the whole conversation unless assistant_only_loss=True.

solid answer

~40 s

`SFTTrainer` reads the columns: `text` or `messages` is language-modeling data, `prompt` plus `completion` is prompt-completion data, and either may be plain strings or role/content message lists, in which case the chat template is applied for you. In TRL 1.14 `completion_only_loss` defaults to `None`, meaning completion-only for prompt-completion rows and full-sequence for language-modeling rows. `assistant_only_loss` defaults to `False`; set to `True` it trains only assistant turns, but it raises `ValueError` on non-conversational data and needs a chat template with generation markers, which TRL patches in for known model families and otherwise refuses. The quiet trap is a `messages` dataset left at defaults: system and user turns train too.

go deeper

for a junior

Recall the four row shapes SFTTrainer accepts - text, messages, prompt with completion, and the conversational prompt-completion form - and that chat templates are applied for you.

for a middle

Explain the defaults: completion_only_loss None follows the dataset type, and assistant_only_loss is off, so messages data trains every turn.

for a senior

Pick the wiring that trains only what you intend and predict the failures: non-conversational data raises, unknown templates raise, and a missing end-of-turn mask only warns.

for a principal

Decide a data contract for a team's SFT datasets - which row shape, which flags - so every run masks the same tokens and results stay comparable across models.

## How SFTTrainer reads a dataset TRL's `SFTTrainer` decides what to do with a row by looking at its column names. There are two **types** of data and two **formats** for each. | | Standard (plain strings) | Conversational (role/content messages) | |---|---|---| | **Language modeling** | `{"text": "..."}` | `{"messages": [{"role": "user", ...}, {"role": "assistant", ...}]}` | | **Prompt-completion** | `{"prompt": "...", "completion": "..."}` | `{"prompt": [{"role": "user", ...}], "completion": [{"role": "assistant", ...}]}` | When the rows are conversational, the trainer applies the tokenizer's chat template for you, so the dataset should hold messages rather than pre-rendered strings. The text column name defaults to `"text"` and is changed with `dataset_text_field`. ## The two loss flags in TRL 1.14 The question that matters in practice is which tokens contribute to the loss. Two `SFTConfig` flags control it; why restricting the loss helps is method material, and this answer stays with the switches. - **`completion_only_loss`** - default `None`. `None` means: prompt-completion data trains on the completion only, language-modeling data trains on the whole sequence. `False` forces the full sequence on prompt-completion data. It has no prompt/completion boundary to use on `text` or `messages` rows. - **`assistant_only_loss`** - default `False`. When `True`, only assistant turns contribute. It is supported **only for conversational data**: on plain text the trainer raises `ValueError` ("the dataset is not conversational"). Padding is always excluded: the trainer sets padded label positions to -100. ## The quiet default on chat data A `messages` dataset is **language-modeling** data as far as `completion_only_loss` is concerned. Left at defaults, every token trains: the system prompt, the user turns and the assistant turns. Nothing warns you, because this is the documented behaviour. If the intent is to teach replies only, choose one of these: 1. **`messages` plus `assistant_only_loss=True`** - every assistant turn in the conversation trains. 2. **Conversational prompt-completion rows** - the earlier turns go into `prompt`, the reply to learn goes into `completion`; by default only the completion trains, so earlier assistant turns in the prompt do not. 3. **Both together** - TRL's docs state the two settings are compatible: conversational prompt-completion data with `assistant_only_loss=True`. ## What assistant_only_loss needs from the template To mask by role, the trainer asks the tokenizer for an assistant mask while applying the chat template, which works only when the template marks assistant spans with generation markers. In TRL 1.14: - If the template already has the markers, it is used as is. - For known model families (the TRL docs name Qwen3 as an example), TRL swaps in a bundled training template automatically. - Otherwise TRL raises an error asking you to modify the template yourself. - If a tokenized row still ends up with no assistant tokens, the trainer raises a `RuntimeError` naming the missing generation keyword. - If the template leaves the assistant's end-of-turn token outside the mask, TRL logs a warning that the model may not learn to stop. How those markers are written in a template is chat-template material; the wiring point is that the flag depends on them. ## Other interactions worth knowing - **`formatting_func`** turns rows into plain text, so the trainer treats the data as language modeling; combining it with completion-only loss raises a `ValueError`. - **Pre-tokenized data**: a dataset with `input_ids` is accepted; a `labels` column is used as is, otherwise labels are built from `assistant_masks` or (when completion-only loss applies) `completion_mask` columns. - **`max_length`** (default 1024) truncates every tokenized sequence, keeping its start; a long conversation can lose its final reply that way. ## Checking the mask before a long run After construction, `SFTTrainer` has already tokenized the data and built a `labels` column in which every excluded position is -100. Decoding the positions that stay in the loss is the fastest way to confirm the flags did what you meant: ```python row = trainer.train_dataset[0] kept = [t for t, l in zip(row["input_ids"], row["labels"]) if l != -100] print(tokenizer.decode(kept)) # should show only the reply text you intend to train ``` If system or user text appears in that output, the dataset shape or the flags are wrong, and it is cheaper to find out here than after a day of GPU time. ## A worked choice A support-chat corpus with multi-turn conversations, where only the agent's replies should be learned, is wired as `SFTConfig(assistant_only_loss=True)` over a `messages` dataset, on a model whose template TRL recognises or that already carries the markers. A dataset of single questions with reference answers is better expressed as prompt-completion rows, where the default already trains only the answer and no template change is needed.

  • When would you use conversational prompt-completion rows rather than a messages dataset with assistant_only_loss=True?
    Prompt-completion rows train only the `completion`, so earlier assistant turns placed in `prompt` are context, not targets - right when only the final reply is the behaviour to learn. A `messages` dataset with `assistant_only_loss=True` trains every assistant turn. TRL's docs say the two combine: conversational prompt-completion data with `assistant_only_loss=True`.
  • You set assistant_only_loss=True on a model whose chat template TRL does not recognise. What happens?
    If the template lacks generation markers and TRL 1.14 has no bundled training template for that family, the trainer raises an error asking you to modify the template. If a row still yields no assistant tokens after tokenisation, it raises a `RuntimeError` naming the missing generation keyword. Either way the run stops before training.

  • SFTTrainer trains only on assistant replies by default for chat data.
  • completion_only_loss must be set to True before prompts are masked on prompt-completion data.
  • assistant_only_loss works on plain text datasets that contain role prefixes.
  • A formatting_func can be combined freely with completion-only loss.
  • Any chat template supports assistant_only_loss without changes.

Assumes TRL 1.14 on Transformers v5: completion_only_loss defaults to None and assistant_only_loss to False in this TRL release.