Lesson 8 / 25
Generating Code
protoc and Buf.
Compilers and plugins
The protobuf compiler protoc with language plugins generates message classes and gRPC stubs. In Python, grpcio-tools bundles protoc; Go uses protoc-gen-go and protoc-gen-go-grpc; Java and Kotlin usually use Gradle or Maven plugins. Buf is a popular alternative toolchain that manages dependencies, runs linting and breaking-change detection, and generates code from a buf.gen.yaml file. Commit or publish generated code consistently, and regenerate in CI to catch drift.
Generating Python code and a Buf config
Commands based on the gRPC Python and Buf documentation.
# Python: generates order_pb2.py and order_pb2_grpc.py
python -m grpc_tools.protoc -I proto \
--python_out=gen --pyi_out=gen --grpc_python_out=gen \
proto/shop/v1/order.proto
# Buf: buf.gen.yaml (v2 format) generating Go code
# version: v2
# plugins:
# - remote: buf.build/protocolbuffers/go
# out: gen
# - remote: buf.build/grpc/go
# out: gen
buf lint && buf generateLint proto files
buf lint enforces naming and structure conventions so APIs across teams look consistent.
Quick check: What does the --grpc_python_out option generate?
- A database schema
- Only message classes
- gRPC client stubs and server base classes
- HTML documentation
Answer
gRPC client stubs and server base classes — Message classes come from --python_out.