Lightning-AI/pytorch-lightning · error · MisconfigurationException

`write_interval` should be one of {[i.value for i in WriteIn

Error message

`write_interval` should be one of {[i.value for i in WriteInterval]}.

What it means

BasePredictionWriter writes prediction batches/epochs to storage at a configurable interval. write_interval must be one of the WriteInterval enum values: 'batch', 'epoch', or 'batch_and_epoch'; any other string raises MisconfigurationException in __init__.

Source

Thrown at src/lightning/pytorch/callbacks/prediction_writer.py:112

                torch.save(predictions, os.path.join(self.output_dir, f"predictions_{trainer.global_rank}.pt"))

                # optionally, you can also save `batch_indices` to get the information about the data index
                # from your prediction data
                torch.save(batch_indices, os.path.join(self.output_dir, f"batch_indices_{trainer.global_rank}.pt"))


        # or you can set `write_interval="batch"` and override `write_on_batch_end` to save
        # predictions at batch level
        pred_writer = CustomWriter(output_dir="pred_path", write_interval="epoch")
        trainer = Trainer(accelerator="gpu", strategy="ddp", devices=8, callbacks=[pred_writer])
        model = BoringModel()
        trainer.predict(model, return_predictions=False)

    """

    def __init__(self, write_interval: Literal["batch", "epoch", "batch_and_epoch"] = "batch") -> None:
        if write_interval not in list(WriteInterval):
            raise MisconfigurationException(f"`write_interval` should be one of {[i.value for i in WriteInterval]}.")
        self.interval = WriteInterval(write_interval)

    @override
    def setup(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule", stage: str) -> None:
        if is_param_in_hook_signature(pl_module.predict_step, "dataloader_iter", explicit=True):
            raise NotImplementedError("The `PredictionWriterCallback` does not support using `dataloader_iter`.")

    def write_on_batch_end(
        self,
        trainer: "pl.Trainer",
        pl_module: "pl.LightningModule",
        prediction: Any,
        batch_indices: Optional[Sequence[int]],
        batch: Any,
        batch_idx: int,
        dataloader_idx: int,
    ) -> None:
        """Override with the logic to write a single batch."""

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Use one of 'batch', 'epoch', or 'batch_and_epoch' exactly
  2. Validate config-sourced values against ['batch','epoch','batch_and_epoch'] before constructing the callback

Example fix

# before
BasePredictionWriter(write_interval='step')
# after
BasePredictionWriter(write_interval='batch')
Defensive patterns

Strategy: validation

Validate before calling

VALID = {'batch', 'epoch', 'batch_and_epoch'}
interval = cfg.get('write_interval', 'batch')
assert interval in VALID, f"write_interval must be one of {VALID}, got {interval!r}"

Type guard

def is_write_interval(v) -> bool:
    return v in ('batch', 'epoch', 'batch_and_epoch')

Prevention

When it happens

Trigger: Passing write_interval='step', 'every_batch', 'batch_epoch', or a typo like 'epcoh' to BasePredictionWriter; using a value from a config that doesn't match the enum.

Common situations: Guessing the API ('step' seems natural but is invalid); case sensitivity ('Batch' fails); stale configs written against a different callback's vocabulary.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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