Lightning-AI/pytorch-lightning · warning

predict returned None if it was on purpose, ignore this warn

Error message

predict returned None if it was on purpose, ignore this warning...

What it means

Prediction loop warns when the strategy's predict_step returns None for a batch — no predictions were stored for it. Usually means predict_step didn't return, or returned None via custom logic.

Source

Thrown at src/lightning/pytorch/loops/prediction_loop.py:257

        # the `_step` methods don't take a batch_idx when `dataloader_iter` is used, but all other hooks still do,
        # so we need different kwargs
        hook_kwargs = self._build_kwargs(batch, batch_idx, dataloader_idx if self.num_dataloaders > 1 else None)

        call._call_callback_hooks(trainer, "on_predict_batch_start", *hook_kwargs.values())
        call._call_lightning_module_hook(trainer, "on_predict_batch_start", *hook_kwargs.values())

        self.batch_progress.increment_started()

        # configure step_kwargs
        step_args = (
            self._build_step_args_from_hook_kwargs(hook_kwargs, "predict_step")
            if not using_dataloader_iter
            else (dataloader_iter,)
        )
        predictions = call._call_strategy_hook(trainer, "predict_step", *step_args)
        if predictions is None:
            self._warning_cache.warn("predict returned None if it was on purpose, ignore this warning...")

        self.batch_progress.increment_processed()

        if using_dataloader_iter:
            # update the hook kwargs now that the step method might have consumed the iterator
            batch = data_fetcher._batch
            batch_idx = data_fetcher._batch_idx
            dataloader_idx = data_fetcher._dataloader_idx
            hook_kwargs = self._build_kwargs(batch, batch_idx, dataloader_idx if self.num_dataloaders > 1 else None)

        call._call_callback_hooks(trainer, "on_predict_batch_end", predictions, *hook_kwargs.values())
        call._call_lightning_module_hook(trainer, "on_predict_batch_end", predictions, *hook_kwargs.values())

        self.batch_progress.increment_completed()

        if self._return_predictions or any_on_epoch:
            self._predictions[dataloader_idx].append(move_data_to_device(predictions, torch.device("cpu")))

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Return the predictions from predict_step (default implementation returns model(x), so only overrides hit this)
  2. If None is intentional for some batches, ignore the warning
  3. Verify you didn't shadow 'outputs' vs return

Example fix

# before
def predict_step(self, batch, i):
    x = batch
    self.model(x)  # forgot return
# after
def predict_step(self, batch, i):
    x = batch
    return self.model(x)
Defensive patterns

Strategy: validation

Validate before calling

out = model.predict_step(batch, 0)
assert out is not None, 'predict_step returned None'

Prevention

When it happens

Trigger: LightningModule.predict_step without a return, or overriding predict_step to conditionally return None; also returning a tuple the framework doesn't recognize in older patterns.

Common situations: Writing predict_step for the first time and forgetting the return; custom nested inference logic that discards outputs.

Related errors


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