UTTLV - Python TLV (Tag-Length-Value) Library

raw JSON →
0.7.1 verified Fri May 01 auth: no python

A Python library for encoding and decoding TLV (Tag-Length-Value) objects. Supports custom tag definitions, nested TLV structures, and iterator access. Current version 0.7.1, maintained on GitHub.

pip install uttlv
error TypeError: 'Tlv' object does not support item assignment
cause Attempting to assign to a Tlv object that has not been initialized properly or using a non-integer tag.
fix
Ensure you have created a Tlv instance: tlv = Tlv() and use integer tags: tlv[0x01] = b'data'
error ValueError: Invalid tag length size
cause The tag_len_sizes parameter is not correctly specified or the encoded TLV uses lengths outside the defined sizes.
fix
Define tag_len_sizes as a dict mapping tag ranges to (tag_bytes, length_bytes). Default is {0x00: (1,1)}. See docs for custom sizes.
error Exception: TLV parse error: unknown tag
cause Decoding TLV with tags not present in the current Tlv instance's tag registry.
fix
Use the same Tlv instance or configure the decoder with expected tags via tag_len_sizes or nested_tag_config_map.
breaking In v0.7.0, the 'nested_tag_config_map' parameter was added to Tlv constructor, changing the signature. Code using positional arguments for 'tag_len_sizes' may break.
fix Update constructor calls to use keyword arguments: Tlv(tag_len_sizes=...) if needed.
gotcha Tag values must be integers; assigning non-integer keys will raise TypeError.
fix Always use integer tag values, e.g., tlv[0x01] = b'...'.
gotcha When decoding, from_bytes() expects bytes, not hex string. Common mistake is passing a hex string directly.
fix Convert hex string to bytes first: tlv.from_bytes(bytes.fromhex('010203'))

Create a TLV object, set a tag, encode to bytes, decode back.

from uttlv import Tlv

tlv = Tlv()
tlv[0x01] = b'data'
encoded = tlv.to_bytes()
print('Encoded:', encoded.hex())

decoded = Tlv()
decoded.from_bytes(encoded)
assert decoded[0x01] == b'data'