How to create python GRPC stub with buf with local plugins
5 June, 2025 - Tags: buf.build, protobuf
Recently, I spent quite a bit of time traveling on French high-speed trains. While it's one of the best rail experiences in the world, I often use that time to work on personal or company projects.
Many of these projects involve Golang backends and Python clients that communicate using Protobuf. I’ve introduced Buf into some of them, which significantly improved the way we manage our Protobuf definitions. However, I quickly ran into an issue for the Python gRPC clients: we had been relying on the Python CLI packaged in grpcio
and grpcio-tools
to build our stubs and there was no easy way to call this CLI as a protoc plugin in a buf.gen.yaml file.
Buf supports remote plugins, but when I (or any teammate) am stuck with unreliable internet on a train, it becomes a real limitation.
Fortunately, there's a great solution: the Wasilib project, and more specifically, this repository: wasilibs/protoc.
wasilibs/protoc provides Go-based CLI tools that run C-based protoc
plugins using WebAssembly (WASM). This means we can run the python_grpc
plugin locally, without relying on protoc
directly. It simplifies converting traditional Makefile-based build setups into Buf configurations.
Here is an example of buf.gen.yaml
using the Wasilib project.
version: v2
plugins:
- local:
- go
- run
- github.com/wasilibs/go-protoc-gen-grpc/cmd/protoc-gen-grpc_python@latest
out: out/python