hiyouga/LlamaFactory · error · ValueError
Please specify `export_dir` to save model.
Error message
Please specify `export_dir` to save model.
What it means
`export_model` (src/llamafactory/train/tuner.py:180) requires a destination; if `model_args.export_dir` is None it refuses to proceed because model.save_pretrained would have nowhere to write. export_dir is the `export_dir:` key of the export YAML.
Source
Thrown at src/llamafactory/train/tuner.py:180
def run_exp(args: Optional[dict[str, Any]] = None, callbacks: Optional[list["TrainerCallback"]] = None) -> None:
args = read_args(args)
if "-h" in args or "--help" in args:
get_train_args(args)
ray_args = get_ray_args(args)
callbacks = callbacks or []
if ray_args.use_ray:
_ray_training_function(ray_args, config={"args": args, "callbacks": callbacks})
else:
_training_function(config={"args": args, "callbacks": callbacks})
def export_model(args: Optional[dict[str, Any]] = None) -> None:
model_args, data_args, finetuning_args, _ = get_infer_args(args)
if model_args.export_dir is None:
raise ValueError("Please specify `export_dir` to save model.")
if model_args.adapter_name_or_path is not None and model_args.export_quantization_bit is not None:
raise ValueError("Please merge adapters before quantizing the model.")
tokenizer_module = load_tokenizer(model_args)
tokenizer = tokenizer_module["tokenizer"]
processor = tokenizer_module["processor"]
template = get_template_and_fix_tokenizer(tokenizer, data_args)
model = load_model(tokenizer, model_args, finetuning_args) # must after fixing tokenizer to resize vocab
if getattr(model, "quantization_method", None) is not None and model_args.adapter_name_or_path is not None:
raise ValueError("Cannot merge adapters to a quantized model.")
if not isinstance(model, PreTrainedModel):
raise ValueError("The model is not a `PreTrainedModel`, export aborted.")
if getattr(model, "quantization_method", None) is not None: # quantized model adopts float16 type
setattr(model.config, "torch_dtype", torch.float16)View on GitHub (pinned to f28afaf635)
Solutions
- Add `export_dir: /path/to/output` to the export YAML.
- If calling programmatically, pass {'export_dir': ...} in the args dict.
- Ensure the directory is writable and has space for the sharded weights.
Example fix
# before (yaml) model_name_or_path: meta-llama/Llama-3-8b adapter_name_or_path: output/lora # after model_name_or_path: meta-llama/Llama-3-8b adapter_name_or_path: output/lora export_dir: output/merged-model
Defensive patterns
Strategy: validation
Validate before calling
assert export_dir, "export config must set export_dir"
Prevention
- Treat export_dir as a required field in export config templates.
- Fail fast in wrappers by checking the key before calling export_model.
When it happens
Trigger: Running `llamafactory-cli export` with a YAML missing the `export_dir:` line, or calling export_model(args) with a dict that omits it.
Common situations: Quickly built export configs from a train config where export_dir was never added; automation scripts that template export configs.
Related errors
- Please merge adapters before quantizing the model.
- Cannot merge adapters to a quantized model.
- Please specify peft_config to merge and export model.
- Please upgrade `transformers` to 4.34.0
- Unable to process key {key}
AI-assisted analysis of hiyouga/LlamaFactory@f28afaf635 (2026-08-14).
Data as JSON: /api/errors/fdee1f5949b38ebc.
Report an issue: GitHub.