Lightning-AI/pytorch-lightning · error · ValueError

The 'method' parameter only supports 'script' or 'trace', bu

Error message

The 'method' parameter only supports 'script' or 'trace', but value given was: {method}

What it means

to_torchscript accepts only method='script' or method='trace'. Any other string (typos, 'jit', 'Script', etc.) falls through the if/elif chain and raises ValueError naming the bad value.

Source

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

            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)

        if file_path is not None:
            fs = get_filesystem(file_path)
            with fs.open(file_path, "wb") as f:
                torch.jit.save(torchscript_module, f)

        return torchscript_module

    @torch.no_grad()
    def to_tensorrt(
        self,
        file_path: Optional[Union[str, Path, BytesIO]] = None,
        input_sample: Optional[Any] = None,
        ir: Literal["default", "dynamo", "ts"] = "default",
        output_format: Literal["exported_program", "torchscript"] = "exported_program",
        retrace: bool = False,

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Use method="script" or method="trace"
  2. If the value comes from a config, validate/normalize it before calling to_torchscript

Example fix

# before
model.to_torchscript(method="jit")
# after
model.to_torchscript(method="trace", example_inputs=x)
Defensive patterns

Strategy: type-guard

Validate before calling

assert method in ("script", "trace"), f"bad method {method}"

Type guard

def is_valid_ts_method(m: str) -> bool:
    return m in {"script", "trace"}

Prevention

When it happens

Trigger: Calling model.to_torchscript(method="jit") or any string other than 'script'/'trace'.

Common situations: Copy-paste from other tooling that uses different export method names, or passing a variable that defaulted to an unexpected string.

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/8914480d57db33ba. Report an issue: GitHub.