Lightning-AI/pytorch-lightning · error · MisconfigurationException
Cannot add arguments from: {lightning_class}. You should pro
Error message
Cannot add arguments from: {lightning_class}. You should provide either a callable or a subclass of: Trainer, LightningModule, LightningDataModule, or Callback. What it means
LightningCLI.add_lightning_class_args (also used internally when building the parser) only accepts callables or subclasses of Trainer, LightningModule, LightningDataModule, or Callback. Passing any other class/instance (e.g. a plain nn.Module, a torch optim class directly, or an arbitrary object) raises this MisconfigurationException.
Source
Thrown at src/lightning/pytorch/cli.py:165
"""
if callable(lightning_class) and not isinstance(lightning_class, type):
lightning_class = class_from_function(lightning_class)
if isinstance(lightning_class, type) and issubclass(
lightning_class, (Trainer, LightningModule, LightningDataModule, Callback)
):
if issubclass(lightning_class, Callback):
self.callback_keys.append(nested_key)
if subclass_mode:
return self.add_subclass_arguments(lightning_class, nested_key, fail_untyped=False, required=required)
return self.add_class_arguments(
lightning_class,
nested_key,
fail_untyped=False,
instantiate=not issubclass(lightning_class, Trainer),
sub_configs=True,
)
raise MisconfigurationException(
f"Cannot add arguments from: {lightning_class}. You should provide either a callable or a subclass of: "
"Trainer, LightningModule, LightningDataModule, or Callback."
)
def add_optimizer_args(
self,
optimizer_class: Union[type[Optimizer], tuple[type[Optimizer], ...]] = (Optimizer,),
nested_key: str = "optimizer",
link_to: str = "AUTOMATIC",
) -> None:
"""Adds arguments from an optimizer class to a nested key of the parser.
Args:
optimizer_class: Any subclass of :class:`torch.optim.Optimizer`. Use tuple to allow subclasses.
nested_key: Name of the nested namespace to store arguments.
link_to: Dot notation of a parser key to set arguments or AUTOMATIC.
"""View on GitHub (pinned to 9fed5c27d2)
Solutions
- Use a supported base: subclass LightningModule/LightningDataModule/Callback/Trainer
- Use the dedicated helpers add_optimizer_args / add_lr_scheduler_args for optimizers and schedulers
- Pass the class (callable), not an instance
Example fix
# before class MyWrapper: ... parser.add_lightning_class_args(MyWrapper, "wrapper") # MisconfigurationException # after class MyWrapper(Callback): ... parser.add_lightning_class_args(MyWrapper, "wrapper")
Defensive patterns
Strategy: type-guard
Validate before calling
from lightning.pytorch import Trainer, LightningModule, LightningDataModule, Callback
def addable(cls) -> bool:
return callable(cls) or (isinstance(cls, type) and issubclass(cls, (Trainer, LightningModule, LightningDataModule, Callback)))
assert addable(MyClass) Type guard
from lightning.pytorch import Trainer, LightningModule, LightningDataModule, Callback
def is_addable_class(cls) -> bool:
"""True if cls can be passed to LightningCLI.add_lightning_class_args."""
return callable(cls) or (isinstance(cls, type) and issubclass(cls, (Trainer, LightningModule, LightningDataModule, Callback))) Prevention
- Use add_optimizer_args/add_lr_scheduler_args for optimizers
- Always pass classes (or callables), never instances
When it happens
Trigger: cli.add_lightning_class_args(torch.optim.Adam, 'optimizer') (not via add_optimizer_args), add_lightning_class_args(SomePlainClass), or add_core_arguments_to_parser encountering an unregistered type.
Common situations: Extending LightningCLI and trying to add arguments for arbitrary classes instead of the dedicated add_optimizer_args / add_lr_scheduler_args helpers; passing an instance instead of a class.
Understand the failure class
Background: Invalid argument type errors: "must be of type string", "expected X, got Y", and ERR_INVALID_ARG_TYPE explained — this error's family across 15 libraries.
Related errors
- Filter should be a dictionary, given {filter!r}
- Expected `fabric.save(filter=...)` for key {k!r} to be a cal
- Only PyTorch DataLoader are currently supported in `setup_da
- you tried to log {v} which is currently not supported. Try a
- Unsupported op {op!r} of type {type(op).__name__}
AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28).
Data as JSON: /api/errors/c1cf56b1d543f9ac.
Report an issue: GitHub.