{"record":{"id":"c57cbf8261dcbec3","repo":"Lightning-AI/pytorch-lightning","slug":"gradient-clip-val-should-be-an-int-or-a-float-g","errorCode":null,"errorMessage":"`gradient_clip_val` should be an int or a float. Got {gradient_clip_val}.","messagePattern":"`gradient_clip_val` should be an int or a float\\. Got (.+?)\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/core/module.py","lineNumber":1284,"sourceCode":"                \" Please use only one of them.\"\n            )\n\n        if gradient_clip_algorithm is None:\n            gradient_clip_algorithm = self.trainer.gradient_clip_algorithm or \"norm\"\n        else:\n            gradient_clip_algorithm = gradient_clip_algorithm.lower()\n            if (\n                self.trainer.gradient_clip_algorithm is not None\n                and self.trainer.gradient_clip_algorithm != gradient_clip_algorithm\n            ):\n                raise MisconfigurationException(\n                    f\"You have set `Trainer(gradient_clip_algorithm={self.trainer.gradient_clip_algorithm.value!r})`\"\n                    f\" and have passed `clip_gradients(gradient_clip_algorithm={gradient_clip_algorithm!r})\"\n                    \" Please use only one of them.\"\n                )\n\n        if not isinstance(gradient_clip_val, (int, float)):\n            raise TypeError(f\"`gradient_clip_val` should be an int or a float. Got {gradient_clip_val}.\")\n\n        if not GradClipAlgorithmType.supported_type(gradient_clip_algorithm.lower()):\n            raise MisconfigurationException(\n                f\"`gradient_clip_algorithm` {gradient_clip_algorithm} is invalid.\"\n                f\" Allowed algorithms: {GradClipAlgorithmType.supported_types()}.\"\n            )\n\n        gradient_clip_algorithm = GradClipAlgorithmType(gradient_clip_algorithm)\n        self.trainer.precision_plugin.clip_gradients(optimizer, gradient_clip_val, gradient_clip_algorithm)\n\n    def configure_gradient_clipping(\n        self,\n        optimizer: Optimizer,\n        gradient_clip_val: Optional[Union[int, float]] = None,\n        gradient_clip_algorithm: Optional[str] = None,\n    ) -> None:\n        \"\"\"Perform gradient clipping for the optimizer parameters. Called before :meth:`optimizer_step`.\n","sourceCodeStart":1266,"sourceCodeEnd":1302,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/core/module.py#L1266-L1302","documentation":"clip_gradients validates that gradient_clip_val is an int or float before use; anything else (string, None after no default, tensor, etc.) raises TypeError. Note the Trainer path defaults a missing value to 0.0, so this fires only for genuinely wrong types passed explicitly.","triggerScenarios":"Calling self.clip_gradients(optimizer, gradient_clip_val='1.0') or with a torch.Tensor/None from a config system that didn't cast.","commonSituations":"Value comes from argparse/hydra as a string ('--clip 1.0'); config parsing produced None or an object type; programmatic schedules pass a tensor.","solutions":["Cast to float: gradient_clip_val=float(cfg.clip)","Validate config types before training (argparse type=float)","Ensure the value is not None when passed explicitly"],"exampleFix":"# before\nself.clip_gradients(optimizer, gradient_clip_val=cfg['clip'])  # cfg['clip'] == '1.0'\n\n# after\nself.clip_gradients(optimizer, gradient_clip_val=float(cfg['clip']))","handlingStrategy":"validation","validationCode":"assert isinstance(gradient_clip_val, (int, float)) and not isinstance(gradient_clip_val, bool), gradient_clip_val\nself.clip_gradients(optimizer, gradient_clip_val=float(gradient_clip_val))","typeGuard":"def valid_clip_val(v) -> bool:\n    return isinstance(v, (int, float)) and not isinstance(v, bool)","tryCatchPattern":null,"preventionTips":["Use argparse type=float / hydra typed configs for clipping values","Cast at config-load time rather than at the training hot path"],"tags":["pytorch-lightning","gradient-clipping","type-error","validation"],"backgroundTag":"gradient-clipping-misconfigured","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}