Skip to main content

binal

Declarative binary modeling for Python

Define a binary format once, as a Python class, and get encoding,
decoding, validation, and layout from the same declaration.

PyPI version Python 3.13+ License: MIT

Documentation · Capabilities · Installation · Usage · Contributing


Capabilities

Area What it covers
Binary types Integers of any width up to 128 bits, varints (LEB128, zigzag, VLQ), binary floats from 8-bit to binary128, decimal floats (BID and DPD), fixed point, BCD, bytes, and strings (sized, prefixed, terminated, greedy)
Semantic types UUIDs, IP and MAC addresses, timestamps, dates, times, durations, complex and rational numbers
Modeling Nested and recursive models, bit fields, lists, tuples, enums and flags, tagged and untagged unions, conditional and dependent fields, derived lengths and counts
Checksums CRC-8, CRC-16, CRC-32, CRC-32C, CRC-64, and Adler-32, verified on decode
I/O File, buffer, stream, and asynchronous I/O, incremental parsing and partial decoding
Validation Field constraints, cross-field validation, and structured errors with field paths and byte offsets
Introspection Static schema and runtime layout with field offsets and sizes
Low-level I/O BinaryReader and BinaryWriter for ad-hoc formats and custom codecs

Installation

binal supports Python 3.13 and newer. It depends on fastcrc (CRC checksums) and tibs (bfloat16 and 8-bit floats), both with prebuilt wheels.

Add it to a uv project:

uv add binal

To install it in an active Python environment:

pip install binal

To build from source:

git clone https://github.com/nolantw/binal
cd binal
uv sync --locked

Usage

from io import BytesIO
from typing import Annotated

from binal import BinaryModel, Prefixed, UInt8, UInt16


class Packet(BinaryModel, byte_order="big"):
    version: UInt8
    payload: Annotated[bytes, Prefixed(UInt16)]


packet = Packet(version=1, payload=b"Hello")
data = packet.encode()  # b"\x01\x00\x05Hello"
assert Packet.decode(data) == packet

# Several records in one stream, read one at a time.
records = list(Packet.iter_read(BytesIO(data + data)))

# One record at an offset, with the bytes it consumed.
record, consumed = Packet.decode_partial(data + b"trailing")

# Where each field landed in the encoding.
print(packet.layout().total_bytes)  # 8

Project status

binal is pre-1.0: the API may change between minor releases.

License

MIT. See LICENSE.

Metadata

Release files for binal 0.5.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for binal 0.5.2
File Size Uploaded Provenance Actions
binal-0.5.2.tar.gz 111.6 kB With provenance Details for binal-0.5.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for binal 0.5.2
File Interpreter ABI Platform Actions
binal-0.5.2-py3-none-any.whl Python 3 none any Details for binal-0.5.2-py3-none-any.whl

Total release size: 236.2 kB

Release files / binal-0.5.2.tar.gz

Download URL binal-0.5.2.tar.gz
Size 111.6 kB
Tags Source
SHA-256 checksum
How to use checksums
356fac0fe2a0cadda321dee30df621f56c3581b9d4ee119eecf19b6589b3ba84
BLAKE2b-256 checksum
How to use checksums
640d886493910f81f62b77da67addb3540f462f1969409b1bd5249fe50717d88
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

Release files / binal-0.5.2-py3-none-any.whl

Download URL binal-0.5.2-py3-none-any.whl
Size 124.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a8cb716bdddd8d625353e2d32381edbe6eeca78ebd35910a32edd37190497143
BLAKE2b-256 checksum
How to use checksums
28feac0c33201fce29b40b6bd1f5a2c074deb2c0fe936537aa8af2d4b68c3837
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

Security for binal 0.5.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for binal-0.5.2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page