{"record":{"id":"444473a6cb4a065d","repo":"hiyouga/LlamaFactory","slug":"output-directory-already-exists-and-is-not-empty","errorCode":null,"errorMessage":"Output directory already exists and is not empty. Please set `overwrite_output_dir`.","messagePattern":"Output directory already exists and is not empty\\. Please set `overwrite_output_dir`\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/llamafactory/hparams/parser.py","lineNumber":623,"sourceCode":"        can_resume_from_checkpoint = False\n        if training_args.resume_from_checkpoint is not None:\n            logger.warning_rank0(\"Cannot resume from checkpoint in current stage.\")\n            training_args.resume_from_checkpoint = None\n    else:\n        can_resume_from_checkpoint = True\n\n    if (\n        training_args.resume_from_checkpoint is None\n        and training_args.do_train\n        and os.path.isdir(training_args.output_dir)\n        and not getattr(training_args, \"overwrite_output_dir\", False)  # for mca training args and transformers >= 5.0\n        and can_resume_from_checkpoint\n    ):\n        last_checkpoint = get_last_checkpoint(training_args.output_dir)\n        if last_checkpoint is None and any(\n            os.path.isfile(os.path.join(training_args.output_dir, name)) for name in CHECKPOINT_NAMES\n        ):\n            raise ValueError(\"Output directory already exists and is not empty. Please set `overwrite_output_dir`.\")\n\n        if last_checkpoint is not None:\n            training_args.resume_from_checkpoint = last_checkpoint\n            logger.info_rank0(f\"Resuming training from {training_args.resume_from_checkpoint}.\")\n            logger.info_rank0(\"Change `output_dir` or use `overwrite_output_dir` to avoid.\")\n\n    if (\n        finetuning_args.stage in [\"rm\", \"ppo\"]\n        and finetuning_args.finetuning_type == \"lora\"\n        and training_args.resume_from_checkpoint is not None\n    ):\n        logger.warning_rank0(\n            f\"Add {training_args.resume_from_checkpoint} to `adapter_name_or_path` to resume training from checkpoint.\"\n        )\n\n    # Post-process model arguments\n    if training_args.bf16 or finetuning_args.pure_bf16:\n        model_args.compute_dtype = torch.bfloat16","sourceCodeStart":605,"sourceCodeEnd":641,"githubUrl":"https://github.com/hiyouga/LlamaFactory/blob/f28afaf6355af515454dfb16c97d728307c93897/src/llamafactory/hparams/parser.py#L605-L641","documentation":"Raised in get_train_args when do_train is true, the output_dir already exists, resume_from_checkpoint is unset, overwrite_output_dir is false, and no resumable checkpoint (no file named in CHECKPOINT_NAMES) can be found in it. LlamaFactory refuses to silently mix a new run's artifacts with leftovers from a previous run in the same directory.","triggerScenarios":"Running llamafactory-cli train twice with the same output_dir after the first run crashed before writing a checkpoint, or pointing output_dir at a non-empty directory that contains stray files (e.g. a tokenizer export or a log) but no trainer checkpoint.","commonSituations":"Re-running a failed experiment into the same folder; resuming after an OOM kill that happened before the first checkpoint was saved; reusing a directory that was partially cleaned with rm but still holds files.","solutions":["Set overwrite_output_dir: true in the training section of the YAML config.","Point output_dir to a fresh, empty directory for the new run.","Delete or move the stale files in the existing output_dir (only if you no longer need them).","If you meant to resume, ensure the directory actually contains a valid checkpoint (e.g. checkpoint-500 with trainer_state.json); otherwise the run cannot auto-resume."],"exampleFix":"# before\noutput_dir: saves/llama3-lora\n# -> ValueError if saves/llama3-lora has leftover files\n\n# after\noutput_dir: saves/llama3-lora\noverwrite_output_dir: true","handlingStrategy":"validation","validationCode":"import os\nfrom transformers.trainer_utils import get_last_checkpoint\n\nckpt = get_last_checkpoint(output_dir) if os.path.isdir(output_dir) else None\nif ckpt is None and os.path.isdir(output_dir) and os.listdir(output_dir):\n    # decide explicitly: wipe or fail\n    assert overwrite_output_dir, f\"{output_dir} non-empty and not resumable; set overwrite_output_dir or clean it\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Template output_dir with a run id (e.g. output_dir: saves/{model}/{timestamp}) so directories are never reused accidentally.","Clean or archive output_dir in your run script before launching a fresh experiment."],"tags":["output-dir","checkpoint","resume","training-config"],"backgroundTag":null,"analyzedSha":"f28afaf6355af515454dfb16c97d728307c93897","analyzedAt":"2026-08-14T21:57:28.298Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}