Skip to main content

FastAPI-like developer experience for gRPC: pydantic-first, async, built on grpc.aio

Project description

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 pydantic import BaseModel
from fastgrpc2 import GrpcApp, GrpcService, Context, GrpcError, StatusCode

class GetOrderRequest(BaseModel):
    order_number: str

class OrderReply(BaseModel):
    order_number: str
    status: str

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")
app.add_service(service)
# import asyncio; asyncio.run(app.run())

Client:

from fastgrpc2 import GrpcClient, Channel

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

async with Channel("127.0.0.1:50051", 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.
  • Pydantic ↔ protobuf — 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.
  • ErrorsGrpcError + the full StatusCode enum + rich google.rpc-style details.
  • Client — a reusable Channel and a typed GrpcClient; per-call timeout / metadata.
  • SecurityTLS / MTLS / Insecure builders and BearerToken.
  • DI & middlewareDepends with dependency_overrides, and interceptors.
  • Opsenable_health() (grpc.health.v1), graceful stop(grace), lifespan, Compression, message-size limits, 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

Project details


Download files

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

Source Distribution

fastgrpc2-0.3.1.tar.gz (29.2 kB view details)

Uploaded Source

Built Distribution

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

fastgrpc2-0.3.1-py3-none-any.whl (28.4 kB view details)

Uploaded Python 3

File details

Details for the file fastgrpc2-0.3.1.tar.gz.

File metadata

  • Download URL: fastgrpc2-0.3.1.tar.gz
  • Upload date:
  • Size: 29.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for fastgrpc2-0.3.1.tar.gz
Algorithm Hash digest
SHA256 b0c0857def5b108bce904c07780e04a7c9cbb90783c5e629342249d0ec8a425f
MD5 2d0f735cd0c5e5eaf7f1d1f5035a17c9
BLAKE2b-256 94f86d7f1dc05d8b7f30e665f16f9f3970966883d6348efdf41b0451b23e837d

See more details on using hashes here.

File details

Details for the file fastgrpc2-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: fastgrpc2-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 28.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for fastgrpc2-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3e991e413da01de3bf8bc728eeeb45e0d0d6fe3c7b6c0f029ac4a9e62c042851
MD5 c600ed635533deea35463efd6169770c
BLAKE2b-256 03aadb4b1a09b3a460cfe4c9d13ae2b9994f38ce4a3c03da5a375e32a346444e

See more details on using hashes here.

Supported by

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