keras-team/keras · error · ValueError

Torch export requires PyTorch backend.

Error message

Torch export requires PyTorch backend.

What it means

Exporting to TorchScript/torch format requires running Keras with the PyTorch backend. Requesting format="torch" under any other backend raises this ValueError; reconfigure KERAS_BACKEND=torch or choose another format.

Solutions

  1. Run with the PyTorch backend (keras.config.set_backend("torch")) before exporting
  2. Choose a different export format compatible with the current backend
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/models/model.py:957 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/e451205ba2829875. Report an issue: GitHub.

Appendix: source

Thrown at keras/src/models/model.py:957

        if format not in available_formats:
            raise ValueError(
                f"Unrecognized format={format}. Supported formats are: "
                f"{list(available_formats)}."
            )

        # Check if LiteRT export is available (requires TensorFlow or
        # PyTorch backend)
        if format == "litert" and backend.backend() not in (
            "tensorflow",
            "torch",
        ):
            raise ValueError(
                "LiteRT export requires TensorFlow or PyTorch backend."
            )

        # Check if Torch export is available (requires PyTorch backend)
        if format == "torch" and backend.backend() != "torch":
            raise ValueError("Torch export requires PyTorch backend.")

        if format == "tf_saved_model":
            export_saved_model(
                self,
                filepath,
                verbose,
                input_signature=input_signature,
                **kwargs,
            )
        elif format == "onnx":
            export_onnx(
                self,
                filepath,
                verbose,
                input_signature=input_signature,
                **kwargs,
            )
        elif format == "openvino":

View on GitHub (pinned to 7a34a03db6)