{"record":{"id":"eeeb5e92357a16f3","repo":"Lightning-AI/pytorch-lightning","slug":"f-gradient-clip-val-should-be-an-int-or-a-float","errorCode":null,"errorMessage":"f\"`gradient_clip_val` should be an int or a float. Got {gradient_clip_val}.\"","messagePattern":"f\"`gradient_clip_val` should be an int or a float\\. Got (.+?)\\.\"","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/trainer/trainer.py","lineNumber":472,"sourceCode":"            callbacks,\n            enable_checkpointing,\n            enable_progress_bar,\n            default_root_dir,\n            enable_model_summary,\n            max_time,\n        )\n\n        # init data flags\n        self.check_val_every_n_epoch: Optional[int]\n        self._data_connector.on_trainer_init(\n            val_check_interval,\n            reload_dataloaders_every_n_epochs,\n            check_val_every_n_epoch,\n        )\n\n        # gradient clipping\n        if gradient_clip_val is not None and 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 gradient_clip_algorithm is not None and not GradClipAlgorithmType.supported_type(\n            gradient_clip_algorithm.lower()\n        ):\n            raise MisconfigurationException(\n                f\"`gradient_clip_algorithm` {gradient_clip_algorithm} is invalid. \"\n                f\"Allowed algorithms: {GradClipAlgorithmType.supported_types()}.\"\n            )\n\n        self.gradient_clip_val: Optional[Union[int, float]] = gradient_clip_val\n        self.gradient_clip_algorithm: Optional[GradClipAlgorithmType] = (\n            GradClipAlgorithmType(gradient_clip_algorithm.lower()) if gradient_clip_algorithm is not None else None\n        )\n\n        if detect_anomaly:\n            rank_zero_info(\n                \"You have turned on `Trainer(detect_anomaly=True)`. This will significantly slow down compute speed and\"\n                \" is recommended only for model debugging.\"","sourceCodeStart":454,"sourceCodeEnd":490,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/trainer/trainer.py#L454-L490","documentation":"Trainer's gradient_clip_val argument must be an int or float (or None to disable clipping). A non-numeric value such as a string was passed, so the constructor raises a TypeError before training starts.","triggerScenarios":"Trainer(gradient_clip_val=\"0.5\"), gradient_clip_val=[0.5], or any non-int/float value other than None; often comes from CLI/config parsing where numbers stay strings.","commonSituations":"Reading hyperparameters from YAML/JSON/argparse without casting to float, or passing a numpy string / tensor / list.","solutions":["Cast to float when loading from configs: gradient_clip_val=float(cfg.gradient_clip_val)","Pass a plain Python int/float literal","Pass None to disable clipping"],"exampleFix":"# before\ntrainer = Trainer(gradient_clip_val=cfg[\"gradient_clip_val\"])  # \"0.5\" string\n# after\ntrainer = Trainer(gradient_clip_val=float(cfg[\"gradient_clip_val\"]))","handlingStrategy":"type-guard","validationCode":"gcv = cfg.get(\"gradient_clip_val\")\nif gcv is not None and not isinstance(gcv, (int, float)) or isinstance(gcv, bool):\n    gcv = float(gcv)\ntrainer = Trainer(gradient_clip_val=gcv)","typeGuard":"def is_valid_clip_val(v) -> bool:\n    return v is None or (isinstance(v, (int, float)) and not isinstance(v, bool))","tryCatchPattern":null,"preventionTips":["Cast numeric hyperparameters from YAML/CLI to float at load time","Type your config with pydantic or dataclasses so strings fail early"],"tags":["trainer","gradient-clipping","type-error","hyperparameters","pytorch-lightning"],"backgroundTag":"type-mismatch-argument","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}