Lightning-AI/pytorch-lightning · error · ValueError
Could not export to ONNX since neither `input_sample` nor `m
Error message
Could not export to ONNX since neither `input_sample` nor `model.example_input_array` attribute is set.
What it means
ONNX export needs a concrete input tensor to trace/script the model. If `input_sample` is not passed and the LightningModule has no `self.example_input_array` attribute set, Lightning raises ValueError because there is nothing to run the model with.
Source
Thrown at src/lightning/pytorch/core/module.py:1493
model = SimpleModel()
input_sample = torch.randn(1, 64)
model.to_onnx("export.onnx", input_sample, export_params=True)
"""
if not _ONNX_AVAILABLE:
raise ModuleNotFoundError(f"`{type(self).__name__}.to_onnx()` requires `onnx` to be installed.")
if kwargs.get("dynamo", False) and not _ONNXSCRIPT_AVAILABLE:
raise ModuleNotFoundError(
f"`{type(self).__name__}.to_onnx(dynamo=True)` requires `onnxscript` to be installed."
)
mode = self.training
if input_sample is None:
if self.example_input_array is None:
raise ValueError(
"Could not export to ONNX since neither `input_sample` nor"
" `model.example_input_array` attribute is set."
)
input_sample = self.example_input_array
input_sample = self._on_before_batch_transfer(input_sample)
input_sample = self._apply_batch_transfer_handler(input_sample)
file_path = str(file_path) if isinstance(file_path, Path) else file_path
# PyTorch (2.5) declares file_path to be str | PathLike[Any] | None, but
# BytesIO does work, too.
ret = torch.onnx.export(self, input_sample, file_path, **kwargs) # type: ignore
self.train(mode)
return ret
@torch.no_grad()
def to_torchscript(
self,View on GitHub (pinned to 9fed5c27d2)
Solutions
- Pass an input sample explicitly: model.to_onnx("f.onnx", torch.randn(1, *input_shape))
- Or set self.example_input_array = torch.randn(1, *input_shape) in the model's __init__
Example fix
# before
model.to_onnx("f.onnx")
# after
model.to_onnx("f.onnx", torch.randn(1, 28, 28))
# or in __init__: self.example_input_array = torch.randn(1, 28, 28) Defensive patterns
Strategy: validation
Validate before calling
sample = input_sample if input_sample is not None else getattr(model, "example_input_array", None)
if sample is None:
raise ValueError("Provide input_sample or set model.example_input_array")
model.to_onnx(path, sample) Prevention
- Always pass input_sample explicitly in export scripts
- Set example_input_array in every deployable LightningModule's __init__
When it happens
Trigger: Calling `model.to_onnx("f.onnx")` with no second argument on a model whose `__init__` never assigned `self.example_input_array`.
Common situations: Reusing a plain nn.Module-style LightningModule that omits example_input_array, or refactorings that removed the attribute.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- `{type(self).__name__}.to_onnx()` requires `onnx` to be inst
- `{type(self).__name__}.to_onnx(dynamo=True)` requires `onnxs
- Choosing method=`trace` requires either `example_inputs` or
- `{type(self).__name__}.to_tensorrt` requires `torch_tensorrt
- Could not export to TensorRT since neither `input_sample` no
AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28).
Data as JSON: /api/errors/af2e42ca8daf3fc2.
Report an issue: GitHub.