{"record":{"id":"b69fe9f86d984696","repo":"Lightning-AI/pytorch-lightning","slug":"primitives-primitive-types-are-not-allowed","errorCode":null,"errorMessage":"Primitives {_PRIMITIVE_TYPES} are not allowed.","messagePattern":"Primitives (.+?) are not allowed\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/core/mixins/hparams_mixin.py","lineNumber":161,"sourceCode":"\n        This allows derived classes to drop hyperparameters previously saved\n        by base classes.\n\n        Args:\n            ignore_list: Names of hyperparameters to remove.\n\n        \"\"\"\n        for key in ignore_list:\n            self._hparams.pop(key, None)\n\n    @staticmethod\n    def _to_hparams_dict(hp: Union[MutableMapping, Namespace, str]) -> Union[MutableMapping, AttributeDict]:\n        if isinstance(hp, Namespace):\n            hp = vars(hp)\n        if isinstance(hp, dict):\n            hp = AttributeDict(hp)\n        elif isinstance(hp, _PRIMITIVE_TYPES):\n            raise ValueError(f\"Primitives {_PRIMITIVE_TYPES} are not allowed.\")\n        elif not isinstance(hp, _ALLOWED_CONFIG_TYPES):\n            raise ValueError(f\"Unsupported config type of {type(hp)}.\")\n        return hp\n\n    @property\n    def hparams(self) -> Union[AttributeDict, MutableMapping]:\n        \"\"\"The collection of hyperparameters saved with :meth:`save_hyperparameters`. It is mutable by the user. For\n        the frozen set of initial hyperparameters, use :attr:`hparams_initial`.\n\n        Returns:\n            Mutable hyperparameters dictionary\n\n        \"\"\"\n        if not hasattr(self, \"_hparams\"):\n            self._hparams = AttributeDict()\n        return self._hparams\n\n    @property","sourceCodeStart":143,"sourceCodeEnd":179,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/core/mixins/hparams_mixin.py#L143-L179","documentation":"_to_hparams_dict (used by save_hyperparameters/_set_hparams) only accepts mappings, argparse.Namespace, and a few structured config types. Python primitives (str, int, float, bool, etc. in _PRIMITIVE_TYPES) are rejected because hparams must be a key-value structure, not a single scalar.","triggerScenarios":"Calling model.save_hyperparameters(\"some_string\") or save_hyperparameters(42); passing a bare primitive to LightningModule(save_hyperparameters=...) or _set_hparams.","commonSituations":"User tried save_hyperparameters('resnet18') intending to store a model name scalar; passed a single value instead of a dict from a config loader.","solutions":["Wrap the value in a dict: save_hyperparameters({'model_name': 'resnet18'})","Pass keyword arguments: save_hyperparameters(model_name='resnet18')","Use an argparse.Namespace or AttributeDict for structured configs"],"exampleFix":"# before\nself.save_hyperparameters('resnet18')\n\n# after\nself.save_hyperparameters({'arch': 'resnet18'})","handlingStrategy":"type-guard","validationCode":"from argparse import Namespace\nif not isinstance(hp, (dict, Namespace)) and isinstance(hp, (str, int, float, bool)):\n    hp = {'value': hp}  # wrap primitives before save_hyperparameters","typeGuard":"def is_valid_hparams(obj) -> bool:\n    return isinstance(obj, (dict, MutableMapping, Namespace)) or type(obj).__name__ in _ALLOWED_CONFIG_TYPE_NAMES","tryCatchPattern":"try:\n    self.save_hyperparameters(hp)\nexcept ValueError as e:\n    if 'Primitives' in str(e):\n        self.save_hyperparameters({'value': hp})\n    else:\n        raise","preventionTips":["Always pass dicts or kwargs to save_hyperparameters","Lint calls like save_hyperparameters('literal_string') in code review"],"tags":["pytorch-lightning","hparams","save-hyperparameters","type-validation"],"backgroundTag":"hyperparameter-config-invalid","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}