{"record":{"id":"1d63f1070e644b43","repo":"Lightning-AI/pytorch-lightning","slug":"found-more-than-one-stateful-callback-of-type-ty","errorCode":null,"errorMessage":"Found more than one stateful callback of type `{type(callback).__name__}`. In the current configuration, this callback does not support being saved alongside other instances of the same type. Please consult the documentation of `{type(callback).__name__}` regarding valid settings for the callback state to be checkpointable. HINT: The `callback.state_key` must be unique among all callbacks in the Trainer.","messagePattern":"Found more than one stateful callback of type `(.+?)`\\. In the current configuration, this callback does not support being saved alongside other instances of the same type\\. Please consult the documentation of `(.+?)` regarding valid settings for the callback state to be checkpointable\\. HINT: The `callback\\.state_key` must be unique among all callbacks in the Trainer\\.","errorType":"validation","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/trainer/connectors/callback_connector.py","lineNumber":273,"sourceCode":"        checkpoint_callbacks: list[Callback] = []\n\n        for cb in callbacks:\n            if isinstance(cb, (BatchSizeFinder, LearningRateFinder)):\n                tuner_callbacks.append(cb)\n            elif isinstance(cb, Checkpoint):\n                checkpoint_callbacks.append(cb)\n            else:\n                other_callbacks.append(cb)\n\n        return tuner_callbacks + other_callbacks + checkpoint_callbacks\n\n\ndef _validate_callbacks_list(callbacks: list[Callback]) -> None:\n    stateful_callbacks = [cb for cb in callbacks if is_overridden(\"state_dict\", instance=cb, parent=Callback)]\n    seen_callbacks = set()\n    for callback in stateful_callbacks:\n        if callback.state_key in seen_callbacks:\n            raise RuntimeError(\n                f\"Found more than one stateful callback of type `{type(callback).__name__}`. In the current\"\n                \" configuration, this callback does not support being saved alongside other instances of the same type.\"\n                f\" Please consult the documentation of `{type(callback).__name__}` regarding valid settings for\"\n                \" the callback state to be checkpointable.\"\n                \" HINT: The `callback.state_key` must be unique among all callbacks in the Trainer.\"\n            )\n        seen_callbacks.add(callback.state_key)\n","sourceCodeStart":255,"sourceCodeEnd":281,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/trainer/connectors/callback_connector.py#L255-L281","documentation":"Raised by _validate_callbacks_list at Trainer init when two or more callbacks override state_dict (are stateful) and share the same state_key (class name plus distinguishing init arguments). Because checkpointing stores callback state keyed by state_key, duplicates would overwrite each other and cannot be restored unambiguously.","triggerScenarios":"Adding two instances of a custom stateful callback (or a stateful ModelCheckpoint-like callback) with identical constructor arguments, e.g., callbacks=[MyCallback(lr=1), MyCallback(lr=1)]; the state_key only differs when init args differ.","commonSituations":"Ensembling or multi-experiment setups that add several same-config callbacks; copying a callback instance list; adding two built-in ModelCheckpoint callbacks with identical settings.","solutions":["Give each instance distinct constructor arguments so state_key differs, e.g., MyCallback(name=\"a\") vs MyCallback(name=\"b\")","Keep only one stateful callback of that type","Implement state_dict on only one of the callbacks, or design the callback to aggregate multiple roles internally"],"exampleFix":"# before\ntrainer = Trainer(callbacks=[MyCallback(lr=1.0), MyCallback(lr=1.0)])\n# after\ntrainer = Trainer(callbacks=[MyCallback(lr=1.0), MyCallback(lr=0.5)])  # distinct init args -> unique state_key","handlingStrategy":"validation","validationCode":"from lightning.pytorch.callbacks import Callback\nfrom lightning.pytorch.utilities import is_overridden\n\ndef validate_state_keys(callbacks):\n    seen = set()\n    for cb in callbacks:\n        if is_overridden(\"state_dict\", instance=cb, parent=Callback):\n            key = cb.state_key\n            assert key not in seen, f\"duplicate state_key: {key}\"\n            seen.add(key)\n\nvalidate_state_keys(my_callbacks)\ntrainer = Trainer(callbacks=my_callbacks)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Give stateful callbacks distinguishing init args when adding multiple instances","Run the state_key uniqueness check in tests before Trainer construction"],"tags":["lightning","callbacks","state-dict","checkpointing","trainer-init"],"backgroundTag":"duplicate-callback-instances","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}