Skip to main content

Binal

Declarative, type-safe binary modeling for Python.

Binal is a pure-Python library for defining, encoding, decoding, validating, and composing binary data structures using native Python classes and type annotations.

It provides a unified binary modeling system that eliminates repetitive serialization logic, manual buffer management, offset calculations, bitwise operations, and structural validation.

The data model serves as the single source of truth for both Python objects and their binary representations.

Installation

Requires Python 3.11+.

uv add binal
pip install binal

Quick Start

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'
packet = Packet.decode(data)
# Packet(version=1, payload=b'Hello')

Features

  • Declarative models with native type annotations
  • Integers (including custom widths), floats, booleans, bytes, strings, and bit fields
  • Nested models, collections, enums, and tagged unions
  • Length-prefixed, fixed-length, null-terminated, and length-dependent fields
  • Derived lengths, counts, and checksums (CRC32, CRC16, Adler32)
  • Conditional and dependent fields
  • Variable-length integers: ULEB128, SLEB128, ZigZag
  • Schema introspection with field offsets and sizes
  • File, buffer, stream, and asynchronous I/O
  • Structured errors with field paths and byte offsets
  • Zero runtime dependencies, pure Python

See the documentation and examples/ for more.

Development

uv sync --locked
uv run ruff format
uv run ruff check
uv run pyright
uv run pytest
uv run pytest --cov=binal
uv build

License

MIT. See LICENSE.

Metadata

Release files for binal 0.2.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.2.0
File Size Uploaded Provenance Actions
binal-0.2.0.tar.gz 46.6 kB With provenance Details for binal-0.2.0.tar.gz

Built distribution (wheel)

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

Total release size: 100.1 kB

Release files / binal-0.2.0.tar.gz

Download URL binal-0.2.0.tar.gz
Size 46.6 kB
Tags Source
SHA-256 checksum
How to use checksums
aae4f1be0f76a6b17017e1e9a967305c2ac5069a90681f8951a80929e16792fe
BLAKE2b-256 checksum
How to use checksums
a29b028b5aaf8baa2e4ddcec830f53d0abfb4391f8f01540392c9423262b6969
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 25, 2026.

Transparency log

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

Download URL binal-0.2.0-py3-none-any.whl
Size 53.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6a6a5674e27e2dc34f79c547b311c949e8eee156da391c7e5bec64673b47d552
BLAKE2b-256 checksum
How to use checksums
01e090f12b7d360f9680bbab92364e9632d4646901dc429aaaa21192f783b4d6
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 25, 2026.

Transparency log

Security for binal 0.2.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.2.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 25, 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