Lightning-AI/pytorch-lightning · error · RuntimeError

Missing folder: {os.path.dirname(config_yaml)}.

Error message

Missing folder: {os.path.dirname(config_yaml)}.

What it means

Same guard as the CSV writer but for save_hparams_to_yaml: the parent directory of config_yaml must exist on the fsspec filesystem or a RuntimeError is raised. Called from LightningModule.save and logger hyperparameter logging, so it frequently surfaces indirectly.

Source

Thrown at src/lightning/pytorch/core/saving.py:343

        with contextlib.suppress(UnsupportedValueType, ValidationError):
            # OmegaConf containers are mapping-like but not `dict` subclasses
            return cast("dict[str, Any]", OmegaConf.create(hparams))
    return hparams


def save_hparams_to_yaml(config_yaml: _PATH, hparams: Union[dict, Namespace], use_omegaconf: bool = True) -> None:
    """
    Args:
        config_yaml: path to new YAML file
        hparams: parameters to be saved
        use_omegaconf: If omegaconf is available and ``use_omegaconf=True``,
            the hparams will be converted to ``DictConfig`` if possible.

    """
    fs = get_filesystem(config_yaml)
    if not _is_dir(fs, os.path.dirname(config_yaml)):
        raise RuntimeError(f"Missing folder: {os.path.dirname(config_yaml)}.")

    # convert Namespace or AD to dict
    if isinstance(hparams, Namespace):
        hparams = vars(hparams)
    elif isinstance(hparams, AttributeDict):
        hparams = dict(hparams)

    # saving with OmegaConf objects
    if _OMEGACONF_AVAILABLE and use_omegaconf:
        from omegaconf import OmegaConf
        from omegaconf.dictconfig import DictConfig
        from omegaconf.errors import UnsupportedValueType, ValidationError

        # deepcopy: hparams from user shouldn't be resolved
        hparams = deepcopy(hparams)
        hparams = apply_to_collection(hparams, DictConfig, OmegaConf.to_container, resolve=True)
        with fs.open(config_yaml, "w", encoding="utf-8") as fp:
            try:

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Create the parent directory before saving: Path(config_yaml).parent.mkdir(parents=True, exist_ok=True)
  2. Verify save_dir / logger save path is correct and accessible
  3. For remote URIs, check credentials and that the bucket/prefix exists

Example fix

// before
save_hparams_to_yaml("runs/abc/hparams.yaml", hparams)  # runs/abc missing
// after
from pathlib import Path
Path("runs/abc").mkdir(parents=True, exist_ok=True)
save_hparams_to_yaml("runs/abc/hparams.yaml", hparams)
Defensive patterns

Strategy: validation

Validate before calling

from pathlib import Path
Path(config_yaml).parent.mkdir(parents=True, exist_ok=True)

Prevention

When it happens

Trigger: trainer.logger.log_hyperparams / model.save writing a hparams.yaml into a folder that was never created; direct calls to save_hparams_to_yaml with a bad parent path.

Common situations: Custom save dirs (save_dir passed to loggers), fsspec remote targets, or code that assumed Lightning creates the folder (it does in most flows — the error usually means a custom path bypassed that).

Related errors


AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28). Data as JSON: /api/errors/cf8d9155569b1913. Report an issue: GitHub.