{"record":{"id":"942dad05212803e7","repo":"Lightning-AI/pytorch-lightning","slug":"choosing-method-trace-requires-either-example-i","errorCode":null,"errorMessage":"Choosing method=`trace` requires either `example_inputs` or `model.example_input_array` to be defined.","messagePattern":"Choosing method=`trace` requires either `example_inputs` or `model\\.example_input_array` to be defined\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/core/module.py","lineNumber":1579,"sourceCode":"            This LightningModule as a torchscript, regardless of whether `file_path` is\n            defined or not.\n\n        \"\"\"\n        rank_zero_deprecation(\n            \"`LightningModule.to_torchscript` has been deprecated in v2.7 and will be removed in v2.8. \"\n            \"TorchScript is deprecated in PyTorch. Use `torch.export.export()` for model exporting instead. \"\n            \"See https://pytorch.org/docs/stable/export.html for more information.\"\n        )\n        mode = self.training\n\n        if method == \"script\":\n            with _jit_is_scripting():\n                torchscript_module = torch.jit.script(self.eval(), **kwargs)\n        elif method == \"trace\":\n            # if no example inputs are provided, try to see if model has example_input_array set\n            if example_inputs is None:\n                if self.example_input_array is None:\n                    raise ValueError(\n                        \"Choosing method=`trace` requires either `example_inputs`\"\n                        \" or `model.example_input_array` to be defined.\"\n                    )\n                example_inputs = self.example_input_array\n\n            if kwargs.get(\"check_inputs\") is not None:\n                kwargs[\"check_inputs\"] = self._on_before_batch_transfer(kwargs[\"check_inputs\"])\n                kwargs[\"check_inputs\"] = self._apply_batch_transfer_handler(kwargs[\"check_inputs\"])\n\n            # automatically send example inputs to the right device and use trace\n            example_inputs = self._on_before_batch_transfer(example_inputs)\n            example_inputs = self._apply_batch_transfer_handler(example_inputs)\n            with _jit_is_scripting():\n                torchscript_module = torch.jit.trace(func=self.eval(), example_inputs=example_inputs, **kwargs)\n        else:\n            raise ValueError(f\"The 'method' parameter only supports 'script' or 'trace', but value given was: {method}\")\n\n        self.train(mode)","sourceCodeStart":1561,"sourceCodeEnd":1597,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/core/module.py#L1561-L1597","documentation":"to_torchscript(method='trace') requires tracing inputs because torch.jit.trace must execute the model once. Lightning falls back to `self.example_input_array`; if that is also None it raises ValueError.","triggerScenarios":"Calling `model.to_torchscript(method=\"trace\")` with no example_inputs on a model without example_input_array set.","commonSituations":"Converting a model to TorchScript for deployment (C++ inference, Trititon/TorchServe) where the model class never defined example_input_array.","solutions":["Pass example_inputs=model.to(torch.randn(1, *input_shape)) explicitly","Or set self.example_input_array in the model","Or use method='script' if the model is scriptable and inputs are unavailable"],"exampleFix":"# before\nts = model.to_torchscript(method=\"trace\")\n# after\nts = model.to_torchscript(method=\"trace\", example_inputs=torch.randn(1, 28, 28))","handlingStrategy":"validation","validationCode":"inputs = example_inputs if example_inputs is not None else getattr(model, \"example_input_array\", None)\nassert inputs is not None, \"trace needs example_inputs\"\nmodel.to_torchscript(method=\"trace\", example_inputs=inputs)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Standardize on setting example_input_array in model constructors","Prefer method='script' when no sample is available and the model is scriptable"],"tags":["torchscript","trace","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"}