Comfy-Org/ComfyUI · error · ValueError

{}\n\nFile path: {}\n\nThe safetensors file is corrupt or in

Error message

{}\n\nFile path: {}\n\nThe safetensors file is corrupt or invalid. Make sure this is actually a safetensors file and not a ckpt or pt or other filetype.

What it means

comfy.utils.load_torch_file uses safetensors.safe_open when the file ends in .safetensors. safetensors raises HeaderTooLarge when the 8-byte header length prefix is absurdly large — the classic signature of a pickle-based .ckpt/.pt file renamed to .safetensors, or a truncated/corrupt download. ComfyUI re-raises with the file path and an explanation suggesting a wrong file type.

Source

Thrown at comfy/utils.py:146

            if comfy.memory_management.aimdo_enabled:
                sd, metadata = load_safetensors(ckpt)
                if not return_metadata:
                    metadata = None
            else:
                with safetensors.safe_open(ckpt, framework="pt", device=device.type) as f:
                    sd = {}
                    for k in f.keys():
                        tensor = f.get_tensor(k)
                        if DISABLE_MMAP:  # TODO: Not sure if this is the best way to bypass the mmap issues
                            tensor = tensor.to(device=device, copy=True)
                        sd[k] = tensor
                    if return_metadata:
                        metadata = f.metadata()
        except Exception as e:
            if len(e.args) > 0:
                message = e.args[0]
                if "HeaderTooLarge" in message:
                    raise ValueError("{}\n\nFile path: {}\n\nThe safetensors file is corrupt or invalid. Make sure this is actually a safetensors file and not a ckpt or pt or other filetype.".format(message, ckpt))
                if "MetadataIncompleteBuffer" in message:
                    raise ValueError("{}\n\nFile path: {}\n\nThe safetensors file is corrupt/incomplete. Check the file size and make sure you have copied/downloaded it correctly.".format(message, ckpt))
            raise e
    else:
        torch_args = {}
        if MMAP_TORCH_FILES:
            torch_args["mmap"] = True

        pl_sd = torch.load(ckpt, map_location=device, weights_only=True, **torch_args)

        if "state_dict" in pl_sd:
            sd = pl_sd["state_dict"]
        else:
            if len(pl_sd) == 1:
                key = list(pl_sd.keys())[0]
                sd = pl_sd[key]
                if not isinstance(sd, dict):
                    sd = pl_sd

View on GitHub (pinned to 1c6d8d45b3)

Solutions

  1. Confirm the true file type (file model.safetensors / xxd first bytes); if pickle/zip, rename to .ckpt/.pt so the torch.load branch is used
  2. Re-download the file and check its size against the source
  3. If it is a git-lfs pointer or HTML page, fetch the real binary with git lfs pull or a proper URL

Example fix

// before
sd = utils.load_torch_file('model.safetensors')  # actually a renamed ckpt

# after
# file model.safetensors -> 'Zip archive' or pickle: rename to model.ckpt
sd = utils.load_torch_file('model.ckpt')
Defensive patterns

Strategy: validation

Validate before calling

def looks_like_safetensors(path):
    with open(path, 'rb') as f:
        import struct
        n = struct.unpack('<Q', f.read(8))[0]
    return 0 < n < 100_000_000  # sane header size
if path.endswith('.safetensors') and not looks_like_safetensors(path):
    raise ValueError('file is not a real safetensors file (probably renamed ckpt or truncated)')

Try / catch

except ValueError as e:
    if 'corrupt or invalid' in str(e):
        # point user at file type check / re-download
        raise

Prevention

When it happens

Trigger: Calling load_torch_file on a .safetensors-suffixed file whose first 8 bytes decode to a huge header size — e.g. a PyTorch pickle file renamed, a ckpt saved with the wrong extension, or a partially downloaded file.

Common situations: Renamed ckpt to safetensors to satisfy a loader; download interrupted so only the beginning exists; file is actually a git-lfs pointer or HTML error page saved with the right name.

Related errors


AI-assisted analysis of Comfy-Org/ComfyUI@1c6d8d45b3 (2026-08-14). Data as JSON: /api/errors/b668794c0fe118a8. Report an issue: GitHub.