keras-team/keras · error · RuntimeError

Failed to convert PyTorch model to LiteRT. Common causes…

Error message

Failed to convert PyTorch model to LiteRT. Common causes: unsupported operations, dynamic shapes, or complex control flow. Original error: {e}

What it means

Raised when the underlying litert_torch.convert() call raises while translating the Keras-wrapped PyTorch module into a LiteRT/FlatBuffer artifact. The original exception is chained as {e}, so this is a wrapper error: the real cause is the PyTorch-to-TFLite converter rejecting something in the model (unsupported ops, dynamic shapes, control flow).

Solutions

  1. Inspect the chained original error to identify the exact op or shape that failed conversion
  2. Rebuild or refactor the model to avoid operations unsupported by the AI Edge Torch converter (e.g., exotic custom layers, data-dependent control flow)
  3. Make input shapes static (fixed batch/sequence dimensions) before exporting
  4. Replace dynamic control flow (loops/conditionals on tensor values) with tensor operations
  5. Export to an intermediate format (e.g., ONNX or saved PyTorch state) and use a TFLite conversion path that supports the ops
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at keras/src/export/litert.py:246 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/1374a957e4656db2. Report an issue: GitHub.

Appendix: source

Thrown at keras/src/export/litert.py:246

                # Although Keras models manage training mode via the
                # `training` argument to `call()`, `litert_torch.convert`
                # checks `module.training` and warns when it is `True`.
                # Calling `eval()` silences that warning and ensures any
                # PyTorch-native submodules are in inference mode.
                model.eval()

                litert_torch_kwargs = _prepare_litert_kwargs(
                    kwargs, litert_torch
                )

                with _silence_output(verbose is False):
                    try:
                        edge_model = litert_torch.convert(
                            model, sample_inputs, **litert_torch_kwargs
                        )
                    except Exception as e:
                        raise RuntimeError(
                            "Failed to convert PyTorch model to LiteRT. "
                            "Common causes: unsupported operations, "
                            "dynamic shapes, or complex control flow. "
                            f"Original error: {e}"
                        ) from e

                    edge_model.export(filepath)
        finally:
            if device != "cpu":
                for v in model.variables:
                    v.value.data = v.value.data.to(device)

    if verbose is not False:
        io_utils.print_msg(f"Saved artifact at '{filepath}'.")

    return filepath

View on GitHub (pinned to 7a34a03db6)