{"record":{"id":"3423938c7b84e153","repo":"Lightning-AI/pytorch-lightning","slug":"self-class-name-add-configure-optimizer","errorCode":null,"errorMessage":"`{self.__class__.__name__}.add_configure_optimizers_method_to_model` expects at most one optimizer and one lr_scheduler to be 'AUTOMATIC', but found {optimizers + lr_schedulers}. In this case the user is expected to link the argument groups and implement `configure_optimizers`, see https://lightning.ai/docs/pytorch/stable/common/lightning_cli.html#optimizers-and-learning-rate-schedulers","messagePattern":"`(.+?)\\.add_configure_optimizers_method_to_model` expects at most one optimizer and one lr_scheduler to be 'AUTOMATIC', but found (.+?)\\. In this case the user is expected to link the argument groups and implement `configure_optimizers`, see https://lightning\\.ai/docs/pytorch/stable/common/lightning_cli\\.html#optimizers-and-learning-rate-schedulers","errorType":"exception","errorClass":"MisconfigurationException","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/cli.py","lineNumber":710,"sourceCode":"        def get_automatic(\n            class_type: Union[type, tuple[type, ...]], register: dict[str, tuple[Union[type, tuple[type, ...]], str]]\n        ) -> list[str]:\n            automatic = []\n            for key, (base_class, link_to) in register.items():\n                if not isinstance(base_class, tuple):\n                    base_class = (base_class,)\n                if link_to == \"AUTOMATIC\" and any(issubclass(c, class_type) for c in base_class):\n                    automatic.append(key)\n            return automatic\n\n        optimizers = get_automatic(Optimizer, parser._optimizers)\n        lr_schedulers = get_automatic(LRSchedulerTypeTuple, parser._lr_schedulers)\n\n        if len(optimizers) == 0:\n            return\n\n        if len(optimizers) > 1 or len(lr_schedulers) > 1:\n            raise MisconfigurationException(\n                f\"`{self.__class__.__name__}.add_configure_optimizers_method_to_model` expects at most one optimizer \"\n                f\"and one lr_scheduler to be 'AUTOMATIC', but found {optimizers + lr_schedulers}. In this case the \"\n                \"user is expected to link the argument groups and implement `configure_optimizers`, see \"\n                \"https://lightning.ai/docs/pytorch/stable/common/lightning_cli.html\"\n                \"#optimizers-and-learning-rate-schedulers\"\n            )\n\n        optimizer_class = parser._optimizers[optimizers[0]][0]\n        optimizer_init = self._get(self.config_init, optimizers[0])\n        if not isinstance(optimizer_class, tuple):\n            optimizer_init = _global_add_class_path(optimizer_class, optimizer_init)\n        if not optimizer_init:\n            # optimizers were registered automatically but not passed by the user\n            return\n\n        lr_scheduler_init = None\n        if lr_schedulers:\n            lr_scheduler_class = parser._lr_schedulers[lr_schedulers[0]][0]","sourceCodeStart":692,"sourceCodeEnd":728,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/cli.py#L692-L728","documentation":"LightningCLI can auto-implement configure_optimizers only when at most one optimizer and one lr_scheduler argument group is left as AUTOMATIC. During instantiate_classes -> add_configure_optimizers_method_to_model, if both counts exceed the limit (their combined list is non-empty beyond the single allowance) it raises this MisconfigurationException telling the user to link groups and implement configure_optimizers manually.","triggerScenarios":"CLI config with two optimizers (e.g. --optimizer.class=torch.optim.Adam plus another linked optimizer group, or optimizer + optimizer.lr_scheduler combos) all left AUTOMATIC so len(optimizers)+len(lr_schedulers) > allowed.","commonSituations":"Multi-optimizer GAN-style models configured entirely via CLI; users adding --lr_scheduler alongside two optimizer groups expecting LightningCLI to wire them automatically.","solutions":["Implement configure_optimizers manually on the LightningModule and link groups in the config (use --optimizer.link_a.b etc. per the linked docs)","Reduce to one optimizer + one scheduler so AUTOMATIC mode can generate configure_optimizers","Pass subclass_args so optimizer groups aren't all AUTOMATIC"],"exampleFix":"# before\n# cli command with two automatic optimizers -> MisconfigurationException\n# lightning run train --optimizer=Adam --optimizer.lr_scheduler=CosineAnnealingLR --optimizer2=SGD ...\n# after\n# in model:\ndef configure_optimizers(self):\n    opt1 = torch.optim.Adam(self.enc.parameters())\n    opt2 = torch.optim.SGD(self.dec.parameters(), lr=0.01)\n    return [opt1, opt2], []\n# and configure cli with linked/manual groups instead of AUTOMATIC","handlingStrategy":"validation","validationCode":"def configure_optimizers(self):\n    # manual wiring when using multiple optimizers via CLI\n    enc_opt = self.hparams.optimizer_enc(self.parameters('enc'))\n    dec_opt = self.hparams.optimizer_dec(self.parameters('dec'))\n    return [enc_opt, dec_opt], []","typeGuard":null,"tryCatchPattern":null,"preventionTips":["For multi-optimizer models, implement configure_optimizers yourself and link CLI groups","Keep AUTOMATIC mode to one optimizer + one scheduler"],"tags":["lightning-cli","configure-optimizers","multiple-optimizers","automatic-mode"],"backgroundTag":"cli-optimizer-autoconfig-limit","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}