Skip to main content

FastProto

Fast, Pythonic Protocol Buffers — messages are plain, readable @dataclass types, with all encoding and decoding handled by a compiled Rust core.

Google's Python protobuf generates opaque classes full of getters/setters and a reflection API you have to learn. FastProto instead generates clean dataclasses you can construct, compare, and repr() like any other — and does the wire work in Rust.

  • Idiomatic messages — generated code is a @dataclass with plain annotations (str, int, list[...], dict[...], | None); autocomplete, type checkers, and repr() all just work.
  • Rust wire codec — encode/decode run in Rust via PyO3, not pure Python.
  • Wire-compatible — bytes interoperate both ways with Google's reference protobuf runtime.
  • Standard toolchain — ships a protoc plugin; just add --fastproto_out.

Install

pip install fastproto            # runtime (Python 3.12+)
pip install "fastproto[plugin]"  # + the protoc code generator

Code generation also needs the protoc compiler itself — install it from your package manager (brew install protobuf, apt install protobuf-compiler) or the official releases.

Quick start

1. Define user.proto:

syntax = "proto3";
package example;

enum Role {
  ROLE_UNSPECIFIED = 0;
  ROLE_ADMIN = 1;
  ROLE_USER = 2;
}

message Address {
  string city = 1;
  string street = 2;
}

message User {
  int64 id = 1;
  string name = 2;
  optional string email = 3;
  Role role = 4;
  repeated string tags = 5;
  Address address = 6;
  map<string, int32> counters = 7;
}

2. Generate with protoc:

protoc --proto_path=. --fastproto_out=. user.proto

This writes user_pb.py — a plain, readable dataclass module:

# @generated by fastproto. DO NOT EDIT.
# source: user.proto
# pyright: reportUnknownVariableType=false
from dataclasses import dataclass, field
from enum import IntEnum

from fastproto import Message, Scalar, message


class Role(IntEnum):
    ROLE_UNSPECIFIED = 0
    ROLE_ADMIN = 1
    ROLE_USER = 2


_ADDRESS_DESCRIPTOR = bytes.fromhex("...")  # @generated (bytes elided)


@message(_ADDRESS_DESCRIPTOR)
@dataclass(slots=True)
class Address(Message):
    city: Scalar.String = ""
    street: Scalar.String = ""


_USER_DESCRIPTOR = bytes.fromhex("...")  # @generated (bytes elided)


@message(_USER_DESCRIPTOR)
@dataclass(slots=True)
class User(Message):
    id: Scalar.Int64 = 0
    name: Scalar.String = ""
    email: Scalar.String | None = None
    role: Role = Role(0)
    tags: list[Scalar.String] = field(default_factory=list)
    address: "Address | None" = None
    counters: dict[Scalar.String, Scalar.Int32] = field(default_factory=dict)

3. Use it like any dataclass:

from user_pb import Address, Role, User

user = User(
    id=42,
    name="Ada",
    role=Role.ROLE_ADMIN,
    tags=["vip", "beta"],
    address=Address(city="London", street="Baker St"),
    counters={"logins": 7},
)

data = user.to_bytes()               # serialize to protobuf wire bytes
assert User.from_bytes(data) == user  # and back

No SerializeToString() / ParseFromString() ceremony and no reflection — just to_bytes() / from_bytes() on a dataclass you can build, compare, and print.

Type mapping

Each proto scalar maps to an alias under fastproto.Scalar. An alias is just the underlying Python type (int, str, ...) tagged with Annotated[...], so it type-checks as the base type while still recording the exact wire type.

proto Python proto Python
double Scalar.Double fixed32 Scalar.Fixed32
float Scalar.Float fixed64 Scalar.Fixed64
int32 Scalar.Int32 sfixed32 Scalar.SFixed32
int64 Scalar.Int64 sfixed64 Scalar.SFixed64
uint32 Scalar.UInt32 bool Scalar.Bool
uint64 Scalar.UInt64 string Scalar.String
sint32 Scalar.SInt32 bytes Scalar.Bytes
sint64 Scalar.SInt64

Composite fields: repeated T → list[T], map<K, V> → dict[K, V], enum → IntEnum, and optional / message / oneof fields → T | None.

Semantics

  • Presence (proto3): plain scalars use their zero value and are not nullable; optional scalars, message fields, and oneof members are T | None and track explicit presence (a set empty string is distinct from unset). An all-default message encodes to b"".
  • oneof: members are plain optional fields; setting more than one raises ValueError at encode time.
  • Open enums (proto3): an enum value not defined in your schema decodes to a plain int (like google's runtime) and survives re-encoding.
  • Unknown fields: fields your schema doesn't know are preserved verbatim across decode → encode (forward compatibility). They are invisible to __init__ / repr / ==.
  • Nesting limit: messages deeper than 100 levels — or a cyclic object graph on encode — raise ValueError instead of exhausting the stack.
  • References: sibling, self, and enum references resolve lazily on the first to_bytes() / from_bytes() — nothing for you to wire up.
empty = User()
assert empty.to_bytes() == b"" and empty.email is None
User(phone="1", telegram="a").to_bytes()  # ValueError: ... oneof ...

Contributing

See CONTRIBUTING.md for setup, project layout, and the release flow.

License

MIT

Release files for fastproto 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 fastproto 0.2.0
File Size Uploaded
fastproto-0.2.0.tar.gz 78.3 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for fastproto 0.2.0
File
fastproto-0.2.0-cp312-abi3-win_amd64.whl CPython 3.12 abi3 Windows x86-64 Details
fastproto-0.2.0-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.12 abi3 Linux glibc 2.17+ x86-64 Details
fastproto-0.2.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.12 abi3 Linux glibc 2.17+ ARM64 Details
fastproto-0.2.0-cp312-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl CPython 3.12 abi3 macOS 11.0+ ARM64, macOS 10.12+ x86-64, macOS 10.12+ universal2 (ARM64, x86-64) Details

Total release size: 1.4 MB

Release files / fastproto-0.2.0.tar.gz

Download URL fastproto-0.2.0.tar.gz
Size 78.3 kB
Tags Source
SHA-256 checksum
How to use checksums
319a80e86fa8cb2adc64b4f48731b78ff35af69c9fe03fb1731544fb06b1b236
BLAKE2b-256 checksum
How to use checksums
d99629bd5b5e247a7405e207496e14ee2b2d7686e4ff906d7dd8c715998b5e0a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Jul 10, 2026.

Transparency log

Release files / fastproto-0.2.0-cp312-abi3-win_amd64.whl

Download URL fastproto-0.2.0-cp312-abi3-win_amd64.whl
Size 168.8 kB
Tags CPython 3.12 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
c403e1456a9aa2dbc2008b409774eb8b3eba0644ba1dff97569e96f69d2f1c64
BLAKE2b-256 checksum
How to use checksums
7f72119b50f3ed91e6615721a10019878b5d74ef1993d6aea4fabc8864bdfa72
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Jul 10, 2026.

Transparency log

Release files / fastproto-0.2.0-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL fastproto-0.2.0-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 308.8 kB
Tags CPython 3.12 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
c1cbdb85f1a9a1a5ca64314c6e5ed3076c969fb2a44a0e2501c5685696f0c98a
BLAKE2b-256 checksum
How to use checksums
047078f9ddb9aa4fce53e845e74d396e93f44c1625d86cbcc37ea5db1c3c1b79
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Jul 10, 2026.

Transparency log

Release files / fastproto-0.2.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL fastproto-0.2.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 304.6 kB
Tags CPython 3.12 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
3d9a12c5196ac42ace9f32e305ac7e0f5c8520083acd81f40983ef0438e966d6
BLAKE2b-256 checksum
How to use checksums
cc75635310f2f0ba2efa60288a5d589342d9b97a6b89f9fe802534a272fe6b04
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Jul 10, 2026.

Transparency log

Release files / fastproto-0.2.0-cp312-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl

Download URL fastproto-0.2.0-cp312-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Size 530.8 kB
Tags CPython 3.12 abi3 macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
57f303902ea1a264e229361a93862b05635cd6b72e3b75a03632524049f18dcf
BLAKE2b-256 checksum
How to use checksums
4022c3439975c30c4fee7c16e5f951a6ae49bad8f8c40eacac538a049afd35eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Jul 10, 2026.

Transparency log

Release history Release notifications | RSS feed

0.6.0

5 release files

0.5.0

5 release files

0.4.6

5 release files

0.4.5

5 release files

0.4.4

5 release files

0.4.3

5 release files

0.4.2

5 release files

0.4.1

5 release files

0.4.0

5 release files

0.3.1

5 release files

0.3.0

5 release files

This release

0.2.0 This release

5 release files

0.1.4

5 release files

0.1.3

5 release files

0.1.2

5 release files

0.1.1

2 release files

0.1.0

2 release files

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