liveBook · Manning

Protobuf

As explained in Designing AI Systems

Protocol Buffers is a language-neutral, platform-neutral serialization format for structured data and service interface definitions.

Protocol Buffers define structured messages and service interfaces for communication across services. With gRPC, messages use a compact binary encoding, while a compiled service contract provides client methods that hide serialization and network calls. This is useful when a workflow makes many internal calls and the overhead of repeated JSON processing compounds. section 2.4.2

service SessionService {
    rpc GetOrCreateSession(GetOrCreateSessionRequest)
        returns (GetOrCreateSessionResponse);
    rpc AddMessages(AddMessagesRequest)
        returns (AddMessagesResponse);
}
 
message GetOrCreateSessionRequest {
    string user_id = 1;
    optional string session_id = 2;
}

The Session Service contract declares remote methods and a request message with numbered fields, including an optional session identifier. section 2.4.2

Explicit fields make the data crossing a service boundary precise. A model chat request, for example, contains a model identifier, repeated messages, generation configuration, tools, an optional response format, and a named system prompt. An empty model identifier allows the service to select a provider using its routing rules. section 3.1.6

Listing 3.6 Chat request Protocol Buffer definition
message ChatRequest {
  string model = 1;    #A
  repeated ChatMessage messages = 2;    #B
  ChatConfig config = 3;
  repeated ToolDefinition tools = 4;    #C
  optional ResponseFormat response_format = 5;
  string system_prompt_name = 6;    #D
}
 
message ToolDefinition {    #E
  string type = 1;    #F
  FunctionDef function = 2;
}
message FunctionDef {
  string name = 1;
  string description = 2;
  string parameters_json = 3;    #G
}
 

The chat request schema shows repeated and optional fields alongside nested tool definitions. section 3.1.6

A Python SDK client translates between application objects and the service contract in three phases:

  • Construct Protocol Buffer messages from Python inputs, such as converting message dictionaries into ChatMessage objects. section 3.9.2
  • Call the generated gRPC stub, which serializes the request, sends it with routing metadata, and deserializes the response. section 3.9.1 section 3.9.2
  • Convert the response into an application-facing Python object, such as a ChatResponse dataclass. section 3.9.2

An API gateway can proxy internal gRPC calls by forwarding binary data without deserializing it. This centralizes routing while preserving the benefit of the encoding. Compared with JSON, the reported typical figures are 30–50% smaller payloads and 3–5 times faster serialization; actual gains depend on the workload. section 2.4.3 section 2.4.2

How Designing AI Systems covers it

How deep it goes
Traces schemas, generated stubs, message conversion, and gateway forwarding without detailing binary encoding internals.
Examples
Uses Session Service methods and a model chat request with nested tool definitions to illustrate contracts.
What you can do after
Apply it via gRPC generated stubs to translate Python objects into service requests and responses.
How it connects
Connects gRPC, API gateways, SDKs, and JSON serialization.

Also explained in

Compare how 12 books explain protobuf →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Designing AI Systems. The section links point to the parts of the book it draws on.