Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/ismael-sarmiento/kimera_python/llms.txt

Use this file to discover all available pages before exploring further.

The build_hash function produces a stable, deterministic MD5 hex digest for any Python object by encoding both str(_obj) and str(type(_obj)) before hashing. Combining the string representation with the type is critical for collision safety — without the type component, the integer 42, the float 42.0, and the string '42' would all produce the same hash because str() renders them identically. Use build_hash wherever you need a consistent, reproducible key: cache key generation, request deduplication, content-addressable storage, or fingerprinting intermediate pipeline results.

Import

from components.tools.hash.md5 import build_hash

Function Signature

def build_hash(_obj: object) -> str
_obj
object
required
Any Python object. The function calls str(_obj) to obtain the object’s representation and str(type(_obj)) to capture its type. For custom classes, define __str__ or __repr__ to ensure consistent output across sessions.
Returns: A 32-character lowercase hexadecimal string — the MD5 digest of the combined representation and type encoding.

How the Hash Is Built

Internally the function concatenates two encoded byte strings and passes them to hashlib.md5:
hashlib.md5(str(_obj).encode() + str(type(_obj)).encode()).hexdigest()
The result is deterministic for any object whose str() output is stable. For custom classes that do not define __str__ or __repr__, the default object.__repr__ includes the memory address — which changes between interpreter runs — so the hash will not be reproducible across sessions.

Usage Examples

The following examples demonstrate how build_hash handles primitives, collections, and custom objects. Notice that values that look identical in string form — such as 42 (int) and '42' (str) — produce distinct hashes because their types differ.
from components.tools.hash.md5 import build_hash

# Primitives — same value, different types, different hashes
print(build_hash('hello'))      # consistent hex string
print(build_hash(42))           # different from build_hash('42')
print(build_hash(42.0))         # different from build_hash(42)

# Collections
print(build_hash([1, 'a']))     # consistent for same list
print(build_hash({'x': 1}))    # consistent for same dict

# Custom objects — stable hash requires a stable __str__
class Point:
    def __init__(self, x, y):
        self.x, self.y = x, y

    def __str__(self):
        return f'Point({self.x}, {self.y})'

p = Point(1, 2)
print(build_hash(p) == build_hash(p))  # True

Using build_hash as a Cache Key

A common pattern is to hash a dictionary of query parameters or function arguments to produce a compact, fixed-length cache key. This avoids dealing with arbitrarily long or complex key strings while guaranteeing that two semantically identical parameter sets always map to the same key.
from components.tools.hash.md5 import build_hash

params = {'query': 'SELECT *', 'limit': 100}
cache_key = build_hash(params)
print(cache_key)  # e.g. 'a3f1c7d2e5b04918...' (32 hex chars)
build_hash relies on str() for object representation. For custom classes, define __repr__ or __str__ to ensure stable hashing across interpreter sessions. Without an explicit representation, the default object.__repr__ includes the memory address, which changes between runs and breaks repeatability.
MD5 is not cryptographically secure. Do not use build_hash for security-sensitive purposes such as password storage or HMAC signatures. It is intended solely for deterministic key generation in caching and deduplication workflows.

Build docs developers (and LLMs) love