CID(data: str | bytes) - Immutable CID from its string or binary form, with version, codec, hash, str(cid) and bytes(cid)
decode_cid(data: str | bytes | CID) -> dict[str, Any] - Decode CIDs from string representation (e.g., 'bafy...') or raw bytes into structured data containing version, codec, and hash information
encode_cid(data: str | bytes | CID) -> str - Encode CID raw bytes to string representation, or return string CIDs as-is
decode_dag_cbor(data: bytes, *, canonical: bool = False) -> Any - Decode DAG-CBOR binary data into Python objects (dicts, lists, primitives, CID for links)
decode_dag_cbor_multi(data: bytes, *, canonical: bool = False) -> list[Any] - Decode multiple concatenated DAG-CBOR objects from a single byte stream
encode_dag_cbor(data: Any) -> bytes - Encode Python objects into DAG-CBOR binary format
🌐 Multibase Operations
decode_multibase(data: str) -> tuple[str, bytes] - Decode multibase-encoded strings, returning the base identifier and decoded data
encode_multibase(code: str, data: str | bytes) -> str - Encode data using specified multibase encoding (e.g. 'u' for base64url)
🚗 CAR (Content Addressable Archives) Operations
decode_car(data: bytes) -> tuple[dict[str, Any], dict[CID, dict[str, Any]]] - Decode CAR files into header metadata and a mapping of block CIDs to block data
Error handling
Every function raises ValueError for input it cannot decode or encode. The message starts with the failing operation (for example Failed to read CAR block.) followed by the reason.
The DAG-CBOR and CAR functions additionally raise RecursionError when the data is nested deeper than sys.getrecursionlimit().
Inputs are validated strictly against the specifications. There is no option to skip validation or accept partially broken data. Data produced by third-party encoders is not always valid, so consumers of untrusted input (for example the AT Protocol firehose) should be prepared to catch ValueError per message and skip it.
import libipld
try:
header, blocks = libipld.decode_car(data)
except ValueError as e:
print(f'skipping malformed CAR: {e}')
except RecursionError:
print('skipping CAR with excessively nested block')
Passing a value of the wrong type, such as a str where bytes is expected, raises TypeError.
Links and canonical form
Links decode to CID, and encode_dag_cbor writes a CID as a link and bytes as a byte string, so canonical data round-trips byte-exact. canonical=True rejects data in any other form:
value = libipld.decode_dag_cbor(data, canonical=True)
assert libipld.encode_dag_cbor(value) == data
A CID equals only another CID: compare with a string as cid == libipld.CID(s).
Requirements
Python 3.8 or higher.
Prebuilt wheels are published for CPython 3.8-3.15, free-threaded CPython 3.14t and 3.15t, and PyPy 3.11.
Installing
You can install or upgrade libipld via
pip install -U libipld
Performance
Benchmarks against cbrrr (C), py-ipld-dag (Python wrapper over Rust-backed cbor2) and dag_cbor (pure Python), measured on the four classic nativejson-benchmark fixtures (round-tripped through DAG-CBOR). Bars are operations/second relative to pure-Python dag_cbor; higher is better.
Measured on Apple M1, macOS 27, CPython 3.14.0, libipld installed from PyPI (PGO + LTO wheel).