Metadata Parsing

no

Original Documentation

Given the simplicity of the format, it’s very simple and efficient to fetch and parse metadata about Safetensors weights – i.e. the list of tensors, their types, and their shapes or numbers of parameters – using small (Range) HTTP requests.

This parsing has been implemented in JS in huggingface.js (sample code follows below), but it would be similar in any language.

Example use case#

There can be many potential use cases. For instance, we use it on the HuggingFace Hub to display info about models which have safetensors weights:

Usage#

From 🤗 Hub, you can get metadata of a model with HTTP range requests instead of downloading the entire safetensors file with all the weights. In this example python script below (you can use any language that has HTTP requests support), we are parsing metadata of gpt2.

import requests # pip install requests
import struct

def parse_single_file(url):
    # Fetch the first 8 bytes of the file
    headers = {'Range': 'bytes=0-7'}
    response = requests.get(url, headers=headers)
    # Interpret the bytes as a little-endian unsigned 64-bit integer
    length_of_header = struct.unpack(' & {
	__metadata__: Record;
};

interface SafetensorsIndexJson {
	weight_map: Record;
}

export type SafetensorsShardedHeaders = Record;

huggingface_hub provides a Python API to parse safetensors metadata. Use get_safetensors_metadata to get all safetensors metadata of a model. Depending on if the model is sharded or not, one or multiple safetensors files will be parsed.

>>> from huggingface_hub import get_safetensors_metadata

# Parse repo with single weights file
>>> metadata = get_safetensors_metadata("bigscience/bloomz-560m")
>>> metadata
SafetensorsRepoMetadata(
    metadata=None,
    sharded=False,
    weight_map={'h.0.input_layernorm.bias': 'model.safetensors', ...},
    files_metadata={'model.safetensors': SafetensorsFileMetadata(...)}
)
>>> metadata.files_metadata["model.safetensors"].metadata
{'format': 'pt'}

# Parse repo with sharded model (i.e. multiple weights files)
>>> metadata = get_safetensors_metadata("bigscience/bloom")
Parse safetensors files: 100%|██████████████████████████████████████████| 72/72 [00:12>> metadata
SafetensorsRepoMetadata(metadata={'total_size': 352494542848}, sharded=True, weight_map={...}, files_metadata={...})
>>> len(metadata.files_metadata)
72  # All safetensors files have been fetched

# Parse repo that is not a safetensors repo
>>> get_safetensors_metadata("runwayml/stable-diffusion-v1-5")
NotASafetensorsRepoError: 'runwayml/stable-diffusion-v1-5' is not a safetensors repo. Couldn't find 'model.safetensors.index.json' or 'model.safetensors' files.

To parse the metadata of a single safetensors file, use parse_safetensors_file_metadata.

Example output#

For instance, here are the number of params per dtype for a few models on the HuggingFace Hub. Also see this issue for more examples of usage.

modelsafetensorsparams
gpt2single-file{ ‘F32’ => 137022720 }
roberta-basesingle-file{ ‘F32’ => 124697433, ‘I64’ => 514 }
Jean-Baptiste/camembert-nersingle-file{ ‘F32’ => 110035205, ‘I64’ => 514 }
roberta-largesingle-file{ ‘F32’ => 355412057, ‘I64’ => 514 }
distilbert-base-german-casedsingle-file{ ‘F32’ => 67431550 }
EleutherAI/gpt-neox-20bsharded{ ‘F16’ => 20554568208, ‘U8’ => 184549376 }
bigscience/bloom-560msingle-file{ ‘F16’ => 559214592 }
bigscience/bloomsharded{ ‘BF16’ => 176247271424 }
bigscience/bloom-3bsingle-file{ ‘F16’ => 3002557440 }
Link last verified June 7, 2026. View original ↗
Source: Safetensors Docs
Link last verified: 2026-02-26