invoke-ai/InvokeAI · error · Exception

Supported only pytorch safetensors files

Error message

Supported only pytorch safetensors files

What it means

_fast_safetensors_reader() parses a safetensors header and only accepts tensors whose declared __metadata__ 'format' is pt/torch/pytorch. A safetensors file saved by another framework (TensorFlow, JAX, Paddle, MLX) is rejected with this generic Exception.

Source

Thrown at invokeai/backend/model_manager/util/model_util.py:32

from invokeai.backend.util.logging import InvokeAILogger

logger = InvokeAILogger.get_logger()


def _fast_safetensors_reader(path: str) -> Dict[str, torch.Tensor]:
    checkpoint = {}
    device = torch.device("meta")
    with open(path, "rb") as f:
        definition_len = int.from_bytes(f.read(8), "little")
        definition_json = f.read(definition_len)
        definition = json.loads(definition_json)

        if "__metadata__" in definition and definition["__metadata__"].get("format", "pt") not in {
            "pt",
            "torch",
            "pytorch",
        }:
            raise Exception("Supported only pytorch safetensors files")
        definition.pop("__metadata__", None)

        for key, info in definition.items():
            dtype = {
                "I8": torch.int8,
                "I16": torch.int16,
                "I32": torch.int32,
                "I64": torch.int64,
                "F16": torch.float16,
                "F32": torch.float32,
                "F64": torch.float64,
            }[info["dtype"]]

            checkpoint[key] = torch.empty(info["shape"], dtype=dtype, device=device)

    return checkpoint

View on GitHub (pinned to 0b6a024f2f)

Solutions

  1. Obtain a PyTorch-format version of the model (most HF repos have one).
  2. Re-export the file with PyTorch save_file() so __metadata__['format'] is 'pt'.
  3. As a last resort, strip/patch the __metadata__ format field with the safetensors library after verifying tensors are loadable (advanced).
Defensive patterns

Strategy: validation

Validate before calling

import json, struct

def safetensors_format(path) -> str | None:
    with open(path, 'rb') as f:
        (n,) = struct.unpack('<Q', f.read(8))
        header = json.loads(f.read(n))
    return header.get('__metadata__', {}).get('format', 'pt')

if safetensors_format(file) not in ('pt', 'torch', 'pytorch'):
    skip_import = True  # non-PyTorch safetensors

Type guard

def is_pytorch_safetensors(path) -> bool:
    try:
        return safetensors_format(path) in {'pt', 'torch', 'pytorch'}
    except Exception:
        return False

Try / catch

try:
    meta = read_checkpoint_meta(path)
except Exception as e:
    if 'Supported only pytorch safetensors' in str(e):
        meta = None  # need a PyTorch-format file
    else:
        raise

Prevention

When it happens

Trigger: Calling read_checkpoint_meta() on a .safetensors file whose header contains __metadata__ with format set to something other than 'pt', 'torch', or 'pytorch' (e.g. 'tf', 'jax', 'np', 'mlx').

Common situations: Importing models converted/exported from TensorFlow or JAX ecosystems; files produced by non-PyTorch training frameworks; MLX-converted checkpoints on Apple silicon.

Related errors


AI-assisted analysis of invoke-ai/InvokeAI@0b6a024f2f (2026-08-29). Data as JSON: /api/errors/4205719ef762a96c. Report an issue: GitHub.