{"record":{"id":"33a2050b2631c679","repo":"Lightning-AI/pytorch-lightning","slug":"training-with-multiple-optimizers-is-only-supporte","errorCode":null,"errorMessage":"Training with multiple optimizers is only supported with manual optimization. Remove the `optimizer_idx` argument from `training_step`, set `self.automatic_optimization = False` and access your optimizers in `training_step` with `opt1, opt2, ... = self.optimizers()`.","messagePattern":"Training with multiple optimizers is only supported with manual optimization\\. Remove the `optimizer_idx` argument from `training_step`, set `self\\.automatic_optimization = False` and access your optimizers in `training_step` with `opt1, opt2, \\.\\.\\. = self\\.optimizers\\(\\)`\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"critical","filePath":"src/lightning/pytorch/core/optimizer.py","lineNumber":353,"sourceCode":"                f\"The provided lr scheduler `{scheduler.__class__.__name__}` is invalid.\"\n                \" It should have `state_dict` and `load_state_dict` methods defined.\"\n            )\n\n        if (\n            not isinstance(scheduler, LRSchedulerTypeTuple)\n            and not is_overridden(\"lr_scheduler_step\", model)\n            and model.automatic_optimization\n        ):\n            raise MisconfigurationException(\n                f\"The provided lr scheduler `{scheduler.__class__.__name__}` doesn't follow PyTorch's LRScheduler\"\n                \" API. You should override the `LightningModule.lr_scheduler_step` hook with your own logic if\"\n                \" you are using a custom LR scheduler.\"\n            )\n\n\ndef _validate_multiple_optimizers_support(optimizers: list[Optimizer], model: \"pl.LightningModule\") -> None:\n    if is_param_in_hook_signature(model.training_step, \"optimizer_idx\", explicit=True):\n        raise RuntimeError(\n            \"Training with multiple optimizers is only supported with manual optimization. Remove the `optimizer_idx`\"\n            \" argument from `training_step`, set `self.automatic_optimization = False` and access your optimizers\"\n            \" in `training_step` with `opt1, opt2, ... = self.optimizers()`.\"\n        )\n    if model.automatic_optimization and len(optimizers) > 1:\n        raise RuntimeError(\n            \"Training with multiple optimizers is only supported with manual optimization. Set\"\n            \" `self.automatic_optimization = False`, then access your optimizers in `training_step` with\"\n            \" `opt1, opt2, ... = self.optimizers()`.\"\n        )\n\n\ndef _validate_optimizers_attached(optimizers: list[Optimizer], lr_scheduler_configs: list[LRSchedulerConfig]) -> None:\n    for config in lr_scheduler_configs:\n        if config.scheduler.optimizer not in optimizers:\n            raise MisconfigurationException(\n                \"Some schedulers are attached with an optimizer that wasn't returned from `configure_optimizers`.\"\n            )","sourceCodeStart":335,"sourceCodeEnd":371,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/core/optimizer.py#L335-L371","documentation":"The `optimizer_idx` argument in training_step belonged to the removed automatic multi-optimizer API. If Lightning detects it in the training_step signature it raises RuntimeError instructing you to move to manual optimization and fetch optimizers via self.optimizers().","triggerScenarios":"Defining def training_step(self, batch, batch_idx, optimizer_idx) in a LightningModule, regardless of optimizer count.","commonSituations":"Upgrading code from Lightning 1.x to 2.x where optimizer_idx in automatic optimization was removed.","solutions":["Remove the optimizer_idx parameter from training_step","Set self.automatic_optimization = False in __init__","Access optimizers in training_step via opt1, opt2 = self.optimizers() and call opt.zero_grad(); loss.backward(); opt.step() manually"],"exampleFix":"# before\ndef training_step(self, batch, batch_idx, optimizer_idx):\n    ...\n# after\ndef __init__(self):\n    self.automatic_optimization = False\n\ndef training_step(self, batch, batch_idx):\n    opt1, opt2 = self.optimizers()\n    ...","handlingStrategy":"validation","validationCode":"import inspect\nsig = inspect.signature(model.training_step)\nassert \"optimizer_idx\" not in sig.parameters, \"optimizer_idx removed in Lightning 2.x\"","typeGuard":"def has_optimizer_idx(module) -> bool:\n    import inspect\n    return \"optimizer_idx\" in inspect.signature(module.training_step).parameters","tryCatchPattern":null,"preventionTips":["Run Lightning 2.x migration checks before upgrading","Use self.optimizers() unpacking instead of optimizer_idx"],"tags":["optimizer","manual-optimization","migration","lightning"],"backgroundTag":"removed-api-parameter","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}