Safetensors File Integrity Checker — Verify LoRA & Checkpoint Files Using Python
Introduction
Safetensors files are widely used in AI image generation workflows, including LoRA models and checkpoints. A damaged or incomplete file may cause loading errors or unexpected problems when using an AI model.
The Safetensors File Integrity Checker is a simple Python tool designed to verify whether a Safetensors file can be read correctly and generate a SHA-256 checksum for file identification.
This tool helps users inspect their model files without modifying the original data.
Features
Check whether a Safetensors file exists.
Verify that the file can be opened and read.
Read tensor names and metadata.
Generate a SHA-256 checksum.
Display file size and basic information.
Detect common file-reading errors.
Works with LoRA and checkpoint files that use the Safetensors format.
Requirements
Install Python and the Safetensors library:
pip install safetensors
The script also uses Python's built-in hashlib and pathlib modules.
Python Script
Copy the following code and save it as safetensors_integrity_.
import hashlib
from pathlib import Path
from safetensors import safe_open
def calculate_sha256(file_path):
sha256 = hashlib.sha256()
with open(file_path, "rb") as file:
while chunk := file.read(1024 * 1024):
sha256.update(chunk)
return sha256.hexdigest()
def check_safetensors(file_path):
file_path = Path(file_path)
print("=" * 60)
print("SAFETENSORS FILE INTEGRITY CHECKER")
print("=" * 60)
if not file_path.exists():
print("Status: File not found.")
return
if not file_path.is_file():
print("Status: The selected path is not a file.")
return
file_size = file_path.stat().st_size
file_size_mb = file_size / (1024 * 1024)
print(f"File: {file_path.name}")
print(f"Size: {file_size_mb:.2f} MB")
print("\n[SHA-256 CHECKSUM]")
try:
checksum = calculate_sha256(file_path)
print(checksum)
except OSError as error:
print(f"Unable to calculate checksum: {error}")
return
print("\n[SAFETENSORS VALIDATION]")
try:
with safe_open(
str(file_path),
framework="pt",
device="cpu"
) as model:
tensor_names = list(model.keys())
metadata = model.metadata()
print("Status: File opened successfully.")
print(f"Tensor count: {len(tensor_names)}")
if metadata:
print("Metadata: Available")
else:
print("Metadata: Not available")
print("\n[VALIDATION RESULT]")
print("The file can be opened using Safetensors.")
except Exception as error:
print("Status: Unable to read the Safetensors file.")
print(f"Error: {error}")
print("\n" + "=" * 60)
print("CHECK COMPLETE")
print("=" * 60)
if __name__ == "__main__":
file_path = input(
"Enter the path to your Safetensors file: "
).strip().strip('"')
check_safetensors(file_path)
How to Use
Install Python on your computer.
Install the Safetensors library.
Save the script as safetensors_integrity_.
Open a terminal in the script's directory.
Run the script:
python safetensors_integrity_checker.py
Enter the full path to your LoRA or checkpoint file.
Wait for the script to complete the validation.
Review the checksum and file-reading results.
Example Output
============================================================
SAFETENSORS FILE INTEGRITY CHECKER
============================================================
File: example_lora.safetensors
Size: 144.25 MB
[SHA-256 CHECKSUM]
a1b2c3d4e5f678901234567890abcdef1234567890abcdef1234567890abcdef
[SAFETENSORS VALIDATION]
Status: File opened successfully.
Tensor count: 128
Metadata: Available
[VALIDATION RESULT]
The file can be opened using Safetensors.
============================================================
CHECK COMPLETE
============================================================
Note: The checksum, file size, and tensor count in this example are illustrative. Actual results depend on the selected file.
Understanding the SHA-256 Checksum
A SHA-256 checksum is a unique-looking digital fingerprint calculated from the contents of a file.
It can be useful for comparing two files:
If two files have the same SHA-256 checksum, they have the same contents with respect to that checksum calculation.
If two files have different checksums, their contents differ.
A checksum alone does not prove that a file is safe or that its model weights are correct.
You can use the checksum to help identify duplicate files or compare a downloaded model against a checksum provided by a trusted source.
Understanding File Validation
This tool checks whether the Safetensors library can open and read the file header, metadata, and tensor names.
A successful read indicates that the file passed the operations performed by this script. It does not guarantee that the model is compatible with every AI framework or that all model weights are semantically correct.
If the script reports an error, the file may be incomplete, corrupted, unsupported by the installed library, or affected by another file-related issue.
Important Notes
Keep a backup of your original LoRA and checkpoint files.
Do not overwrite model files during the checking process.
A matching checksum is useful for verifying file identity, not for proving that the model is trustworthy.
Use files from reliable sources and keep your Python dependencies updated.
Conclusion
The Safetensors File Integrity Checker provides a simple way to inspect model files using Python. It combines SHA-256 checksum generation with Safetensors file-reading validation to help users understand their model files and identify potential reading problems.
This tool can be useful for AI image-generation workflows involving LoRA models, checkpoints, and other Safetensors-based files.



