dict-hash

raw JSON →
1.3.7 verified Mon Apr 27 auth: no python

Python package to hash dictionaries using default hash, md5, sha256 and more. Supports nested structures, pandas, numpy, polars, datetime, and custom hash functions. Active development with recent additions including approximate hashing and support for timedelta. Current version: 1.3.7.

pip install dict-hash
error ModuleNotFoundError: No module named 'dict_hash'
cause Package not installed or installed as 'dict-hash' but imported incorrectly.
fix
Install with 'pip install dict-hash' and import using 'from dict_hash import ...'.
error ValueError: Invalid hash algorithm: md5
cause In older versions, hash function names were not exported; direct use of string names may fail.
fix
Update to latest version and use 'from dict_hash import md5' then call 'md5(my_dict)'.
error TypeError: Object of type DataFrame is not JSON serializable
cause Trying to hash a pandas DataFrame without pandas support installed or using a custom serializer.
fix
Install pandas: 'pip install pandas'. Then use 'from dict_hash import sha256; sha256(my_dict)'.
gotcha The library does not guarantee deterministic hashing across different Python versions (e.g., default hash seeds). Use sha256 or another cryptographic hash for consistency.
fix Use 'sha256' or 'md5' instead of 'dict_hash' for a stable hash.
breaking Version 1.3.0 changed the handling of NumPy objects; older code may produce different hashes if pandas is not installed.
fix Ensure pandas is installed if you hash dictionaries containing pandas objects, or update code to handle NumPy-only dicts separately.
deprecated The 'hash_func' parameter in earlier versions is deprecated; use named functions like 'sha256' instead.
fix Replace 'dict_hash(my_dict, hash_func='sha256')' with 'from dict_hash import sha256; sha256(my_dict)'.

Compute a SHA256 hash of a dictionary.

from dict_hash import sha256

my_dict = {"a": 1, "b": [2, 3], "c": {"d": "text"}}
hash_value = sha256(my_dict)
print(hash_value)  # Example: b"..."