# Generating Code — gRPC

Source: https://www.skillbyai.com/en/grpc/s-codegen

> 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.

```bash
# 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 generate
```

## Lint proto files

buf lint enforces naming and structure conventions so APIs across teams look consistent.

**Quiz:** What does the --grpc_python_out option generate?

- [ ] A database schema
- [ ] Only message classes
- [x] gRPC client stubs and server base classes
- [ ] HTML documentation

*Answer:* gRPC client stubs and server base classes. Message classes come from --python_out.
