gRPC Message Field Types

Every gRPC message is a list of typed fields. The type of each field decides how much space the value uses, how the value behaves when missing, and how every language reads it. Choosing the right field type keeps messages small and APIs clear.

Messages as Forms

Imagine a paper form with labelled boxes. One box accepts only numbers, another accepts text, and another accepts a tick mark. A proto message works like that form. Each box has a name, a type, and a fixed box number called the field number.

message Customer {                    Paper form view
  string name   = 1;     ───────▶    ┌─ Name:   [ text   ]
  int32  age    = 2;                 ├─ Age:    [ number ]
  bool   active = 3;                 └─ Active: [ yes/no ]
}

Scalar Types

Proto TypeHoldsDefault Value
int32, int64Whole numbers0
uint32, uint64Whole numbers without a minus sign0
float, doubleDecimal numbers0
booltrue or falsefalse
stringUTF-8 textEmpty string
bytesRaw binary data such as imagesEmpty bytes

Use int64 for money stored in cents and for timestamps in milliseconds. Avoid double for money, because decimal rounding creates tiny errors.

Enums

An enum limits a field to a fixed set of named choices. The first value must equal zero, and it works as the default.

enum OrderStatus {
  ORDER_STATUS_UNSPECIFIED = 0;
  ORDER_STATUS_PLACED      = 1;
  ORDER_STATUS_SHIPPED     = 2;
  ORDER_STATUS_DELIVERED   = 3;
}

The name "UNSPECIFIED" signals that the sender never set the value. Receivers can detect that case and respond safely.

Repeated Fields

The keyword repeated turns a field into a list. A missing list arrives as an empty list.

message Playlist {
  string title = 1;
  repeated string song_ids = 2;
}

Map Fields

A map stores key and value pairs. Keys must be strings or integer types. Values accept any type except another map.

message Product {
  string id = 1;
  map<string, string> labels = 2;
}

Nested Messages

A message can hold another message. Nesting groups related data and mirrors real objects.

message Order {
  string id = 1;
  Address shipping_address = 2;
  repeated Item items = 3;
}

message Address {
  string street = 1;
  string city   = 2;
  string postal_code = 3;
}

message Item {
  string product_id = 1;
  int32  quantity   = 2;
}

Oneof

A oneof group lets a message carry exactly one value from a set of choices. Setting one choice clears the others. A payment message shows the idea well.

message Payment {
  oneof method {
    CardDetails card   = 1;
    UpiDetails  upi    = 2;
    string      wallet_id = 3;
  }
}
Payment message
  ├─ card      ← set
  ├─ upi       ← empty
  └─ wallet_id ← empty
Only one slot stays filled at any moment.

Optional Fields

Plain proto3 scalars cannot tell "value is zero" from "value is missing". The optional keyword tracks presence. A receiver can check whether the sender set the field at all.

message Settings {
  optional int32 volume = 1;
}

Well Known Types

Google ships ready made types for common needs. Import them and reuse them across services.

TypePurpose
google.protobuf.TimestampA point in time
google.protobuf.DurationA span of time
google.protobuf.EmptyA message with no fields
google.protobuf.StringValueA string that can be null

Summary

Scalars hold basic values, enums limit choices, repeated creates lists, maps store pairs, nested messages group data, oneof allows one choice, and optional tracks presence. Pick the narrowest type that fits the data, and every client reads it the same way.