Lightning-AI/pytorch-lightning · error · TypeError
`gradient_clip_val` should be an int or a float. Got {gradie
Error message
`gradient_clip_val` should be an int or a float. Got {gradient_clip_val}. What it means
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.
Source
Thrown at src/lightning/pytorch/core/module.py:1284
" Please use only one of them."
)
if gradient_clip_algorithm is None:
gradient_clip_algorithm = self.trainer.gradient_clip_algorithm or "norm"
else:
gradient_clip_algorithm = gradient_clip_algorithm.lower()
if (
self.trainer.gradient_clip_algorithm is not None
and self.trainer.gradient_clip_algorithm != gradient_clip_algorithm
):
raise MisconfigurationException(
f"You have set `Trainer(gradient_clip_algorithm={self.trainer.gradient_clip_algorithm.value!r})`"
f" and have passed `clip_gradients(gradient_clip_algorithm={gradient_clip_algorithm!r})"
" Please use only one of them."
)
if not isinstance(gradient_clip_val, (int, float)):
raise TypeError(f"`gradient_clip_val` should be an int or a float. Got {gradient_clip_val}.")
if not GradClipAlgorithmType.supported_type(gradient_clip_algorithm.lower()):
raise MisconfigurationException(
f"`gradient_clip_algorithm` {gradient_clip_algorithm} is invalid."
f" Allowed algorithms: {GradClipAlgorithmType.supported_types()}."
)
gradient_clip_algorithm = GradClipAlgorithmType(gradient_clip_algorithm)
self.trainer.precision_plugin.clip_gradients(optimizer, gradient_clip_val, gradient_clip_algorithm)
def configure_gradient_clipping(
self,
optimizer: Optimizer,
gradient_clip_val: Optional[Union[int, float]] = None,
gradient_clip_algorithm: Optional[str] = None,
) -> None:
"""Perform gradient clipping for the optimizer parameters. Called before :meth:`optimizer_step`.
View on GitHub (pinned to 9fed5c27d2)
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
Example fix
# before self.clip_gradients(optimizer, gradient_clip_val=cfg['clip']) # cfg['clip'] == '1.0' # after self.clip_gradients(optimizer, gradient_clip_val=float(cfg['clip']))
Defensive patterns
Strategy: validation
Validate before calling
assert isinstance(gradient_clip_val, (int, float)) and not isinstance(gradient_clip_val, bool), gradient_clip_val self.clip_gradients(optimizer, gradient_clip_val=float(gradient_clip_val))
Type guard
def valid_clip_val(v) -> bool:
return isinstance(v, (int, float)) and not isinstance(v, bool) Prevention
- Use argparse type=float / hydra typed configs for clipping values
- Cast at config-load time rather than at the training hot path
When it happens
Trigger: Calling self.clip_gradients(optimizer, gradient_clip_val='1.0') or with a torch.Tensor/None from a config system that didn't cast.
Common situations: Value comes from argparse/hydra as a string ('--clip 1.0'); config parsing produced None or an object type; programmatic schedules pass a tensor.
Related errors
- f"`gradient_clip_val` should be an int or a float. Got {grad
- Device should be CUDA, got {device} instead.
- You requested to find {num_devices} devices but there are no
- `setup_optimizers` requires at least one optimizer as input.
- `setup_dataloaders` requires at least one dataloader as inpu
AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28).
Data as JSON: /api/errors/c57cbf8261dcbec3.
Report an issue: GitHub.