RuntimeError
safetensors.safe_open.RuntimeError
Stack trace
Traceback (most recent call last):
File "load_model.py", line 12, in <module>
model = AutoModel.from_pretrained(checkpoint_path, trust_remote_code=True)
File "/usr/local/lib/python3.10/site-packages/transformers/models/auto/auto_factory.py", line 484, in from_pretrained
state_dict = safetensors.safe_open(filename, framework="pt")
File "/usr/local/lib/python3.10/site-packages/safetensors/safe_open.py", line 45, in safe_open
raise RuntimeError("Error loading safetensors checkpoint: corrupted file")
RuntimeError: Error loading safetensors checkpoint: corrupted file Why it happens
The safetensors loader detects that the checkpoint file is corrupted, truncated, or incomplete, often due to interrupted downloads, disk write errors, or file transfer issues. This prevents the model weights from being loaded correctly.
Detection
Check for RuntimeError exceptions from safetensors.safe_open during model loading and verify file integrity by comparing checksums or re-downloading the checkpoint if errors occur.
Causes & fixes
Checkpoint file was partially downloaded or interrupted during download
Re-download the checkpoint file completely from the Huggingface hub or your storage source to ensure a full, uncorrupted file.
Disk write or storage corruption corrupted the checkpoint file after download
Verify disk health and storage integrity, then delete and re-save the checkpoint file to a reliable storage location.
Using an outdated or incompatible safetensors version that misreads the checkpoint format
Upgrade safetensors to the latest stable version compatible with your transformers library.
Manual modification or partial extraction of the checkpoint file corrupted its internal structure
Avoid manual edits; always use official tools or APIs to handle safetensors files and re-download if necessary.
Code: broken vs fixed
from transformers import AutoModel
checkpoint_path = "./model_checkpoint"
model = AutoModel.from_pretrained(checkpoint_path) # RuntimeError: corrupted safetensors checkpoint import os
from transformers import AutoModel
checkpoint_path = "./model_checkpoint"
os.environ["HF_HOME"] = "/tmp/hf_cache" # Optional: set cache dir
model = AutoModel.from_pretrained(checkpoint_path) # Fixed after re-download and safetensors upgrade
print("Model loaded successfully") Workaround
Catch RuntimeError during model loading, then fallback to loading from a PyTorch .bin checkpoint if available or trigger an automatic re-download of the safetensors file.
Prevention
Use verified downloads with checksum validation and automate checkpoint integrity checks before loading models to avoid corrupted safetensors files.