{"record":{"id":"af2e42ca8daf3fc2","repo":"Lightning-AI/pytorch-lightning","slug":"could-not-export-to-onnx-since-neither-input-samp","errorCode":null,"errorMessage":"Could not export to ONNX since neither `input_sample` nor `model.example_input_array` attribute is set.","messagePattern":"Could not export to ONNX since neither `input_sample` nor `model\\.example_input_array` attribute is set\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/core/module.py","lineNumber":1493,"sourceCode":"\n            model = SimpleModel()\n            input_sample = torch.randn(1, 64)\n            model.to_onnx(\"export.onnx\", input_sample, export_params=True)\n\n        \"\"\"\n        if not _ONNX_AVAILABLE:\n            raise ModuleNotFoundError(f\"`{type(self).__name__}.to_onnx()` requires `onnx` to be installed.\")\n\n        if kwargs.get(\"dynamo\", False) and not _ONNXSCRIPT_AVAILABLE:\n            raise ModuleNotFoundError(\n                f\"`{type(self).__name__}.to_onnx(dynamo=True)` requires `onnxscript` to be installed.\"\n            )\n\n        mode = self.training\n\n        if input_sample is None:\n            if self.example_input_array is None:\n                raise ValueError(\n                    \"Could not export to ONNX since neither `input_sample` nor\"\n                    \" `model.example_input_array` attribute is set.\"\n                )\n            input_sample = self.example_input_array\n\n        input_sample = self._on_before_batch_transfer(input_sample)\n        input_sample = self._apply_batch_transfer_handler(input_sample)\n\n        file_path = str(file_path) if isinstance(file_path, Path) else file_path\n        # PyTorch (2.5) declares file_path to be str | PathLike[Any] | None, but\n        #               BytesIO does work, too.\n        ret = torch.onnx.export(self, input_sample, file_path, **kwargs)  # type: ignore\n        self.train(mode)\n        return ret\n\n    @torch.no_grad()\n    def to_torchscript(\n        self,","sourceCodeStart":1475,"sourceCodeEnd":1511,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/core/module.py#L1475-L1511","documentation":"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.","triggerScenarios":"Calling `model.to_onnx(\"f.onnx\")` with no second argument on a model whose `__init__` never assigned `self.example_input_array`.","commonSituations":"Reusing a plain nn.Module-style LightningModule that omits example_input_array, or refactorings that removed the attribute.","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__"],"exampleFix":"# before\nmodel.to_onnx(\"f.onnx\")\n# after\nmodel.to_onnx(\"f.onnx\", torch.randn(1, 28, 28))\n# or in __init__: self.example_input_array = torch.randn(1, 28, 28)","handlingStrategy":"validation","validationCode":"sample = input_sample if input_sample is not None else getattr(model, \"example_input_array\", None)\nif sample is None:\n    raise ValueError(\"Provide input_sample or set model.example_input_array\")\nmodel.to_onnx(path, sample)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass input_sample explicitly in export scripts","Set example_input_array in every deployable LightningModule's __init__"],"tags":["onnx","export","example-input","lightning"],"backgroundTag":"missing-required-argument","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}