Skip to main content

The Buf logo

protoc-gen-grpc-py

PyPI version License Slack

protoc-gen-grpc-py generates well-typed, idiomatic gRPC stubs for protobuf-py, the ergonomic and modern Protobuf library for Python.

Fully typed clients and servicers you can read. asyncio and sync variants.
Plugs into your existing grpcio channels and servers.

QuickstartExampleprotobuf-py

It's a compatibility layer for projects already built on grpcio. The generated clients and servicers plug into your existing gRPC channels and servers, but serialize protobuf-py messages directly. This lets you upgrade to a better Protobuf package without touching your RPC stack.

[!TIP] For new projects, use Connect for Python instead. Connect speaks the gRPC and gRPC-Web protocols in addition to its own, so existing gRPC clients can call a Connect server unchanged. But you also get plain HTTP APIs you can curl, first-class streaming, and generated clients for every major language, including your frontend. protoc-gen-grpc-py only exists so current gRPC codebases can get the improvements of protobuf-py today without a full rewrite.

Quickstart

Generated code requires the runtime libraries protobuf-py and grpcio. The plugin works with Protobuf compilers like buf.

$ uv add protobuf-py grpcio
$ uv add --dev protoc-gen-py protoc-gen-grpc-py buf-bin

Add protoc-gen-grpc-py alongside protoc-gen-py in your buf.gen.yaml:

version: v2
inputs:
  - directory: proto
plugins:
  # Generates message types (*_pb.py).
  - local: protoc-gen-py
    out: src/gen
  # Generates gRPC service stubs (*_pb_grpc.py).
  - local: protoc-gen-grpc-py
    out: src/gen

To generate code for all Protobuf files within your project, run:

$ uv run -- buf generate

A *_pb_grpc.py file is generated for each proto file that declares a service. Message types come from protoc-gen-py, which also manages __init__.py files.

Feature highlights

Well-typed clients you can read

For each service, the plugin emits a client whose methods are fully typed, so editors and type checkers understand every RPC method, its request and response types, and its streaming shape:

async with grpc.aio.insecure_channel("localhost:50051") as channel:
    client = ElizaServiceClient(channel)
    response = await client.say(SayRequest(sentence="Hello!"))
    print(response.sentence)

Servicers that register themselves

Each service also gets a servicer base class with typed method signatures and an add_to_server method:

class ElizaService(ElizaServiceServicer):
    async def say(
        self, request: SayRequest, context: grpc.aio.ServicerContext
    ) -> SayResponse:
        return SayResponse(sentence=f"You said: {request.sentence}")


async def serve() -> None:
    server = grpc.aio.server()
    ElizaService().add_to_server(server)
    server.add_insecure_port("[::]:50051")
    await server.start()
    await server.wait_for_termination()

asyncio and sync, side by side

Every client and servicer is generated in both asyncio (grpc.aio) and synchronous flavors:

with grpc.insecure_channel("localhost:50051") as channel:
    client = ElizaServiceClientSync(channel) #synchronous servicer
    response = client.say(SayRequest(sentence="Hello!"))

protobuf-py messages end to end

Requests and responses are protobuf-py messages. They have readable generated code, typed oneofs with pattern matching, real IntEnum enums, and a high-performance Rust encoder/decoder, with zero dependency on the legacy google-protobuf runtime.

Generating with protoc

The plugin also works with protoc as a standard protoc plugin:

$ uv run protoc --proto_path proto \
    --py_out src/gen \
    --grpc-py_out src/gen \
    proto/a.proto proto/b.proto proto/c.proto

Example

See the example for a complete client and server using the generated stubs, including server, client, and bidirectional streaming.

Download files

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

Source Distribution

protoc_gen_grpc_py-0.2.0.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

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

protoc_gen_grpc_py-0.2.0-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file protoc_gen_grpc_py-0.2.0.tar.gz.

File metadata

  • Download URL: protoc_gen_grpc_py-0.2.0.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for protoc_gen_grpc_py-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e68fe26c0e26817aba88c02a3dc8f3b57bc58d3fba426a4d664435026c5316f8
MD5 e5d6c9b2216d638837ed6f302e14b721
BLAKE2b-256 609306a73ef21a298a5bcd95fc68fae561b5d881cbecd5b7834e845e9df4d1f8

See more details on using hashes here.

Provenance

The following attestation bundles were made for protoc_gen_grpc_py-0.2.0.tar.gz:

Publisher: release.yaml on bufbuild/protoc-gen-grpc-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file protoc_gen_grpc_py-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for protoc_gen_grpc_py-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0fcde39296feee0f9ece0b7f86a8648706296c50503ff5d365d44d1ffd0e0f92
MD5 6ff87cd05f5e48bd0b4bb75cceb4741e
BLAKE2b-256 013d6b9ac44359a742dfdfc3515011b0da9cc12dca1438cb809520172d8016f6

See more details on using hashes here.

Provenance

The following attestation bundles were made for protoc_gen_grpc_py-0.2.0-py3-none-any.whl:

Publisher: release.yaml on bufbuild/protoc-gen-grpc-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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