hiyouga/LlamaFactory · error · ValueError

`train_on_prompt` or `mask_history` cannot be set as True ex

Error message

`train_on_prompt` or `mask_history` cannot be set as True except SFT.

What it means

train_on_prompt (train on prompt tokens too) and mask_history (mask earlier conversation turns) are labels-masking options implemented only in the SFT supervised processor. For pt/dpo/rm/ppo/kto stages the label construction differs (e.g. preference pairs, scalar rewards), so either flag with a non-SFT stage is rejected at parse time.

Source

Thrown at src/llamafactory/hparams/parser.py:426

    else:
        model_args, data_args, training_args, finetuning_args, generating_args = _parse_train_args(args)
        finetuning_args.use_mca = False
        finetuning_args.use_megatron_bridge = False

    # Setup logging
    if training_args.should_log:
        _set_transformers_logging()

    # Check arguments
    if finetuning_args.stage != "sft":
        if training_args.predict_with_generate:
            raise ValueError("`predict_with_generate` cannot be set as True except SFT.")

        if data_args.neat_packing:
            raise ValueError("`neat_packing` cannot be set as True except SFT.")

        if data_args.train_on_prompt or data_args.mask_history:
            raise ValueError("`train_on_prompt` or `mask_history` cannot be set as True except SFT.")

    if finetuning_args.stage == "sft" and training_args.do_predict and not training_args.predict_with_generate:
        raise ValueError("Please enable `predict_with_generate` to save model predictions.")

    if finetuning_args.use_megatron_bridge:
        if finetuning_args.use_mca or finetuning_args.use_hyper_parallel:
            raise ValueError("Megatron Bridge cannot be used together with MCA or HyperParallel.")
        if finetuning_args.stage not in ["pt", "sft"]:
            raise ValueError("Megatron Bridge only supports the `pt` and `sft` stages.")
        if finetuning_args.finetuning_type not in ["full", "lora"]:
            raise ValueError("Megatron Bridge only supports `full` and `lora` finetuning.")
        if model_args.quantization_bit is not None:
            raise ValueError("Quantized models are not supported with Megatron Bridge.")
        if training_args.deepspeed is not None:
            raise ValueError("Megatron Bridge is incompatible with DeepSpeed.")
        if mb_args is None:
            raise ValueError("Megatron Bridge arguments are missing. Please set USE_MEGATRON_BRIDGE=1.")
        _validate_megatron_bridge_parallel_args(mb_args, training_args.world_size)

View on GitHub (pinned to f28afaf635)

Solutions

  1. Set train_on_prompt: false and mask_history: false (or delete those keys) for non-SFT stages.
  2. Keep such masking options only in stage: sft configs.
  3. Template-level control (e.g. empty-system or template choice) is the right place to adjust prompt content for other stages.

Example fix

# before
stage: dpo
train_on_prompt: true
mask_history: true

# after
stage: dpo
train_on_prompt: false
mask_history: false
Defensive patterns

Strategy: validation

Validate before calling

if cfg.get("stage", "sft") != "sft" and (cfg.get("train_on_prompt") or cfg.get("mask_history")):
    raise SystemExit("train_on_prompt/mask_history are SFT-only flags")

Prevention

When it happens

Trigger: A train config with stage != sft plus train_on_prompt: true or mask_history: true under data_args. Raised by get_train_args() before data processing begins.

Common situations: Porting a chat-SFT YAML (which often sets mask_history: true) to a DPO or RM stage by only editing stage and dataset; enabling train_on_prompt for continued pretraining (pt) where it has no meaning.

Related errors


AI-assisted analysis of hiyouga/LlamaFactory@f28afaf635 (2026-08-14). Data as JSON: /api/errors/795aa4a07d398d3e. Report an issue: GitHub.