refusal rate
Subscribe

GGUF vs safetensors explained: what each file contains, which runtimes load which, why safetensors replaced pickle, how to convert, and which file to download.

[ fig. 01 — explainer ]48×5
[ fig. 01 — explainer ]16×5

Short answer

GGUF and safetensors are model file formats, not quantization methods. A GGUF file is self-contained: tensors plus architecture, tokenizer and chat template in one file, built for llama.cpp, Ollama and LM Studio. A safetensors file holds only tensors and a JSON index; config and tokenizer sit beside it in the repo. Download GGUF for llama.cpp-based tools, safetensors for vLLM, SGLang, Transformers, MLX and fine-tuning.

GGUF contents
Tensors + metadata + tokenizer
Safetensors contents
Tensors + JSON index only
Safetensors audit
Trail of Bits, May 2023
GGUF spec version
v3

Key takeaways

  1. GGUF is a single-file inference format from the llama.cpp project; safetensors is Hugging Face's tensor-only storage format.
  2. The format does not decide speed or quality. The quantization level and the runtime do.
  3. Safetensors exists because PyTorch's pickle-based .bin files can run code when loaded; a 2023 Trail of Bits audit found no arbitrary-code-execution flaw in safetensors.
  4. Conversion is mostly one way: convert_hf_to_gguf.py turns a safetensors repo into GGUF, then llama-quantize shrinks it.
  5. vLLM still calls GGUF support highly experimental and under-optimized, so GPU servers should pull the safetensors repo.

The short version of GGUF vs safetensors: they answer different questions. Safetensors is how a model is stored and shared after training. GGUF is how a model is packaged to run in llama.cpp and the tools built on it. Most models are released as safetensors first, and someone converts them to GGUF later. Which one you download depends almost entirely on the program that will load it.

What is the difference in one table?

GGUF bundles everything a runtime needs into one file, while safetensors stores only tensors and leaves the rest to sibling files.

GGUFSafetensors
Made byGeorgi Gerganov / llama.cpp projectHugging Face
PurposeInference with GGML-based executorsSafe, fast tensor storage for any framework
What one file holdsTensors, architecture, tokenizer, chat template, quant typeTensors and a JSON index; optional string-only metadata
Typical precisionF16/BF16 or quantized (Q8_0, Q4_K_M, IQ types)BF16/FP16, or packed AWQ/GPTQ/FP8 weights
Typical repo shapeSeveral .gguf files, one per quant levelSharded model-0000N-of-0000M.safetensors plus config.json and tokenizer files
Loaded natively byllama.cpp, Ollama, LM Studio, GPT4AllTransformers, vLLM, SGLang, MLX, diffusers
Executes code on loadNoNo

The Hugging Face Hub docs put it in one line: “unlike tensor-only file formats like safetensors… GGUF encodes both the tensors and a standardized set of metadata” (HF Hub).

What is inside a GGUF file vs a safetensors file?

A GGUF file is a header, a metadata store and the tensors; a safetensors file is a size prefix, a JSON index and a raw byte buffer.

[ fig. 01 — Byte layout of each format. Sources: GGUF spec, safetensors README ]
A GGUF file carries the tokenizer, chat template and architecture metadata inside the file, while a safetensors file holds only a JSON tensor index and raw bytes, so config and tokenizer live in separate repo files.

Per the GGUF spec, a GGUF file starts with the magic bytes GGUF, a version number (currently 3) and a tensor count. Then come typed key/value pairs. Standard keys include general.architecture (such as llama), general.file_type (the type of most tensors), tokenizer.ggml.tokens (the vocabulary) and tokenizer.chat_template (a Jinja template). After that come tensor descriptors with name, shape, ggml type and offset, padding to an alignment boundary (32 bytes by default), and the tensor data. The spec lists three goals: single-file deployment with no external files, mmap compatibility for fast loading, and extensibility without breaking old files. GGUF replaced the earlier GGML, GGMF and GGJT formats.

The safetensors format is simpler on purpose. The first 8 bytes are an unsigned little-endian integer giving the header size. The header is UTF-8 JSON mapping each tensor name to its dtype, shape and data_offsets. An optional __metadata__ key may hold extra data, but “all values must be strings.” The rest is a byte buffer that must be fully indexed with no holes. Architecture, hyperparameters and tokenizer live in config.json, tokenizer.json and tokenizer_config.json next to the weights.

That difference is why a GGUF “just loads” and a safetensors file on its own can’t be run. It’s also why chat template bugs behave differently. In a GGUF the template is baked in at conversion time. In a safetensors repo it’s read from tokenizer_config.json on every load.

Which runtimes load which format?

llama.cpp-family tools want GGUF; Python and GPU serving stacks want safetensors, and each side has only partial support for the other.

RuntimeNative formatThe other format
llama.cppGGUF (“requires the model to be stored in the GGUF file format”)Convert first with convert_*.py scripts
OllamaIts own library, built on GGUFImports a safetensors directory with FROM <dir> in a Modelfile
LM StudioGGUF via llama.cpp on Mac, Windows and LinuxMLX models on Apple silicon (MLX repos are safetensors)
vLLMHugging Face safetensors reposGGUF via the out-of-tree vllm-gguf-plugin; “highly experimental and under-optimized”
SGLangHugging Face safetensors reposGGUF listed as supported on NVIDIA GPUs and Ascend NPUs, not AMD
TransformerssafetensorsLoads GGUF with gguf_file=; most architectures are dequantized at load
MLX / mlx-lmsafetensors shards in MLX layoutNot GGUF (see GGUF vs MLX)

Sources: llama.cpp models doc, Ollama import, LM Studio docs, vLLM GGUF, SGLang quantization, Transformers GGUF. For which engine to pick in the first place, see Ollama vs llama.cpp vs vLLM.

Two details matter in practice. vLLM recommends “using the tokenizer from base model instead of GGUF model,” because converting the tokenizer out of a GGUF “is time-consuming and unstable.” Transformers’ GGUF loader turns quantized weights back into a dense model in memory for most architectures, so a 4-bit GGUF does not stay 4-bit there.

Is GGUF or safetensors faster or higher quality?

Neither format is inherently faster or higher quality; the quantization level and the runtime decide both.

The confusion comes from what people usually compare. “GGUF” on the Hub usually means a 4- to 6-bit quant run by llama.cpp on a consumer GPU or CPU. “Safetensors” usually means the original BF16 weights, or an AWQ/GPTQ quant served by vLLM on a datacenter GPU. Those differ in bits per weight and in engine, not in container. A GGUF can hold full BF16 weights, and a safetensors repo can hold 4-bit AWQ weights. What Q4_K_M costs in quality is covered in GGUF quantization explained, and the GPU-side 4-bit methods in AWQ vs GPTQ.

Where format does matter is loading. GGUF is designed for mmap, so llama.cpp can map the file instead of copying it into memory. Safetensors supports zero-copy on CPU when the file is in the page cache, and lazy loading of single tensors. Its README cites BLOOM loading on 8 GPUs dropping “from 10mn with regular PyTorch weights down to 45s” (safetensors). That comparison is against pickle, not GGUF. It also concedes “no format is really zero-copy in ML,” since GPU memory always needs a copy.

Why is safetensors safer than .bin and pickle?

Safetensors can only describe tensors, while PyTorch’s older .bin/.pt checkpoints are pickle files that can run code when loaded.

Hugging Face’s audit announcement says PyTorch “uses pickle under the hood, which is inherently unsafe.” A crafted file could “give full control of a user’s computer to an attacker without the user’s knowledge.” The current PyTorch docs still warn that torch.load() “uses an unpickler under the hood. Never load data from an untrusted source,” even with weights_only=True as the default (PyTorch).

The safetensors design closes that path:

  • No code, only data. The header is JSON with a 100 MB cap, and the body is raw bytes.
  • No overlapping offsets, so loading never needs more memory than the file size.
  • No holes in the buffer, which blocks polyglot files that hide a second payload.
  • Audited. Trail of Bits reviewed it in 2023, commissioned by Hugging Face with EleutherAI and Stability AI. It found “no critical security flaw leading to arbitrary code execution.” The fixes were spec clarifications and polyglot validation.

GGUF is also a data-only format, so the pickle risk does not apply to it. The open risk with either format is the software that parses it. A file is only as safe as the loader reading it.

On the safetensors vs GGUF vs ONNX question: ONNX is a graph format, not just weights. In the safetensors README’s comparison, Protobuf (ONNX) is safe but lacks zero-copy, lazy loading and has a file size limit. It’s rarely the file you want for LLM chat models.

How do you convert safetensors to GGUF?

Run llama.cpp’s convert_hf_to_gguf.py on the safetensors repo to get a 16-bit GGUF, then quantize it with llama-quantize.

The quantize README describes two phases:

bash
pip install -r requirements.txt
python convert_hf_to_gguf.py ./my-model --outtype bf16 --outfile my-model-bf16.gguf
./build/bin/llama-quantize my-model-bf16.gguf my-model-Q4_K_M.gguf Q4_K_M

Notes from the source:

  • --outtype defaults to auto, “the highest-fidelity 16-bit float type.” That’s fine for models shipped in 16-bit.
  • --remote <repo> reads safetensors over HTTP “without downloading to disk.” It’s marked experimental, and config and tokenizer files are still downloaded.
  • The requirements install Transformers 4, but newer models such as Gemma 4 need Transformers 5.
  • Don’t requantize an already quantized GGUF unless you must. --allow-requantize “can severely reduce quality compared to quantizing from 16bit or 32bit.”

No local setup? The GGUF-my-repo Space converts and quantizes on Hugging Face, synced from llama.cpp main every 6 hours. Ollama skips the manual step for supported architectures: point a Modelfile at the safetensors folder and run ollama create (Ollama import).

Going back from GGUF to safetensors is possible but lossy. Transformers can load a GGUF and dequantize it into a dense model, which can then be saved as safetensors. Precision lost to quantization does not come back, so always start fine-tuning or merging from the original safetensors.

What do developers report in practice?

Developers mostly hit the format split as a workflow tax: two runtimes, two copies of the weights.

  • Two stacks, two formats. One Hacker News commenter moving between llama-server and vLLM notes the former uses GGUF and the latter safetensors. They call sharing weights between them, with conversion, “a hassle” (HN).
  • Same model, two repos. In a thread about which upload to use, another commenter’s rule was the Unsloth GGUF for llama.cpp, the original repo for vLLM (HN). A reply added that the GGUF saves you producing your own quant from the safetensors (HN).
  • Convenience is the GGUF selling point. One developer argued GGUFs aren’t technically needed but are “way more convenient” than safetensors plus setting up the Jinja prompt by hand (HN). That’s the bundled chat template at work.
  • New releases arrive as safetensors only. When a Gemma 4 checkpoint appeared with no .gguf, one commenter wrote they’d have to convert the safetensors themselves “like it’s 2023 again” (HN).
  • Load speed depends on setup. Pushing back on slow vLLM startup, one user said vLLM can load safetensors at over 5 GB/s with a persistent kernel compile cache (HN).
  • Packed quants confuse the Hub. 4-bit weights packed into safetensors often show as u8 arrays, so the Hub UI counts each as half a weight, one commenter explained (HN). Check parameter counts on quantized safetensors repos against the model card.

These are single community reports, not measurements, but they match what the docs say about each runtime.

Which file should you download?

Pick the runtime first; the file follows from it.

[ fig. 02 — Decision tree by runtime. Sources: llama.cpp, vLLM, LM Studio and Ollama docs ]
Pick the file by runtime: llama.cpp, Ollama and LM Studio want a GGUF; vLLM, SGLang and Transformers want the safetensors repo; MLX wants an MLX safetensors repo; fine-tuning starts from safetensors.
  1. llama.cpp, Ollama, or LM Studio on Windows/Linux: download one .gguf from a GGUF repo, sized to your VRAM plus context. With -hf user/model:quant, llama.cpp fetches it directly. If the GGUF is split into parts, download all of them into the same folder.
  2. vLLM or SGLang on a GPU server: download the safetensors repo, either BF16 or a pre-quantized AWQ, GPTQ or FP8 variant. Only use GGUF here if it’s the only file that exists.
  3. Mac with MLX: use an MLX-converted repo, which is safetensors underneath.
  4. Fine-tuning, LoRA, merging or weight edits such as abliteration: start from the original safetensors and convert to GGUF at the end.
  5. A repo offers only pytorch_model.bin: prefer a safetensors upload of the same model. Load pickle files only from publishers you trust.

FAQ

Is GGUF better than safetensors?

No. GGUF is better for llama.cpp-based local inference because it is self-contained and supports llama.cpp’s quant types. Safetensors is better for GPU serving, training and fine-tuning. Neither is better in general.

Is GGUF the same as quantized?

No. GGUF is a container that can hold F16, BF16 or quantized tensors. Most GGUF downloads are quantized, which is why people conflate the two.

Can vLLM run GGUF models?

Yes, through the vllm-gguf-plugin, but vLLM’s docs call it highly experimental and under-optimized. They recommend passing the base model’s tokenizer with --tokenizer. For production, use safetensors.

Can Ollama use safetensors?

Yes, for supported architectures. Put the weights directory in a Modelfile’s FROM line and run ollama create. GGUF files are imported as-is, and Ollama does not quantize them on import.

Are GGUF files safe to download?

GGUF is data-only, like safetensors, so it can’t carry pickle-style code execution. Download from known publishers anyway, and keep your runtime updated, since the risk sits in the parser.

Sources (9)
  1. GGUF specification, ggml-org/ggml docs/gguf.md
  2. huggingface/safetensors README (format, comparison table, security notes)
  3. Safetensors audited as really safe and becoming the default, Hugging Face blog (May 23, 2023)
  4. GGUF, Hugging Face Hub documentation
  5. Obtaining and quantizing models, llama.cpp docs/models.md
  6. llama.cpp quantize tool README
  7. GGUF, vLLM documentation
  8. GGUF, Hugging Face Transformers documentation
  9. Importing a model, Ollama documentation

Keep reading