Python 3.14+compression.zstdfunction
compression.zstd.finalize_dict()
dictionaries
Turns a raw dictionary (plain bytes with typical fragments) into a full zstd dictionary with a header and statistics from samples.
Syntax
compression.zstd.finalize_dict(zstd_dict, /, samples, dict_size, level)
Use it when you built the dictionary content yourself rather than with train_dict: a raw ZstdDict(is_raw=True) has dict_id 0 and no entropy tables, and after finalize_dict it gets its own id and compresses better. dict_size must fit the original content plus the header part, otherwise ZstdError "Destination buffer is too small". level is the level the statistics are tuned for; compress with that same level afterwards.
Examples
# Python 3.14+
from compression import zstd # → ? нужна сборка CPython с libzstd
samples = [f'{{"id": {i}, "name": "user{i}", "active": true}}'.encode() for i in range(200)]
raw = zstd.ZstdDict(b'{"id": , "name": "user", "active": true}', is_raw=True)
print(raw.dict_id) # → 0 — сырой словарь без id
full = zstd.finalize_dict(raw, samples, 2048, 3)
print(full.dict_id != 0) # → True
one = samples[7]
print(zstd.decompress(zstd.compress(one, zstd_dict=full), zstd_dict=full) == one) # → True
See also
Open in the full glossarydocs.python.orgReport an inaccuracy