safetensors
Provides functions to read and write safetensors which aim to be safer than their PyTorch counterpart. The format is 8 bytes which is an unsized int, being the size of a JSON header, the JSON header refers the `dtype` the `shape` and `data_offsets` which are the offsets for the values in the rest of the file.
0.8.0
22M downloads/mo
#1966 most downloaded on crates.io
huggingface/safetensors
What this package is like to depend on
Last release 2 months ago
09 Jun 2026
Ships fairly regularly
a new release about every 3 months
Rarely documented
notes for 5 of 22 stable releases
Nothing withdrawn
no release was ever pulled
4 years old
22 releases · first in 2022
2 releases in the last 12 months
see the full history below
Release timeline
22 releases · Dec 2022 to Jun 2026Releases
latest 22-
0.8.009 Jun 2026Release notes
Open source →News
safetensors joins the PyTorch foundation!
Read more on that: https://huggingface.co/blog/safetensors-joins-pytorch-foundation
What's changed
Safetensors 0.8.0 brings direct to Metal loading on Apple Silicon, GIL-free serialization, broader hardware and dtype coverage, and a stronger Python API.
Breaking
The
serializeandserialize_filefunctions now release the GIL during writes, enabling true multithreaded saves from Python. Their input contract has also changed: tensor metadata is now passed via aTensorSpecclass (exported from safetensors) instead of plain dicts, making API more explicit and robust to misinputs. This is a breaking change for anyone calling the low-levelserialize/serialize_fileAPI directly; the high-level wrappers (safetensors.torch,safetensors.numpy,safetensors.paddle) are updated internally and their public API is unchanged.The minimum supported Python version is now 3.10 (was 3.9). Python 3.9 reached end-of-life in October 2025.
TensorIndexer::Narrownow carries astep: NonZeroUsizeparameter, so a slice is nowstart:stop:step. This is a fix as this silent error was hidden behind theStorage::Torchvariant which offloaded slicing logic to torch directly.CI
On the platform side, this release adds Windows ARM64 wheel builds, riscv64 Linux wheels, and CI has been hardened with pinned GitHub Actions SHAs.
Also dropped the anaconda CI we had as there's already an automatic tracker via conda-forge.
New features
- Direct MPS load on Apple Silicon: tensors are directly loaded in an
MTLBufferand handed to the frameworks that support it (only torch atm) via DLPack, skipping needless copies. - New
backendparameter introduced, for the addition of thepreadbackend. We now support loading files viapread(2)syscall instead of just mmap. Useful for specific archs/platforms. get_slicenow handles ellipsis[...]and strided slices[:, ::8]wherever safetensors does the slicing itself (preadfor any framework, MPS, andmmapoutside torch/paddle), which silently dropped the step or rejected...before.- MUSA device support for MooreThreads GPUs.
- New dtype support includes
float8_e4m3fnuzandfloat8_e5m2fnuz(AMD FNUZ FP8 formats). - The reader is now explicitly lenient about leading whitespace in the JSON header, which keeps the door open for future page-aligned writes.
Improvements/perf
- File writes on macOS now use
F_NOCACHEfor direct I/O, yielding roughly 30% fastersave_fileon Apple Silicon. - The packaging dependency has been dropped from the
[torch]extra, replaced by a simplehasattrprobe for efficiency.
What's Changed
- Add arm64 windows support by @finnagin in #678
- feat(backend): support musa backend for MooreThreads GPU by @caizhi-mt in #671
- add gil free serialize_file(serialize_file_threadable) by @TomQunChao in #679
- refactor: remove outdated comment by @McPatate in #685
- feat: add direct io write fast path on macos by @McPatate in #687
- fix: keep dropped reference to tensor moved from gpu to cpu by @McPatate in #689
- feat: bump torch to
2.4by @McPatate in #710 - Contribution guide security by @LysandreJik in #712
- Project Governance by @LysandreJik in #721
- Add riscv64 build, make Linux wheel build matrix more explicit by @threexc in #708
- 🔒 Pin GitHub Actions to commit SHAs by @paulinebm in #727
- fix: do not require
packagingfor Torch 2.3.0+ datatype support by @akx in #705 - Bump urllib3 from 2.4.0 to 2.6.3 in /bindings/python by @dependabot[bot] in #715
- Bump protobuf from 5.29.4 to 5.29.6 in /bindings/python by @dependabot[bot] in #716
- Bump filelock from 3.18.0 to 3.20.3 in /bindings/python by @dependabot[bot] in #717
- Bump wheel from 0.45.1 to 0.46.2 in /bindings/python by @dependabot[bot] in #718
- Bump werkzeug from 3.1.3 to 3.1.6 in /bindings/python by @dependabot[bot] in #719
- Bump pillow from 11.2.1 to 12.1.1 in /bindings/python by @dependabot[bot] in #720
- Bump markdown from 3.8 to 3.8.1 in /bindings/python by @dependabot[bot] in #722
- Bump requests from 2.32.3 to 2.33.0 in /bindings/python by @dependabot[bot] in #725
- Bump pygments from 2.19.1 to 2.20.0 in /bindings/python by @dependabot[bot] in #726
- mark
__version__as str in the stub by @tarekziade in #730 - Add a multithreaded stress test by @ngoldbaum in #637
- Bump torch from 2.7.0 to 2.8.0 in /bindings/python by @dependabot[bot] in #732
- Bump mlx from 0.27.1 to 0.29.4 in /bindings/python by @dependabot[bot] in #731
- feat: add missing test depedencies by @McPatate in #733
- refactor: support reading padded/trailing whitespace header by @McPatate in #686
- feat: add float8_e4m3fnuz and float8_e5m2fnuz dtype support for AMD GPUs by @nathanrchn in #711
- fix(torch): cover f8 & f4 in big endian & unskip tests by @McPatate in #734
- fix: multithreaded test on windows + hard fail on thread err by @McPatate in #735
- Bump pillow from 12.1.1 to 12.2.0 in /bindings/python by @dependabot[bot] in #737
- Bump pytest from 9.0.2 to 9.0.3 in /bindings/python by @dependabot[bot] in #739
- feat!: add
TensorSpecparam toserialize*fns by @McPatate in #738 - feat: bump version to
0.8.0-dev.0by @McPatate in #740 - fix(ci): drop
3.9in conda release by @McPatate in #741 - feat: update RELEASE.md notes by @McPatate in #742
- chore: bump doc-builder SHA for PR upload workflow by @rtrompier in #744
- feat(ci): remove anaconda release and codecov by @McPatate in #745
- feat: upstream
convert.pyscript by @McPatate in #746 - fix(ci): update ghcr url for s390x docker image caching by @McPatate in #747
- fix(python): use checked_mul for F4 last-dim doubling by @willardjansen in #749
- feat: add security audit CI by @McPatate in #752
- Update PyO3 to 0.28 by @ngoldbaum in #751
- fix(ci): pin missing actions dependencies by @McPatate in #755
- Handle mps:0 as input device by @Isalia20 in #754
- fix(python): make SafetensorError picklable by @MukundaKatta in #753
- [MPS] Fast load of the tensors by @Isalia20 in #756
- fix(ci): security audit slack message format by @McPatate in #757
- feat: add
backendwith pread file by @McPatate in #760 - Update free-threading tests for Python 3.14 by @ngoldbaum in #699
- fix: write to tempfile + rename to preserve mmap source by @McPatate in #764
- refactor: replace
_host_alias_storagew/MTLBufferby @McPatate in #767 - feat: bump main to
0.9.0-dev.0by @McPatate in #777 - fix(release): move to pypi trusted publisher OIDC by @McPatate in #778
New Contributors
- @finnagin made their first contribution in #678
- @caizhi-mt made their first contribution in #671
- @TomQunChao made their first contribution in #679
- @LysandreJik made their first contribution in #712
- @threexc made their first contribution in #708
- @paulinebm made their first contribution in #727
- @dependabot[bot] made their first contribution in #715
- @tarekziade made their first contribution in #730
- @ngoldbaum made their first contribution in #637
- @nathanrchn made their first contribution in #711
- @rtrompier made their first contribution in #744
- @willardjansen made their first contribution in #749
- @Isalia20 made their first contribution in #754
- @MukundaKatta made their first contribution in #753
Full Changelog: v0.7.0...v0.8.0
- Direct MPS load on Apple Silicon: tensors are directly loaded in an
-
0.7.019 Nov 2025Release notes
Open source →What's new
complex64supportThis release adds support for storing
complex64tensors.What's Changed
- Revert "Early bailing when keys mismatch (faster). (#602)" by @Narsil in #647
- Support using paddle in safe_open by @zeroRains in #630
- Fix clippy lint. by @Narsil in #648
- Support paddle save/load/save_file/load_file without coverting to numpy by @changeyoung98 in #646
- Fix no-std feature configuration to auto-fallback from std to alloc by @antimora in #651
- Fix big endian (S390X) test by @danieldk in #654
- Fixing get_slice. by @Narsil in #655
- Update hashbrown dependency to 0.16 by @danieldk in #658
- Fix documentation build by @danieldk in #667
- Add
packagingas a dependency for thetorchextra by @danieldk in #666 - Support the complex64 data type by @danieldk in #660
- feat: add header size check at serialization by @McPatate in #669
- Set version to 0.7.0-dev.0 by @danieldk in #670
- Fix release CI issues by @danieldk in #673
New Contributors
- @zeroRains made their first contribution in #630
- @changeyoung98 made their first contribution in #646
- @antimora made their first contribution in #651
- @danieldk made their first contribution in #654
Full Changelog: v0.6.2...v0.7.0
-
0.6.208 Aug 2025Release notes
Open source →What's Changed
- Fixing clippy in 1.89 by @Narsil in #644
- Fixing the version check for uint support in torch. by @Narsil in #643
Full Changelog: v0.6.1...v0.6.2
-
0.6.106 Aug 2025Release notes
Open source →What's Changed
- Rust release upgrade (cache v1 is discontinued). by @Narsil in #627
- Re-adding support for u16, u32, u64. by @Narsil in #629
- Adding _safe_open_handle. by @Narsil in #608
- Fix test_simple.py for 0.6.0 by @xanderlent in #634
- GH action... once again. by @Narsil in #635
- Preparing for patch 6.0.1. by @Narsil in #638
New Contributors
- @xanderlent made their first contribution in #634
Full Changelog: v0.6.0...v0.6.1
-
0.6.023 Jun 2025Release notes
Open source →Biggest changes
-
Added support for FP4/FP6 https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf
Support is still nascent in most frameworks (will require torch 2.8 which isn't released yet, and that will only support fp4 with caveats), however being an openspec supported by hardware manufacturers (and therefore hardware support most likely), it fits the bill of implementing it in safetensors (rather than all custom quantized formats existing in the wild in various frameworks.What FP4/FP6 mean, is that now a element of a tensor may have a non byte-aligned size/access. If you store a single fp4, then there is 4 bit on that byte that is outside of the spec. For now, safetensors library will simply raise MisalignedByte exception whenever an operation leads to unused/unaligned bits within a byte. Since most tensors are larger power of 2s, this shouldn't come up too often in practice. Raising an exception now means we have freedom later to actually implement a behavior which could align with tensor libraries.
In that regard
Dtype.size()is now deprecated, as it returns the size of the dtype in bytes, and we now favorbitsize()and it's up to users for now to handle something likelen * bitsize() / 8(and verifying the division is acceptable)On that note, and for Pytorch users specifically, torch doesn't actually implement fp4, it has a dtype called
float4_e2m1fn_x2which actually represents 2 fp4. This is why torch shouldn't have any alignement problems for now (but cannot implement fp6). But that also means that the shape of a [2, 2] tensor for floa4, contains actually 8 values.safetensorswill actuallly silently cast a tensor of shape,[x, y, ...z]into[x, y, ..., z/2], using the last dimension to "swallow" , thex2contained within the types. Again, there is no definite behavior just yet, so this might be subject to change.
What's Changed
- Add safestructures to Featured Projects by @rachthree in #570
- Fixing benchmarks. by @Narsil in #580
- load_file: load tensors ordered by their offsets by @yousong in #571
- Updating python bench ? by @Narsil in #587
- fix(benchmark.rs): "serialize" and "deserialize" typo by @Marco-Christiani in #585
- Add onnx-safetensors to the projects list in documentation by @justinchuby in #581
- Pass device to torch.asarray in get_tensor by @mickvangelderen in #588
- Making py311 the default. by @Narsil in #589
- Fix test by @Narsil in #590
- Fix test by @Narsil in #591
- [WIP] Enabling free-threaded python (without warning). + pyo3 0.24 by @Narsil in #592
- Fix the bench action ? by @Narsil in #603
- fix typo in serialize_file doc-string by @LVivona in #594
- Remove useless code by @cyc4188 in #597
- Early bailing when keys mismatch (faster). by @Narsil in #602
- Fixing the ml_dtypes potentially missing. by @Narsil in #605
- Adding the License to wheels. by @Narsil in #606
- Adding a public API for metadata. by @Narsil in #618
- Update dependencies by @H2CO3 in #614
- Better error handling through improved
DisplayandErrorimpls by @H2CO3 in #616 - Do not force
&Option<T>in public API; useOption<&T>instead by @H2CO3 in #617 - Adding support for MXFP4,6. by @Narsil in #611
- Bumping version because of breaking changes. by @Narsil in #619
- Adding data_len as public API for metadata (to fetch the size of the by @Narsil in #620
- Simplify code and make it more robust by @H2CO3 in #615
- Rename. by @Narsil in #621
- Fixup into pyobject. by @Narsil in #622
- Adding a failing test on the device cast. by @Narsil in #623
New Contributors
- @rachthree made their first contribution in #570
- @yousong made their first contribution in #571
- @Marco-Christiani made their first contribution in #585
- @justinchuby made their first contribution in #581
- @mickvangelderen made their first contribution in #588
- @LVivona made their first contribution in #594
- @cyc4188 made their first contribution in #597
- @H2CO3 made their first contribution in #614
Full Changelog: v0.5.3...v0.6.0
-
-
0.5.326 Feb 2025Nothing published for this version
-
0.5.208 Jan 2025Nothing published for this version
-
0.5.107 Jan 2025Nothing published for this version
-
0.5.002 Jan 2025Nothing published for this version
-
0.4.505 Sep 2024Nothing published for this version
-
0.4.405 Aug 2024Nothing published for this version
-
0.4.315 Apr 2024Nothing published for this version
-
0.4.223 Jan 2024Nothing published for this version
-
0.4.127 Nov 2023Nothing published for this version
-
0.4.006 Oct 2023Nothing published for this version
-
0.3.323 Aug 2023Nothing published for this version
-
0.3.207 Aug 2023Nothing published for this version
-
0.3.125 Apr 2023Nothing published for this version
-
0.3.005 Mar 2023Nothing published for this version
-
0.2.816 Jan 2023Nothing published for this version
-
0.2.727 Dec 2022Nothing published for this version
-
0.2.613 Dec 2022Nothing published for this version