Lightning-AI/pytorch-lightning · error · MisconfigurationException

You called `self.log` with the key `{name}` but it should no

Error message

You called `self.log` with the key `{name}` but it should not contain information about `dataloader_idx` when `add_dataloader_idx=True`

What it means

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_'.

Source

Thrown at src/lightning/pytorch/core/module.py:481

        results = trainer._results
        if results is None:
            raise MisconfigurationException(
                "You are trying to `self.log()` but the loop's result collection is not registered"
                " yet. This is most likely because you are trying to log in a `predict` hook,"
                " but it doesn't support logging"
            )
        if self._current_fx_name is None:
            raise MisconfigurationException(
                "You are trying to `self.log()` but it is not managed by the `Trainer` control flow"
            )

        on_step, on_epoch = _FxValidator.check_logging_and_get_default_levels(
            self._current_fx_name, on_step=on_step, on_epoch=on_epoch
        )

        # make sure user doesn't introduce logic for multi-dataloaders
        if add_dataloader_idx and "/dataloader_idx_" in name:
            raise MisconfigurationException(
                f"You called `self.log` with the key `{name}`"
                " but it should not contain information about `dataloader_idx` when `add_dataloader_idx=True`"
            )

        value = apply_to_collection(value, (Tensor, numbers.Number), self.__to_tensor, name)

        if trainer._logger_connector.should_reset_tensors(self._current_fx_name):
            # if we started a new epoch (running its first batch) the hook name has changed
            # reset any tensors for the new hook name
            results.reset(metrics=False, fx=self._current_fx_name)

        if metric_attribute is None and isinstance(value, Metric):
            if self._metric_attributes is None:
                # compute once
                self._metric_attributes = {
                    id(module): name for name, module in self.named_modules() if isinstance(module, Metric)
                }
                if not self._metric_attributes:

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Drop the suffix from the key and let Lightning add it: self.log('val_loss', loss)
  2. If you truly need the full custom key, pass add_dataloader_idx=False

Example fix

# before
self.log('val_loss/dataloader_idx_0', loss)

# after
self.log('val_loss', loss)  # suffix auto-added when add_dataloader_idx=True
Defensive patterns

Strategy: validation

Validate before calling

if '/dataloader_idx_' in name:
    name = name.split('/dataloader_idx_')[0]  # let Lightning append it
self.log(name, value, add_dataloader_idx=add_dataloader_idx)

Type guard

def clean_metric_key(name: str) -> bool:
    return '/dataloader_idx_' not in name

Prevention

When it happens

Trigger: Calling self.log('val_loss/dataloader_idx_0', loss) with default add_dataloader_idx=True in a module with multiple val/test dataloaders.

Common situations: User pre-formatted metric keys for multi-dataloader runs; copied code that manually disambiguated keys from an older Lightning version or another framework.

Related errors


AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28). Data as JSON: /api/errors/655d980e9626e806. Report an issue: GitHub.