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.Structis generated at class creation time - Field order is deterministic and includes computed fields
- Multiple
ConstructModelroots in inheritance are disallowed
Constraints
- Every field must define a
constructtype - Computed fields must return
Annotated[..., Construct] - Binary layout must be deterministic
License
0BSD
Metadata
Release files for pydantic-construct 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pydantic_construct-0.1.1.tar.gz | 4.5 kB | Without provenance | Details for pydantic_construct-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pydantic_construct-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for pydantic_construct-0.1.1-py3-none-any.whl |
Total release size: 10.1 kB
Release files / pydantic_construct-0.1.1.tar.gz
| Download URL | pydantic_construct-0.1.1.tar.gz |
|---|---|
| Size | 4.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4a5edd2c3bc3f98a521c61fbb9766efc19720604b8b9d77168eb96453c67e623
|
|
BLAKE2b-256 checksum How to use checksums |
e4e91a15f53c5645b88aa2765a8de89e6b3f43eb3adbcdcf77a824495dfeed5d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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}
|
Release files / pydantic_construct-0.1.1-py3-none-any.whl
| Download URL | pydantic_construct-0.1.1-py3-none-any.whl |
|---|---|
| Size | 5.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
03835699ae25a603721e26145c32e92b4459a3d269ae5adc9581ddcd2a02ba27
|
|
BLAKE2b-256 checksum How to use checksums |
65ffae9b60bbc1a93be7a63ab886ecedc8d5d570bd6bec245f33babeda70a71d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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}
|
Security for pydantic-construct 0.1.1
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
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.