Skip to content

Menu

gRPC and Protobuf Cheat Sheet: proto3 Syntax, Wire Types, Status Codes

Checked against Protocol Buffers and gRPC proto3 language guide and encoding docs on protobuf.dev; gRPC core concepts and status codes on grpc.io; encoding run with protobufjs 8.8.1 on 28 November 2026. Each section links to the official docs it was checked against.

This sheet is for developers writing .proto files and gRPC services who need the rules that break compatibility when you get them wrong. It follows the proto3 language guide and encoding docs on protobuf.dev and the gRPC docs on grpc.io. The byte dumps in the example were produced with protobufjs 8.8.1.

Decoding one protobuf fieldThe sheet's example bytes 08 96 01: the tag gives field number 1 and wire type 0, and the varint that follows decodes to 150.Bytes: 08 96 01Tag 0x08field 1, wire type 0 (VARINT)Varint 96 01value 1500 VARINTint32,sint32, bool,enum2 LENlength, thenbytes1 I64 / 5I328 or 4 bytes
The sheet's example bytes 08 96 01: the tag gives field number 1 and wire type 0, and the varint that follows decodes to 150.

When to use what

The field number, not the name, is what goes on the wire. That single fact drives most of the rules: never change a number, never reuse a deleted one (mark it reserved), and you can rename fields freely as long as nobody relies on JSON output, which uses names.

Use optional on scalars when "not set" and "set to zero" mean different things, such as a discount of 0. Without it, proto3 can't tell them apart, because default values aren't encoded at all. The proto3 guide now recommends optional over implicit presence for new fields. Prefer it to the old google.protobuf.Int32Value style wrappers, which the reference calls mostly obsolete.

For gRPC methods, start with unary calls and add streaming only when the data really is a stream: server streaming for feeds and large result sets, client streaming for uploads, bidirectional for chat-like protocols. Always set a deadline on the client. To turn a JSON sample into a first draft of a message, try JSON to Protobuf; to read bytes you captured, paste them into the Protobuf wire decoder. Our JSON Schema validator helps when the same data also travels as JSON.

The syntax (or edition) line must be the first non-comment line. Without it, protoc assumes proto2.

syntax = "proto3";

Select proto3; put it before anything else in the file

int32 id = 1;

Declare a field; the number 1 is its identity on the wire, and in our test field 16 already needed a two-byte tag

1 .. 536,870,911

Valid field numbers, except 19,000-19,999, which are reserved for the implementation

From the docs, not run by us
reserved 2, 9 to 11;

Block old field numbers so nobody reuses them; ranges are inclusive

reserved "body";

Block an old field name; numbers and names can't share one reserved line

double float int32 int64 uint32 uint64 sint32 sint64 fixed32 fixed64 sfixed32 sfixed64 bool string bytes

The proto3 scalar types; in our test int32 -1 took 10 value bytes and sint32 -1 took one

repeated int32 scores = 4;

A list; numeric repeated fields are packed by default into one length-delimited record

map<string, string> labels = 5;

A map; keys must be integral or string types, values can't be maps, and maps can't be repeated

oneof contact { string email = 7; string phone = 8; }

At most one member is set; setting one clears the others; no maps or repeated fields inside

From the docs, not run by us
optional bool pinned = 6;

Track presence, so an explicit false is sent and can be told apart from unset

enum Status { STATUS_UNSPECIFIED = 0; STATUS_OPEN = 1; }

The first value must be 0 and is the default; name it _UNSPECIFIED

From the docs, not run by us

Each record starts with a varint tag computed as (field_number << 3) | wire_type.

0 VARINT

int32, int64, uint32, uint64, sint32, sint64, bool, enum

1 I64

fixed64, sfixed64, double: always 8 bytes

From the docs, not run by us
2 LEN

string, bytes, embedded messages and packed repeated fields: a length then the bytes

3 SGROUP / 4 EGROUP

Group start and end; deprecated, don't use groups

From the docs, not run by us
5 I32

fixed32, sfixed32, float: always 4 bytes

From the docs, not run by us
08 96 01

Tag 0x08 = field 1, wire type 0; varint 96 01 = 150

google.protobuf.Timestamp

A UTC instant at nanosecond resolution; JSON form is an RFC 3339 string ending in Z

From the docs, not run by us
google.protobuf.Duration

A signed span; JSON form is seconds with an s suffix, like "1.212s"

From the docs, not run by us
google.protobuf.Empty

A message with no fields, for methods with nothing to send or return

From the docs, not run by us
google.protobuf.FieldMask

Paths of fields to read or update; JSON form is a comma-separated lowerCamelCase string

From the docs, not run by us
google.protobuf.Any

Any message plus a type URL; JSON adds an @type key

From the docs, not run by us
google.protobuf.Struct / Value

Dynamic JSON-like data when the shape isn't known in advance

From the docs, not run by us

Status codes are listed at https://grpc.io/docs/guides/status-codes/. Metadata keys starting with grpc- are reserved, and keys ending in -bin carry binary values.

rpc GetNote(GetNoteRequest) returns (Note);

Unary: one request, one response

From the docs, not run by us
rpc ListNotes(ListRequest) returns (stream Note);

Server streaming: one request, a sequence of responses

From the docs, not run by us
rpc Upload(stream Chunk) returns (UploadResult);

Client streaming: many requests, one response

From the docs, not run by us
rpc Chat(stream Msg) returns (stream Msg);

Bidirectional: two independent streams

From the docs, not run by us
0 OK, 1 CANCELLED, 2 UNKNOWN, 3 INVALID_ARGUMENT, 4 DEADLINE_EXCEEDED, 5 NOT_FOUND

Codes 0-5; DEADLINE_EXCEEDED can arrive even if the server finished the work

From the docs, not run by us
6 ALREADY_EXISTS, 7 PERMISSION_DENIED, 8 RESOURCE_EXHAUSTED, 9 FAILED_PRECONDITION, 10 ABORTED, 11 OUT_OF_RANGE

Codes 6-11; retry ABORTED at a higher level, don't retry FAILED_PRECONDITION until the state is fixed

From the docs, not run by us
12 UNIMPLEMENTED, 13 INTERNAL, 14 UNAVAILABLE, 15 DATA_LOSS, 16 UNAUTHENTICATED

Codes 12-16; UNAVAILABLE is the one to retry with back-off

From the docs, not run by us

Worked example: Encode a message and read the bytes

Define a message with a reserved slot, a packed list, a map and an optional flag:

syntax = "proto3";
package demo;
message Note {
  reserved 2;
  reserved "body";
  int32 id = 1;
  string title = 3;
  repeated int32 scores = 4;
  map<string, string> labels = 5;
  optional bool pinned = 6;
}

Encode {id: 150, title: "hi", scores: [1, 2, 300], pinned: false} with protobufjs:

08 96 01 1a 02 68 69 22 04 01 02 ac 02 30 00

Read it record by record. 08 96 01 is field 1 as a varint with value 150, the same bytes as the encoding guide's example. 1a is field 3, wire type 2, then length 02 and the bytes 68 69 ("hi"). 22 is field 4 as LEN: the three scores are packed into four bytes, 01, 02 and ac 02 (300 needs two varint bytes). 30 00 is field 6 set to false; it appears only because the field is optional. Without that keyword, false is the default and would be left out. We checked the default rule directly: a message with only id: 0 encoded to zero bytes. The empty labels map is also absent. Decoding the 15 bytes gave back the same object.

Common mistakes

  • Deleting a field and later adding a new field with the same number.

    Add the old number and name to reserved when you delete a field, so protoc rejects reuse.

  • Using int32 for a value that is usually negative, like a temperature offset.

    Use sint32. Encoding -1 with protobufjs took ten value bytes as int32 and one byte as sint32.

  • Expecting proto3 to tell you that a plain int32 count = 1; was explicitly set to 0.

    Declare it optional int32 count = 1; so presence is tracked and the zero is sent.

  • Making the first enum value meaningful, such as ACTIVE = 0.

    Reserve 0 for ..._UNSPECIFIED. Every unset field reads as 0, and you don't want that to look like a real state.

  • Calling gRPC methods without a deadline.

    Set a deadline or timeout on every client call so a stuck server ends in DEADLINE_EXCEEDED instead of a hung request.