Skip to content

The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.

More friendly error messages for corrupted safetensors files.

comfyanonymous13fd4d6e4512
diff --git a/comfy/utils.py b/comfy/utils.py
index b35062824..c901347c4 100644
--- a/comfy/utils.py
+++ b/comfy/utils.py
@@ -50,7 +50,16 @@ def load_torch_file(ckpt, safe_load=False, device=None):
if device is None:
device = torch.device("cpu")
if ckpt.lower().endswith(".safetensors") or ckpt.lower().endswith(".sft"):
- sd = safetensors.torch.load_file(ckpt, device=device.type)
+ try:
+ sd = safetensors.torch.load_file(ckpt, device=device.type)
+ 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 incomplete. Check the file size and make sure you have copied/downloaded it correctly.".format(message, ckpt))
+ raise e
else:
if safe_load or ALWAYS_SAFE_LOAD:
pl_sd = torch.load(ckpt, map_location=device, weights_only=True)