# Proto Gen

> Generate C++ and Python code from Protocol Buffer definitions. Use when user asks to generate proto files, compile protos, regenerate gRPC code, or mentions protobuf/gRPC generation.

- Skill: `majiayu000/proto-gen` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds@latest add majiayu000/proto-gen`
- Raw SKILL.md: https://api.skillmd.com/api/skills/majiayu000/proto-gen/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: majiayu000 (https://skillmd.com/u/majiayu000)
- Updated: 2026-09-09
- Page: https://skillmd.com/skills/majiayu000/proto-gen

---


# Protocol Buffer Code Generation Skill

Generate C++ and Python code from Protocol Buffer (`.proto`) service and message definitions.

## Prerequisites

- `ORDER_PARSER_PROCESSOR_ROOT` environment variable must be set
- `protoc` compiler must be installed
- For C++: `grpc_cpp_plugin` must be available
- For Python: `grpc_tools.protoc` must be installed

## Instructions

1. Verify `ORDER_PARSER_PROCESSOR_ROOT` is set
2. Determine which language to generate (C++, Python, or both)
3. Generate gRPC service files first, then message files
4. Report success or any errors encountered

## C++ Code Generation

### Generate gRPC Services (C++)
```bash
protoc -I$ORDER_PARSER_PROCESSOR_ROOT/proto \
  --cpp_out=$ORDER_PARSER_PROCESSOR_ROOT/generated/cpp/messages/ \
  --grpc_out=$ORDER_PARSER_PROCESSOR_ROOT/generated/cpp/ \
  --plugin=protoc-gen-grpc=`which grpc_cpp_plugin` \
  $(find $ORDER_PARSER_PROCESSOR_ROOT/proto/services/ -iname "*.proto")
```

**Note**: The `--cpp_out` is in `messages/` directory while `--grpc_out` is in root `cpp/` - this is intentional and works correctly.

### Generate Protobuf Messages (C++)
```bash
protoc -I$ORDER_PARSER_PROCESSOR_ROOT/proto/messages \
  --cpp_out=$ORDER_PARSER_PROCESSOR_ROOT/generated/cpp/messages/ \
  $(find $ORDER_PARSER_PROCESSOR_ROOT/proto/messages/ -iname "*.proto")
```

## Python Code Generation

### Generate gRPC Services (Python)
```bash
python3 -m grpc_tools.protoc \
  --proto_path=$ORDER_PARSER_PROCESSOR_ROOT/proto \
  --python_out=$ORDER_PARSER_PROCESSOR_ROOT/generated/python/ \
  --pyi_out=$ORDER_PARSER_PROCESSOR_ROOT/generated/python/ \
  --grpc_python_out=$ORDER_PARSER_PROCESSOR_ROOT/generated/python/ \
  $(find $ORDER_PARSER_PROCESSOR_ROOT/proto/services/ -iname "*.proto")
```

### Generate Protobuf Messages (Python)
```bash
python3 -m grpc_tools.protoc \
  --proto_path=$ORDER_PARSER_PROCESSOR_ROOT/proto \
  --python_out=$ORDER_PARSER_PROCESSOR_ROOT/generated/python/ \
  $(find $ORDER_PARSER_PROCESSOR_ROOT/proto/messages/ -iname "*.proto")
```

## Generated File Locations

### C++ Generated Files
- **Services**: `generated/cpp/services/*.pb.cc` and `*.pb.h`
- **Messages**: `generated/cpp/messages/*.pb.cc` and `*.pb.h`
- **gRPC stubs**: `generated/cpp/services/*.grpc.pb.cc` and `*.grpc.pb.h`

### Python Generated Files
- **Services**: `generated/python/*_pb2.py` and `*_pb2_grpc.py`
- **Messages**: `generated/python/*_pb2.py`
- **Type stubs**: `generated/python/*_pb2.pyi`

## Common Commands

### Generate All (C++ and Python)
Run all four commands in sequence:
1. C++ gRPC services
2. C++ protobuf messages
3. Python gRPC services
4. Python protobuf messages

### Generate Only C++
Run only the two C++ commands

### Generate Only Python
Run only the two Python commands

## Proto File Organization

- **Services**: `proto/services/*.proto` - gRPC service definitions
- **Messages**: `proto/messages/*.proto` - Message structure definitions

## Common Issues

- **ORDER_PARSER_PROCESSOR_ROOT not set**: Export it in your shell profile
- **protoc not found**: Install Protocol Buffers compiler
- **grpc_cpp_plugin not found**: Install gRPC C++ plugin
- **grpc_tools not found**: Install Python package: `pip install grpcio-tools`
- **Import errors in proto files**: Check that proto paths are correct and imports use relative paths

## Important Notes

- **Order matters**: Generate services before messages (services may depend on messages)
- **C++ quirk**: The `--cpp_out` for services goes to `messages/` but `--grpc_out` goes to root `cpp/` - this is correct
- **Regenerate after proto changes**: Always regenerate when modifying `.proto` files
- **CMake integration**:
  - CMake **does NOT** handle gRPC proto file generation automatically
  - CMake **does** handle ANTLR `.g4` file generation automatically
  - For proto files, use this skill to manually generate code before building

## Workflow

1. Modify `.proto` files in `proto/services/` or `proto/messages/`
2. Run this proto-gen skill to generate C++/Python code
3. Run the build skill to compile the project with updated generated code

## Success Criteria

- All proto files compile without errors
- Generated files appear in `generated/cpp/` and/or `generated/python/`
- No import errors or missing dependencies
- Generated code compiles successfully with the project

