torch.serialization.is_safetensors
function is_safetensors(data: Uint8Array): booleanCheck if data is in safetensors format.
Performs a quick header check to determine if the data appears to be in safetensors format. Uses magic bytes and header validation for fast detection without parsing the full file. Useful for:
- Auto-detecting file format when extension is unknown
- Choosing appropriate deserialization function
- Validating uploaded files
- Stream format detection
- Fast check: Only reads header (first ~16 bytes), not full file
- Magic bytes: Uses safetensors header format to detect
- Incomplete detection: Returns false for truncated files
- Not foolproof: Could theoretically have false positives
- Requires data: Needs at least 16 bytes to check
Parameters
dataUint8Array- Uint8Array containing file data to check
Returns
boolean– true if data appears to be safetensors format, false otherwiseExamples
// Check format before deserializing
const data = new Uint8Array(await response.arrayBuffer());
if (torch.is_safetensors(data)) {
const { tensors } = await torch.deserialize_from_safetensors(data);
} else {
const tensors = await torch.deserialize_from_zip(new Blob([data]));
}
// Auto-detect from fetch
const response = await fetch('model.bin'); // Unknown format
const buffer = await response.arrayBuffer();
const data = new Uint8Array(buffer);
const format = torch.is_safetensors(data) ? 'safetensors' : 'pytorch';See Also
- [PyTorch N/A (safetensors is independent format)](https://pytorch.org/docs/stable/generated/N/A .html)
- deserialize_from_safetensors - Deserialize safetensors data
- load - Higher-level load with auto-detection