Skip to main content

byteparse

Typed, safe, declarative binary parsing for Python.

from byteparse.formats import PNGFile

png = PNGFile.parse(open("image.png", "rb").read())
print(png.ihdr.width, png.ihdr.height)

Why byteparse

  • Typed classes. Fields are real Python types, so mypy, pyright and your IDE know that chunk.length is an int. No plugin needed.
  • Safe by default. Length fields can't make the parser allocate gigabytes or loop forever: per-field size, array length and nesting depth are limited out of the box.
  • Errors that say where. PNGFile.chunks[3].data: expected 1024 bytes, got 812 (offset 0x1a3), plus everything parsed before the failure.
  • Formats included. PNG, WAV, BMP and DNS ship ready to use.
  • Fast. Fixed-size runs of fields are read with one precompiled struct.Struct call.

Define your own

import zlib
import byteparse as bp
from byteparse import this

class Chunk(bp.Struct, endian="big"):
    length: int = bp.u32(max=2**31 - 1)
    chunk_type: bytes = bp.bytes(4)
    data: bytes = bp.bytes(this.length)
    crc: int = bp.u32(check=lambda v, ctx: v == zlib.crc32(ctx.chunk_type + ctx.data))

chunk = Chunk.parse(raw)              # bytes, bytearray or memoryview
chunk = Chunk.parse_stream(file_obj)  # reads only what it needs

this.length refers to an earlier field. For anything more complex, use a lambda: bp.bytes(lambda ctx: ctx.total - ctx.header_len).

Field specifiers

Specifier Type
u8 u16 u24 u32 u64, i8 ... i64 int
f32 f64 float
boolean() bool
bytes(n) bytes
cstring(), pascal_string(u16()) str
padding(n) None
bits(n), flag() int, bool
enum(u8(), MyEnum) MyEnum | int
array(Item, count) / until= / greedy=True tuple[Item, ...]
if_(cond, spec) T | None
switch(key, {1: A, 2: B}, default=...) union you annotate
sized(n, Inner) Inner

Validators are keyword arguments: const=, min=, max=, one_of=, check=lambda value, ctx: ....

Errors

try:
    png = PNGFile.parse(data)
except bp.ParseError as e:
    print(e)          # PNGFile.chunks[2].crc: check failed for value 2923585666 (offset 0x47)
    e.path            # ("chunks", 2, "crc")
    e.offset          # 0x47
    e.partial         # everything parsed before the failure

Command line

python -m byteparse dump image.png --format png
python -m byteparse dump answer.bin --format dns --json

Status

byteparse 0.1 parses only. Building (serializing) structs is planned for 1.x. Requires Python 3.11+, no dependencies.

Metadata

Release files for byteparse 0.1.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 byteparse 0.1.0
File Size Uploaded Provenance Actions
byteparse-0.1.0.tar.gz 29.7 kB With provenance Details for byteparse-0.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 52.5 kB

Release files / byteparse-0.1.0.tar.gz

Download URL byteparse-0.1.0.tar.gz
Size 29.7 kB
Tags Source
SHA-256 checksum
How to use checksums
d4f6f6a79133248b07f35fa651d8247785495a26ca0fba76c1740cf99f94ce02
BLAKE2b-256 checksum
How to use checksums
992b4b49f58dc7945867d72a1f394ecc8b275ee8889dd44544774a1bb4b78b90
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 11, 2026.

Transparency log

Release files / byteparse-0.1.0-py3-none-any.whl

Download URL byteparse-0.1.0-py3-none-any.whl
Size 22.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e49bc16a05717e7a5b48018510f871690266443bf387f448b41dd3fb8b5b44f0
BLAKE2b-256 checksum
How to use checksums
56b009d4f4cb7fa36f3886097cb0714bab1781d6b621f37425c2f208745a0940
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 11, 2026.

Transparency log

Security for byteparse 0.1.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 byteparse-0.1.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 Oct 11, 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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

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