{"record":{"id":"655d980e9626e806","repo":"Lightning-AI/pytorch-lightning","slug":"you-called-self-log-with-the-key-name-but-it","errorCode":null,"errorMessage":"You called `self.log` with the key `{name}` but it should not contain information about `dataloader_idx` when `add_dataloader_idx=True`","messagePattern":"You called `self\\.log` with the key `(.+?)` but it should not contain information about `dataloader_idx` when `add_dataloader_idx=True`","errorType":"exception","errorClass":"MisconfigurationException","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/core/module.py","lineNumber":481,"sourceCode":"        results = trainer._results\n        if results is None:\n            raise MisconfigurationException(\n                \"You are trying to `self.log()` but the loop's result collection is not registered\"\n                \" yet. This is most likely because you are trying to log in a `predict` hook,\"\n                \" but it doesn't support logging\"\n            )\n        if self._current_fx_name is None:\n            raise MisconfigurationException(\n                \"You are trying to `self.log()` but it is not managed by the `Trainer` control flow\"\n            )\n\n        on_step, on_epoch = _FxValidator.check_logging_and_get_default_levels(\n            self._current_fx_name, on_step=on_step, on_epoch=on_epoch\n        )\n\n        # make sure user doesn't introduce logic for multi-dataloaders\n        if add_dataloader_idx and \"/dataloader_idx_\" in name:\n            raise MisconfigurationException(\n                f\"You called `self.log` with the key `{name}`\"\n                \" but it should not contain information about `dataloader_idx` when `add_dataloader_idx=True`\"\n            )\n\n        value = apply_to_collection(value, (Tensor, numbers.Number), self.__to_tensor, name)\n\n        if trainer._logger_connector.should_reset_tensors(self._current_fx_name):\n            # if we started a new epoch (running its first batch) the hook name has changed\n            # reset any tensors for the new hook name\n            results.reset(metrics=False, fx=self._current_fx_name)\n\n        if metric_attribute is None and isinstance(value, Metric):\n            if self._metric_attributes is None:\n                # compute once\n                self._metric_attributes = {\n                    id(module): name for name, module in self.named_modules() if isinstance(module, Metric)\n                }\n                if not self._metric_attributes:","sourceCodeStart":463,"sourceCodeEnd":499,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/core/module.py#L463-L499","documentation":"When add_dataloader_idx=True (the default), Lightning automatically appends /dataloader_idx_N to logged keys for multi-dataloader hooks. Manually embedding that suffix in the key yourself would double-apply it, so Lightning rejects any key already containing '/dataloader_idx_'.","triggerScenarios":"Calling self.log('val_loss/dataloader_idx_0', loss) with default add_dataloader_idx=True in a module with multiple val/test dataloaders.","commonSituations":"User pre-formatted metric keys for multi-dataloader runs; copied code that manually disambiguated keys from an older Lightning version or another framework.","solutions":["Drop the suffix from the key and let Lightning add it: self.log('val_loss', loss)","If you truly need the full custom key, pass add_dataloader_idx=False"],"exampleFix":"# before\nself.log('val_loss/dataloader_idx_0', loss)\n\n# after\nself.log('val_loss', loss)  # suffix auto-added when add_dataloader_idx=True","handlingStrategy":"validation","validationCode":"if '/dataloader_idx_' in name:\n    name = name.split('/dataloader_idx_')[0]  # let Lightning append it\nself.log(name, value, add_dataloader_idx=add_dataloader_idx)","typeGuard":"def clean_metric_key(name: str) -> bool:\n    return '/dataloader_idx_' not in name","tryCatchPattern":null,"preventionTips":["Use plain metric names; never hand-format dataloader_idx suffixes","Set add_dataloader_idx=False only when you fully manage key names"],"tags":["pytorch-lightning","self-log","multi-dataloader","key-validation"],"backgroundTag":"metric-key-format-invalid","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}