Skip to main content

Pydantic-Construct

Pydantic-Construct integrates Pydantic with construct to provide typed binary serialization and parsing using standard Pydantic models.

Define binary layouts declaratively with type annotations, while keeping Pydantic’s validation and serialization.


Features

  • Declarative binary schemas via Annotated
  • model_dump_bytes() / model_validate_bytes()
  • Nested models
  • Computed fields with ordering control
  • Mode-aware field omission (json, python, binary)
  • Async parsing from streams

Installation

pip install pydantic-construct

Quick Start

Minimal example:

from typing import Annotated
from construct import Int32ul
from pydantic_construct import ConstructModel

class Model(ConstructModel):
    x: Annotated[int, Int32ul]

m = Model(x=123)

data = m.model_dump_bytes()
parsed = Model.model_validate_bytes(data)

assert data == b"\x7B\x00\x00\x00"
assert parsed.x == 123

With padding (ignored outside binary mode):

from typing import Annotated
from pydantic_construct import ConstructModel, OmitInMode
from construct import Padding, Int32ul

class Model(ConstructModel):
    x: Annotated[int, Int32ul]
    pad: Annotated[bytes | None, Padding(4), OmitInMode({"json", "python"})] = None

Core Concepts

Binary Fields

Each field must define a construct type via Annotated:

x: Annotated[int, Int32ul]

Mode-Based Omission

Exclude fields depending on serialization mode:

from typing import Annotated
from pydantic_construct import OmitInMode
from construct import Padding

pad: Annotated[
    bytes | None,
    Padding(4),
    OmitInMode({"json", "python"})
]

Modes:

  • "python"
  • "json"
  • "binary"

Nested Models

from typing import Annotated
from pydantic_construct import ConstructModel
from construct import Int32ul

class Header(ConstructModel):
    length: Annotated[int, Int32ul]

class Packet(ConstructModel):
    header: Header

Computed Fields (Binary)

Computed fields can participate in binary serialization if they return Annotated[..., Construct].

from typing import Annotated
from pydantic_construct import ConstructModel, binary_after
from pydantic import computed_field
from construct import Int32ul

class Example(ConstructModel):
    x: Annotated[int, Int32ul]

    @computed_field
    @property
    @binary_after("x")
    def checksum(self) -> Annotated[int, Int32ul]:
        return self.x ^ 0xFFFFFFFF

Positioning:

  • @binary_after("field")
  • @binary_before("field")

API Overview

Serialize to Binary

data = model.model_dump_bytes()

Parse from Binary

model = Model.model_validate_bytes(data)

Async Stream Parsing

model = await Model.model_validate_reader(reader)

Design Notes

  • A construct.Struct is generated at class creation time
  • Field order is deterministic and includes computed fields
  • Multiple ConstructModel roots in inheritance are disallowed

Constraints

  • Every field must define a construct type
  • Computed fields must return Annotated[..., Construct]
  • Binary layout must be deterministic

License

0BSD

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydantic_construct-0.1.1.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydantic_construct-0.1.1-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_construct-0.1.1.tar.gz.

File metadata

  • Download URL: pydantic_construct-0.1.1.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pydantic_construct-0.1.1.tar.gz
Algorithm Hash digest
SHA256 4a5edd2c3bc3f98a521c61fbb9766efc19720604b8b9d77168eb96453c67e623
MD5 020f92997dcd6f31c70b2d2ebd87a1c0
BLAKE2b-256 e4e91a15f53c5645b88aa2765a8de89e6b3f43eb3adbcdcf77a824495dfeed5d

See more details on using hashes here.

File details

Details for the file pydantic_construct-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pydantic_construct-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pydantic_construct-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 03835699ae25a603721e26145c32e92b4459a3d269ae5adc9581ddcd2a02ba27
MD5 c04abf2c3cb951e109d846341e294ff6
BLAKE2b-256 65ffae9b60bbc1a93be7a63ab886ecedc8d5d570bd6bec245f33babeda70a71d

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page