Lightning-AI/pytorch-lightning · error · TypeError

f"`gradient_clip_val` should be an int or a float. Got {grad

Error message

f"`gradient_clip_val` should be an int or a float. Got {gradient_clip_val}."

What it means

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.

Source

Thrown at src/lightning/pytorch/trainer/trainer.py:472

            callbacks,
            enable_checkpointing,
            enable_progress_bar,
            default_root_dir,
            enable_model_summary,
            max_time,
        )

        # init data flags
        self.check_val_every_n_epoch: Optional[int]
        self._data_connector.on_trainer_init(
            val_check_interval,
            reload_dataloaders_every_n_epochs,
            check_val_every_n_epoch,
        )

        # gradient clipping
        if gradient_clip_val is not None and 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 gradient_clip_algorithm is not None and 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()}."
            )

        self.gradient_clip_val: Optional[Union[int, float]] = gradient_clip_val
        self.gradient_clip_algorithm: Optional[GradClipAlgorithmType] = (
            GradClipAlgorithmType(gradient_clip_algorithm.lower()) if gradient_clip_algorithm is not None else None
        )

        if detect_anomaly:
            rank_zero_info(
                "You have turned on `Trainer(detect_anomaly=True)`. This will significantly slow down compute speed and"
                " is recommended only for model debugging."

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Cast to float when loading from configs: gradient_clip_val=float(cfg.gradient_clip_val)
  2. Pass a plain Python int/float literal
  3. Pass None to disable clipping

Example fix

# before
trainer = Trainer(gradient_clip_val=cfg["gradient_clip_val"])  # "0.5" string
# after
trainer = Trainer(gradient_clip_val=float(cfg["gradient_clip_val"]))
Defensive patterns

Strategy: type-guard

Validate before calling

gcv = cfg.get("gradient_clip_val")
if gcv is not None and not isinstance(gcv, (int, float)) or isinstance(gcv, bool):
    gcv = float(gcv)
trainer = Trainer(gradient_clip_val=gcv)

Type guard

def is_valid_clip_val(v) -> bool:
    return v is None or (isinstance(v, (int, float)) and not isinstance(v, bool))

Prevention

When it happens

Trigger: 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.

Common situations: Reading hyperparameters from YAML/JSON/argparse without casting to float, or passing a numpy string / tensor / list.

Related errors


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