Lightning-AI/pytorch-lightning · error · ValueError

Unsupported config type of {type(hp)}.

Error message

Unsupported config type of {type(hp)}.

What it means

_to_hparams_dict rejects config objects whose type is neither a dict/MutableMapping, argparse.Namespace, nor one of _ALLOWED_CONFIG_TYPES. The hparams mechanism serializes key-value structures, so arbitrary Python objects cannot be stored as hparams.

Source

Thrown at src/lightning/pytorch/core/mixins/hparams_mixin.py:163

        by base classes.

        Args:
            ignore_list: Names of hyperparameters to remove.

        """
        for key in ignore_list:
            self._hparams.pop(key, None)

    @staticmethod
    def _to_hparams_dict(hp: Union[MutableMapping, Namespace, str]) -> Union[MutableMapping, AttributeDict]:
        if isinstance(hp, Namespace):
            hp = vars(hp)
        if isinstance(hp, dict):
            hp = AttributeDict(hp)
        elif isinstance(hp, _PRIMITIVE_TYPES):
            raise ValueError(f"Primitives {_PRIMITIVE_TYPES} are not allowed.")
        elif not isinstance(hp, _ALLOWED_CONFIG_TYPES):
            raise ValueError(f"Unsupported config type of {type(hp)}.")
        return hp

    @property
    def hparams(self) -> Union[AttributeDict, MutableMapping]:
        """The collection of hyperparameters saved with :meth:`save_hyperparameters`. It is mutable by the user. For
        the frozen set of initial hyperparameters, use :attr:`hparams_initial`.

        Returns:
            Mutable hyperparameters dictionary

        """
        if not hasattr(self, "_hparams"):
            self._hparams = AttributeDict()
        return self._hparams

    @property
    def hparams_initial(self) -> AttributeDict:
        """The collection of hyperparameters saved with :meth:`save_hyperparameters`. These contents are read-only.

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Convert the object to a plain dict first (e.g. dict(config), OmegaConf.to_container(cfg))
  2. Use keyword arguments: save_hyperparameters(lr=1e-3, batch_size=32)
  3. For argparse, pass the Namespace directly

Example fix

# before
self.save_hyperparameters(my_custom_config_obj)

# after
self.save_hyperparameters(dict(my_custom_config_obj))
Defensive patterns

Strategy: type-guard

Validate before calling

if not isinstance(hp, (dict, MutableMapping, Namespace)):
    hp = dict(hp) if hasattr(hp, 'keys') else {'value': hp}

Type guard

def is_supported_config(obj) -> bool:
    return isinstance(obj, (dict, MutableMapping, Namespace)) or type(obj).__name__ in ('DictConfig', 'ListConfig', 'AttributeDict')

Try / catch

try:
    self.save_hyperparameters(cfg)
except ValueError as e:
    if 'Unsupported config type' in str(e):
        from omegaconf import OmegaConf
        self.save_hyperparameters(OmegaConf.to_container(cfg))
    else:
        raise

Prevention

When it happens

Trigger: Calling save_hyperparameters or _set_hparams with e.g. a custom class instance, a list, or a tuple that is not in _ALLOWED_CONFIG_TYPES.

Common situations: User passed a hydra/omegaconf object of an unsupported version, a pathlib.Path, a list of args, or a custom ConfigClass to save_hyperparameters.

Related errors


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