Safetensors is a safe, fast container for a model's raw tensors, used with PyTorch and by Hugging Face Transformers, Diffusers, MLX and many other projects. GGUF is a single-file format for llama.cpp and the apps built on it (Ollama, LM Studio), holding usually quantized weights together with the tokenizer and all the metadata needed to run the model.
Rule of thumb: if you're training, fine-tuning or running a model from Python code, you want safetensors. If you're running a model locally on a laptop or desktop with llama.cpp, Ollama or LM Studio, you want GGUF.
GGUF vs safetensors at a glance
| Safetensors | GGUF | |
|---|---|---|
| Made by | Hugging Face | The ggml / llama.cpp project |
| Contains | Tensors only, plus an optional string-to-string metadata map | Tensors plus typed metadata: architecture, context length, tokenizer and more |
| Typical precision | Full or half precision (F32, F16, BF16) | Quantized (Q8_0, Q4_K_M and many others), or F16/BF16 |
| Files per model | Weights in one or more shards, plus separate config and tokenizer files | Designed as one self-contained file |
| Main users | PyTorch, Transformers, Diffusers, MLX, Candle | llama.cpp, Ollama, LM Studio, GPT4All |
| Header | 8-byte length, then a JSON header | Binary: magic GGUF, version, counts, then key-value metadata |
| Loading | Zero-copy and lazy loading supported | Designed for mmap |
| Runs code on load? | No | No |
Measured: the same model in both formats
We compared the official Qwen3-0.6B release on Hugging Face (safetensors) with ggml-org's GGUF conversions of it, reading file sizes from the Hub and fetching only the first few kilobytes of each file to parse the headers (as of October 2026):
| File | Format and precision | Size | Tensors | Metadata |
|---|---|---|---|---|
model.safetensors | Safetensors, BF16 | 1,503,300,328 bytes (1.50 GB) | 311 | {"format": "pt"}; tokenizer and config in separate files |
Qwen3-0.6B-BF16.gguf | GGUF, BF16 | 1,509,347,552 bytes (1.51 GB) | Not read | Includes tokenizer |
Qwen3-0.6B-Q8_0.gguf | GGUF, 8-bit | 804,753,632 bytes (0.80 GB) | 311 | 36 keys, tokenizer included |
Qwen3-0.6B-Q4_0.gguf | GGUF, 4-bit | 428,970,080 bytes (0.43 GB) | Not read | Includes tokenizer |
At the same BF16 precision the two formats are nearly the same size; the GGUF is about 6 MB larger, which fits with it carrying the tokenizer inside. The big size difference people associate with GGUF comes from quantization, not from the container: the Q4_0 file is less than a third of the BF16 one. For what those quantization names mean, see what is LLM quantization and GGUF.
Inside a safetensors file
The safetensors repository describes the layout: 8 bytes holding an unsigned little-endian 64-bit integer N (the header size), then N bytes of UTF-8 JSON, then the raw tensor bytes. The JSON maps each tensor name to its dtype, shape and data_offsets within the byte buffer, and allows one special key, __metadata__, holding string-to-string pairs only. The header is limited to 100 MB to prevent parsing extremely large JSON.
That simplicity means you can read one with the Python standard library. We wrote a tiny file by hand and read it back:
import json, struct
header = {"weight": {"dtype": "F32", "shape": [2, 2], "data_offsets": [0, 16]},
"__metadata__": {"format": "pt"}}
hb = json.dumps(header, separators=(",", ":")).encode()
with open("tiny.safetensors", "wb") as f:
f.write(struct.pack("<Q", len(hb))) # 8-byte header length
f.write(hb) # JSON header
f.write(struct.pack("<4f", 1.0, 2.0, 3.0, 4.0)) # 16 bytes of tensor data
Output of reading it back:
file bytes 117 header length 93
5d 00 00 00 00 00 00 00
{"weight":{"dtype":"F32","shape":[2,2],"data_offsets":[0,16]},"__metadata__":{"format":"pt"}}
(1.0, 2.0, 3.0, 4.0)
The real Qwen3-0.6B file has the same structure at scale: a 35,552-byte JSON header listing 311 BF16 tensors. The first entry is lm_head.weight, shape [151936, 1024], occupying bytes 0 to 311,164,928 of the buffer. A loader can jump straight to any tensor's offset without reading the rest, which is what makes lazy and partial loading cheap. The documentation shows safe_open() with get_slice() for exactly that, for example to load only part of a tensor onto each of several GPUs.
Inside a GGUF file
The GGUF specification describes a header with the magic bytes GGUF (0x47 0x47 0x55 0x46), a format version, the tensor count and the metadata key-value count, followed by the metadata, the tensor descriptions (name, dimensions, type, offset) and the aligned tensor data. The first 24 bytes of the Qwen3-0.6B Q8_0 file:
47 47 55 46 03 00 00 00 37 01 00 00 00 00 00 00 24 00 00 00 00 00 00 00
That decodes as magic GGUF, version 3, 311 tensors (0x137) and 36 metadata entries (0x24), all little-endian. The first metadata keys, parsed with a few lines of Python using the types the spec defines:
general.architecture = qwen3
general.type = model
general.sampling.top_k = 20
general.sampling.top_p = 0.949999988079071
general.sampling.temp = 0.6000000238418579
general.name = Qwen3-0.6B
general.size_label = 752M
general.license = apache-2.0
Further keys hold the tokenizer vocabulary and settings, so the file carries everything llama.cpp needs. The spec states this as a design goal: a model in one file, compatible with mmap, and extensible with new metadata without breaking existing readers. GGUF replaced the earlier GGML, GGMF and GGJT formats, and its main change was swapping a fixed list of hyperparameters for this key-value section.
Why "safe" matters: the pickle problem
Safetensors exists because PyTorch saves checkpoints with Python's pickle by default. The Python documentation is blunt: the pickle module is not secure, and malicious pickle data can execute arbitrary code during unpickling. The safetensors project's stated goal was to remove the need for pickle in PyTorch. Its comparison table marks pickle as unsafe and safetensors as safe, meaning you can load a file downloaded from anywhere without it running code.
GGUF has the same property for a different reason: its specification defines only typed metadata values and tensor data, with no mechanism for embedding executable code. Both formats are a large improvement over loading untrusted pickle checkpoints.
Converting between them
The common direction is safetensors to GGUF, when you want to run a model locally. The llama.cpp quantize README shows the two steps:
python convert_hf_to_gguf.py --outfile gemma-4-E2B-it-bf16.gguf --outtype bf16 --remote google/gemma-4-E2B-it
./build/bin/llama-quantize gemma-4-E2B-it-bf16.gguf gemma-4-E2B-it-Q4_K_M.gguf Q4_K_M
The first command converts the Hugging Face model to a 16-bit GGUF; the second quantizes it. The README warns that re-quantizing an already quantized file "can severely reduce quality compared to quantizing from 16bit or 32bit", so always quantize from the full-precision conversion. Without a local setup, the Hugging Face Hub docs point to the ggml-org/gguf-my-repo Space, which converts and quantizes a Hub model for you. For running the result, see getting started with llama.cpp.
The reverse is rarely what you want. Fine-tuning tools expect full-precision safetensors, and a quantized GGUF has already lost precision. If you plan to fine-tune (for example with LoRA), start from the original safetensors release, then convert the merged result to GGUF at the end.
Which one to download
- Running locally with Ollama, LM Studio or llama.cpp: GGUF. Choose the quantization by memory: the file size is roughly the memory the weights need, plus room for the context. See how much VRAM you need.
- Using Transformers, Diffusers or PyTorch code: safetensors, together with the repository's config and tokenizer files.
- Fine-tuning: safetensors at the original precision.
- Serving with an inference server: check its documentation for the formats and quantizations it supports.
- A repository offers only PyTorch pickle checkpoints: load them only from publishers you trust, because unpickling can run code.
Frequently asked questions
- Can Ollama or LM Studio load safetensors files directly?
- Their usual format is GGUF, and the Hugging Face Hub docs list llama.cpp, LM Studio, GPT4All and Ollama as tools that use GGUF. If a model is only published as safetensors, look for a GGUF conversion on the Hub (filter by the GGUF library tag) or convert it yourself with llama.cpp's convert_hf_to_gguf.py.
- Can I convert GGUF back to safetensors?
- Converting a quantized GGUF back gives you the quantized weights, not the original precision, because quantization throws information away. If you need full-precision safetensors for training or fine-tuning, download the original model rather than reconstructing it from a GGUF.
- Is GGUF safe to download?
- GGUF is a data format with a fixed binary layout, not a pickle, so loading it doesn't run embedded Python code the way an old PyTorch pickle checkpoint can. As with any file parser, keep the software that reads it up to date, and prefer models from publishers you trust.
- Why are there several GGUF files for one model?
- Each file is the same model quantized to a different precision, such as BF16, Q8_0 or Q4_K_M. Lower-bit files are smaller and faster but lose some quality. Pick the largest one that fits in your memory with room for the context window.