{"record":{"id":"4205719ef762a96c","repo":"invoke-ai/InvokeAI","slug":"supported-only-pytorch-safetensors-files","errorCode":null,"errorMessage":"Supported only pytorch safetensors files","messagePattern":"Supported only pytorch safetensors files","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"invokeai/backend/model_manager/util/model_util.py","lineNumber":32,"sourceCode":"from invokeai.backend.util.logging import InvokeAILogger\n\nlogger = InvokeAILogger.get_logger()\n\n\ndef _fast_safetensors_reader(path: str) -> Dict[str, torch.Tensor]:\n    checkpoint = {}\n    device = torch.device(\"meta\")\n    with open(path, \"rb\") as f:\n        definition_len = int.from_bytes(f.read(8), \"little\")\n        definition_json = f.read(definition_len)\n        definition = json.loads(definition_json)\n\n        if \"__metadata__\" in definition and definition[\"__metadata__\"].get(\"format\", \"pt\") not in {\n            \"pt\",\n            \"torch\",\n            \"pytorch\",\n        }:\n            raise Exception(\"Supported only pytorch safetensors files\")\n        definition.pop(\"__metadata__\", None)\n\n        for key, info in definition.items():\n            dtype = {\n                \"I8\": torch.int8,\n                \"I16\": torch.int16,\n                \"I32\": torch.int32,\n                \"I64\": torch.int64,\n                \"F16\": torch.float16,\n                \"F32\": torch.float32,\n                \"F64\": torch.float64,\n            }[info[\"dtype\"]]\n\n            checkpoint[key] = torch.empty(info[\"shape\"], dtype=dtype, device=device)\n\n    return checkpoint\n\n","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/backend/model_manager/util/model_util.py#L14-L50","documentation":"_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.","triggerScenarios":"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').","commonSituations":"Importing models converted/exported from TensorFlow or JAX ecosystems; files produced by non-PyTorch training frameworks; MLX-converted checkpoints on Apple silicon.","solutions":["Obtain a PyTorch-format version of the model (most HF repos have one).","Re-export the file with PyTorch save_file() so __metadata__['format'] is 'pt'.","As a last resort, strip/patch the __metadata__ format field with the safetensors library after verifying tensors are loadable (advanced)."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"import json, struct\n\ndef safetensors_format(path) -> str | None:\n    with open(path, 'rb') as f:\n        (n,) = struct.unpack('<Q', f.read(8))\n        header = json.loads(f.read(n))\n    return header.get('__metadata__', {}).get('format', 'pt')\n\nif safetensors_format(file) not in ('pt', 'torch', 'pytorch'):\n    skip_import = True  # non-PyTorch safetensors","typeGuard":"def is_pytorch_safetensors(path) -> bool:\n    try:\n        return safetensors_format(path) in {'pt', 'torch', 'pytorch'}\n    except Exception:\n        return False","tryCatchPattern":"try:\n    meta = read_checkpoint_meta(path)\nexcept Exception as e:\n    if 'Supported only pytorch safetensors' in str(e):\n        meta = None  # need a PyTorch-format file\n    else:\n        raise","preventionTips":["Download model weights from PyTorch-based repos only.","Check the safetensors header metadata format before importing.","Convert non-PyTorch exports with the source framework before use."],"tags":["safetensors","format-mismatch","model-loading"],"backgroundTag":"unsupported-tensor-format","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}