Lightning-AI/pytorch-lightning · warning

Skipping '{k}' parameter because it is not possible to safel

Error message

Skipping '{k}' parameter because it is not possible to safely dump to YAML.

What it means

When save_hparams_to_yaml dumps hyperparameters, any value that yaml.dump cannot serialize (raises TypeError/ValueError, e.g. fsspec paths, arbitrary objects) is skipped and replaced by its type name, with this warning.

Source

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

        hparams = apply_to_collection(hparams, DictConfig, OmegaConf.to_container, resolve=True)
        with fs.open(config_yaml, "w", encoding="utf-8") as fp:
            try:
                OmegaConf.save(hparams, fp)
                return
            except (UnsupportedValueType, ValidationError):
                pass

    if not isinstance(hparams, dict):
        raise TypeError("hparams must be dictionary")

    hparams_allowed = {}
    # drop parameters which contain some strange datatypes as fsspec
    for k, v in hparams.items():
        try:
            v = v.name if isinstance(v, Enum) else v
            yaml.dump(v)
        except (TypeError, ValueError):
            warn(f"Skipping '{k}' parameter because it is not possible to safely dump to YAML.")
            hparams[k] = type(v).__name__
        else:
            hparams_allowed[k] = v

    # saving the standard way
    with fs.open(config_yaml, "w", newline="") as fp:
        yaml.dump(hparams_allowed, fp)


def convert(val: str) -> Union[int, float, bool, str]:
    try:
        return ast.literal_eval(val)
    except (ValueError, SyntaxError) as err:
        log.debug(err)
        return val

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Exclude non-serializable args: save_hyperparameters(ignore=['sampler'])
  2. Convert to primitives in __init__ before saving (str(path), dict(config))
  3. Accept the warning — the value is replaced by its type name in YAML only

Example fix

# before
def __init__(self, data_path: AbstractFileSystem, lr=1e-3):
    super().__init__()
    self.save_hyperparameters()  # data_path not YAML-safe
# after
def __init__(self, data_path: str, lr=1e-3):
    super().__init__()
    self.save_hyperparameters()
Defensive patterns

Strategy: type-guard

Validate before calling

import yaml
bad = [k for k, v in hparams.items() if _fails(yaml.safe_dump, v)]
assert not bad, f'non-serializable hparams: {bad}'

Type guard

def is_yaml_safe(v) -> bool:
    import yaml
    try:
        yaml.safe_dump(v)
        return True
    except (TypeError, ValueError):
        return False

Prevention

When it happens

Trigger: save_hyperparameters capturing non-primitive args (Path-likes from fsspec, custom classes, lambdas) and then checkpoint saving or log_hyperparams writing the YAML config.

Common situations: Passing open_file objects, distributed samplers, or model instances as hparams; environment-dependent objects stored in __init__ signature.

Related errors


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