Construct is a powerful declarative and symmetrical parser and builder for binary data.

Instead of writing imperative code to parse a piece of data, you declaratively define a data structure that describes your data. As this data structure is not code, you can use it in one direction to parse data into Pythonic objects, and in the other direction, to build objects into binary data.

The library provides both simple, atomic constructs (such as integers of various sizes), as well as composite ones which allow you form hierarchical and sequential structures of increasing complexity. Construct features bit and byte granularity, easy debugging and testing, an easy-to-extend subclass system, and lots of primitive constructs to make your work easier:


A Struct is a collection of ordered, named fields:

>>> format = Struct(
...     "signature" / Const(b"BMP"),
...     "width" / Int8ub,
...     "height" / Int8ub,
...     "pixels" / Array(this.width * this.height, Byte),
... )
>>> format.parse(b'BMP\x03\x02\x07\x08\t\x0b\x0c\r')
Container(signature=b'BMP')(width=3)(height=2)(pixels=[7, 8, 9, 11, 12, 13])

A Sequence is a collection of ordered fields, and differs from Array and GreedyRange in that those two are homogenous:

>>> format = Sequence(PascalString(Byte, "utf8"), GreedyRange(Byte))
>>>[u"lalaland", [255,1,2]])
>>> format.parse(b"\x004361789432197")
['', [52, 51, 54, 49, 55, 56, 57, 52, 51, 50, 49, 57, 55]]

Construct has been used to parse:

  • Networking formats like Ethernet, IP, ICMP, IGMP, TCP, UDP, DNS, DHCP
  • Binary file formats like Bitmaps, PNG, GIF, EMF, WMF
  • Executable binaries formats like ELF32, PE32
  • Filesystem layouts like Ext2, Fat16, MBR

See more examples in current gallery and in deprecated gallery.

Development and support

Please use github issues to ask general questions, make feature requests (and vote for them), report issues and bugs, and to submit PRs. Feel free to request any changes that would support your project. There is also a gitter chat but using Issues is highly recommended.

Main documentation is at readthedocs, which is substantial. Source is at github. Releases are available at pypi.


Construct should run on CPython 2.7 3.3 3.4 3.5 3.6 3.7 and PyPy 2.7 3.5 implementations. Recommended is CPython 3.6 and PyPy (any version) because they support ordered keyword arguments, and also PyPy achieves much better performance. Therefore PyPy would be most recommended.

Following modules are needed only if you want to use certain features:

  • Enum34 is optional if you want Enum EnumFlags to take labels from IntEnum IntFlag.
  • Numpy is optional, if you want to serialize arrays using Numpy protocol. Otherwise arrays can still be serialized using PrefixedArray.
  • Arrow is optional, if you want to use Timestamp class.
  • Different Python versions support different compression modules (like gzip lzma), if you want to use Compressed class.
  • Ruamel.yaml is optional, if you want to use KaitaiStruct (KSY) exporter.


The library is downloadable and installable from Pypi. Just use standard command-line. There are no hard dependencies, but if you would like to install all supported (not required) modules listed above, you can use the 2nd command-line form.

  • pip install construct
  • pip install construct[extras]