Skip to main content

binal

Declarative, type-safe 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.12+ License: MIT

Documentation  ·  Capabilities  ·  Installation  ·  Usage  ·  Contributing


Capabilities

Area What it covers
Binary types Integers (including custom widths), floats, booleans, bytes, strings, and bit fields; ULEB128, SLEB128, ZigZag32, ZigZag64
Modeling Nested models, collections, enums, tagged unions, conditional and dependent fields, derived lengths, counts, and checksums (CRC32, CRC16, Adler32)
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.12 and newer, with zero runtime dependencies.

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/oceanus403/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 0.2.0 is released. The API may change before 1.0.

License

MIT. See LICENSE.

Metadata

Release files for binal 0.3.0

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.3.0
File Size Uploaded Provenance Actions
binal-0.3.0.tar.gz 47.8 kB With provenance Details for binal-0.3.0.tar.gz

Built distribution (wheel)

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

Total release size: 103.1 kB

Release files / binal-0.3.0.tar.gz

Download URL binal-0.3.0.tar.gz
Size 47.8 kB
Tags Source
SHA-256 checksum
How to use checksums
f8133dbd94625347d650f7fd5c4c21dd64f8fb164c357a50869e5f406277f94d
BLAKE2b-256 checksum
How to use checksums
d18e379e100235f6bbf7bc9e450a834869160b0504195aa6c7192004c74317a9
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 26, 2026.

Transparency log

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

Download URL binal-0.3.0-py3-none-any.whl
Size 55.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d2309cbda7ef68c42286646ca50c0475c3c5d546108128bf9cfb738d6849c851
BLAKE2b-256 checksum
How to use checksums
03444c9d33195de03b72c910f0e84e81eb13908317c808b24dce5639c4a37e3b
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 26, 2026.

Transparency log

Security for binal 0.3.0

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.3.0.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 26, 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