{"record":{"id":"d7519659e954bc4d","repo":"Lightning-AI/pytorch-lightning","slug":"f-hook-name-is-not-a-shared-hook-within-light","errorCode":null,"errorMessage":"f\"`{hook_name}` is not a shared hook within `LightningModule` and `LightningDataModule`.\" f\" Valid hooks are {self._valid_hooks}.\"","messagePattern":"f\"`(.+?)` is not a shared hook within `LightningModule` and `LightningDataModule`\\.\" f\" Valid hooks are (.+?)\\.\"","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/trainer/connectors/data_connector.py","lineNumber":361,"sourceCode":"\n    1. the :class:`~lightning.pytorch.core.LightningModule`,\n    2. the :class:`~lightning.pytorch.core.datamodule.LightningDataModule`,\n\n    Arguments:\n        model: A ``LightningModule``\n        datamodule: A ``LightningDataModule``\n\n    \"\"\"\n\n    model: \"pl.LightningModule\"\n    datamodule: Optional[\"pl.LightningDataModule\"]\n    _valid_hooks: tuple[str, ...] = field(\n        default=(\"on_before_batch_transfer\", \"transfer_batch_to_device\", \"on_after_batch_transfer\")\n    )\n\n    def get_instance(self, hook_name: str) -> Union[\"pl.LightningModule\", \"pl.LightningDataModule\"]:\n        if hook_name not in self._valid_hooks:\n            raise ValueError(\n                f\"`{hook_name}` is not a shared hook within `LightningModule` and `LightningDataModule`.\"\n                f\" Valid hooks are {self._valid_hooks}.\"\n            )\n\n        if self.datamodule is None:\n            return self.model\n\n        if is_overridden(hook_name, self.datamodule):\n            if is_overridden(hook_name, self.model):\n                warning_cache.warn(\n                    f\"You have overridden `{hook_name}` in both `LightningModule` and `LightningDataModule`.\"\n                    \" It will use the implementation from `LightningDataModule` instance.\"\n                )\n            return self.datamodule\n\n        if is_overridden(hook_name, self.model):\n            warning_cache.warn(\n                f\"You have overridden `{hook_name}` in `LightningModule` but have passed in a\"","sourceCodeStart":343,"sourceCodeEnd":379,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/trainer/connectors/data_connector.py#L343-L379","documentation":"ValueError from _DataFetcher/get_instance: the requested hook_name is not one of the shared hooks Lightning forwards between LightningModule and LightningDataModule — only 'on_before_batch_transfer', 'transfer_batch_to_device', 'on_after_batch_transfer' are valid. Calling get_instance with any other hook name is an internal API misuse.","triggerScenarios":"Internal/advanced code calling _DataFetcher.get_instance(\"train_dataloader\") or any hook outside the allowlist; custom forks or plugins invoking the connector's hook-resolution API with arbitrary hook names.","commonSituations":"Plugin/strategy code copied from older Lightning internals where the API accepted more names; monkeypatching or extending Lightning internals during research code.","solutions":["Use only the three supported shared-hook names with get_instance","Resolve other hooks directly on trainer.datamodule or trainer.lightning_module yourself","Update custom code to current Lightning APIs after version changes"],"exampleFix":"# before\ninstance = fetcher.get_instance(\"train_dataloader\")\n# after\ninstance = fetcher.get_instance(\"transfer_batch_to_device\")\n# or directly:\ndl = trainer.datamodule.train_dataloader() if trainer.datamodule else model.train_dataloader","handlingStrategy":"validation","validationCode":"VALID = (\"on_before_batch_transfer\", \"transfer_batch_to_device\", \"on_after_batch_transfer\")\nif hook_name not in VALID:\n    raise ValueError(f\"unsupported hook {hook_name}; resolve it on trainer.datamodule/lightning_module instead\")","typeGuard":"def is_shared_hook(name: str) -> bool:\n    return name in (\"on_before_batch_transfer\", \"transfer_batch_to_device\", \"on_after_batch_transfer\")","tryCatchPattern":null,"preventionTips":["Treat connector internals as private APIs; pin Lightning versions when patching","Resolve non-shared hooks directly on the module/datamodule"],"tags":["lightning","internal-api","hooks","data-transfer","misuse"],"backgroundTag":"invalid-hook-name","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}