Skip to main content

fastgrpc2

PyPI Python License

A FastAPI-style developer experience for gRPC — pydantic-first, async, and typed.

Write an async handler that takes a plain Pydantic request and returns a Pydantic response. fastgrpc2 derives the protobuf contract from your annotations, (de)serializes over real gRPC/HTTP-2, injects your dependencies, and never makes you touch grpc.aio, a _pb2 stub, or a hand-written .proto. It's a thin layer on top of the grpc.aio C-core.

Documentation: https://fastgrpc.ru

Install

pip install fastgrpc2

Requires Python 3.10+. Pulls in grpcio, protobuf, and pydantic.

Example

Server:

from typing import Annotated

from pydantic import BaseModel, Field
from fastgrpc2 import Context, GrpcApp, GrpcError, GrpcService, ProtoField, StatusCode, TLS

class GetOrderRequest(BaseModel):
    order_number: Annotated[str, ProtoField(1), Field(min_length=1)]

class OrderReply(BaseModel):
    order_number: Annotated[str, ProtoField(1)]
    status: Annotated[str, ProtoField(2)]

service = GrpcService("Orders")

@service.unary("GetOrder")
async def get_order(request: GetOrderRequest, ctx: Context) -> OrderReply:
    if not request.order_number:
        raise GrpcError(StatusCode.INVALID_ARGUMENT, "order_number required")
    return OrderReply(order_number=request.order_number, status="paid")

app = GrpcApp(
    "orders",
    address="0.0.0.0:50051",
    tls=TLS(cert="server.crt", key="server.key"),
)
app.add_service(service)
# import asyncio; asyncio.run(app.run())

Client:

from fastgrpc2 import Channel, GrpcClient, TLS

class OrderClient(GrpcClient):
    service = "Orders"
    get_order = GrpcClient.unary("GetOrder", GetOrderRequest, OrderReply)

async with Channel("orders.internal:50051", tls=TLS(ca="ca.crt"), default_timeout=5.0) as ch:
    client = OrderClient(ch)
    reply = await client.get_order(GetOrderRequest(order_number="A-100"))
    print(reply.status)  # -> "paid"

Features

  • All four call types — @service.unary / unary_stream / stream_unary / stream_stream, inferred from the handler signature.
  • Stable Pydantic ↔ protobuf contracts — explicit Annotated[..., ProtoField(n)], validated on decode; scalars, nested models, list, Optional (proto3 presence), Enum, datetime/date; anything without a clean proto mapping falls back to a JSON carrier, so a model never fails to register. app.export_proto() dumps the .proto.
  • Context — metadata, deadline, trailers, and peer, kept out of the message body.
  • Errors — GrpcError + the full StatusCode enum + rich google.rpc-style details.
  • Client — a reusable Channel and a typed GrpcClient; per-call timeout / metadata.
  • Security — fail-closed explicit TLS / MTLS / Insecure; bearer credentials are rejected on plaintext channels and redacted from representations.
  • DI & middleware — Depends with dependency_overrides, and interceptors.
  • Ops — enable_health() (grpc.health.v1), graceful stop(grace), lifespan, Compression, bounded messages/metadata/concurrency, method-scoped RetryPolicy, Keepalive, and deadline propagation across service hops.
  • Testing — an in-process TestClient.

Performance

fastgrpc2 is a thin layer: the transport is the gRPC C-core, the codec is protobuf upb (C), and validation is pydantic-core (Rust). The framework adds single-digit % over a hand-written grpc.aio servicer running the identical handler. Its ceiling is Python's grpc.aio — ergonomics at low cost, not the throughput of compiled languages. See the benchmarks.

License

MIT © Ruslan Kiradiev

Metadata

Release files for fastgrpc2 0.4.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 fastgrpc2 0.4.0
File Size Uploaded Provenance Actions
fastgrpc2-0.4.0.tar.gz 43.2 kB Without provenance Details for fastgrpc2-0.4.0.tar.gz

Built distribution (wheel)

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

Total release size: 78.9 kB

Release files / fastgrpc2-0.4.0.tar.gz

Download URL fastgrpc2-0.4.0.tar.gz
Size 43.2 kB
Tags Source
SHA-256 checksum
How to use checksums
f7114f4da29b2144e9651fa91298007dce35da4af87d7398cae578884d4081bb
BLAKE2b-256 checksum
How to use checksums
0e03aee2fddf98b5b1f34bf2a1cda001dc7b41d73d8fad0363a37c55d4de2e7e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.0

Release files / fastgrpc2-0.4.0-py3-none-any.whl

Download URL fastgrpc2-0.4.0-py3-none-any.whl
Size 35.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
116216aea6847d489721acd58fbf24f79a912c52ea097e20ce1d0150a5896976
BLAKE2b-256 checksum
How to use checksums
c7022385220c042f926c789f218253365d0112f2b48b3dca1b8ffc29236e2d19
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.0

Security for fastgrpc2 0.4.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.

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.

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