Lightning-AI/pytorch-lightning · error · ValueError

Choosing method=`trace` requires either `example_inputs` or

Error message

Choosing method=`trace` requires either `example_inputs` or `model.example_input_array` to be defined.

What it means

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.

Source

Thrown at src/lightning/pytorch/core/module.py:1579

            This LightningModule as a torchscript, regardless of whether `file_path` is
            defined or not.

        """
        rank_zero_deprecation(
            "`LightningModule.to_torchscript` has been deprecated in v2.7 and will be removed in v2.8. "
            "TorchScript is deprecated in PyTorch. Use `torch.export.export()` for model exporting instead. "
            "See https://pytorch.org/docs/stable/export.html for more information."
        )
        mode = self.training

        if method == "script":
            with _jit_is_scripting():
                torchscript_module = torch.jit.script(self.eval(), **kwargs)
        elif method == "trace":
            # if no example inputs are provided, try to see if model has example_input_array set
            if example_inputs is None:
                if self.example_input_array is None:
                    raise ValueError(
                        "Choosing method=`trace` requires either `example_inputs`"
                        " or `model.example_input_array` to be defined."
                    )
                example_inputs = self.example_input_array

            if kwargs.get("check_inputs") is not None:
                kwargs["check_inputs"] = self._on_before_batch_transfer(kwargs["check_inputs"])
                kwargs["check_inputs"] = self._apply_batch_transfer_handler(kwargs["check_inputs"])

            # automatically send example inputs to the right device and use trace
            example_inputs = self._on_before_batch_transfer(example_inputs)
            example_inputs = self._apply_batch_transfer_handler(example_inputs)
            with _jit_is_scripting():
                torchscript_module = torch.jit.trace(func=self.eval(), example_inputs=example_inputs, **kwargs)
        else:
            raise ValueError(f"The 'method' parameter only supports 'script' or 'trace', but value given was: {method}")

        self.train(mode)

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Pass example_inputs=model.to(torch.randn(1, *input_shape)) explicitly
  2. Or set self.example_input_array in the model
  3. Or use method='script' if the model is scriptable and inputs are unavailable

Example fix

# before
ts = model.to_torchscript(method="trace")
# after
ts = model.to_torchscript(method="trace", example_inputs=torch.randn(1, 28, 28))
Defensive patterns

Strategy: validation

Validate before calling

inputs = example_inputs if example_inputs is not None else getattr(model, "example_input_array", None)
assert inputs is not None, "trace needs example_inputs"
model.to_torchscript(method="trace", example_inputs=inputs)

Prevention

When it happens

Trigger: Calling `model.to_torchscript(method="trace")` with no example_inputs on a model without example_input_array set.

Common situations: Converting a model to TorchScript for deployment (C++ inference, Trititon/TorchServe) where the model class never defined example_input_array.

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


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