Skip to main content
gRPC is a high-performance, open-source remote procedure call (RPC) framework that uses HTTP/2 for transport and Protocol Buffers for serialization. Unlike traditional HTTP APIs, gRPC provides strong type safety, high performance, and built-in support for streaming and bidirectional communication. Run a gRPC server on Baseten when you want these properties for model inference. gRPC offers:
  • Type safety: Protocol Buffers enforce strong typing and contract validation between client and server.
  • Ecosystem integration: Integrate Baseten with existing gRPC-based services.
  • Streaming support: Built-in server streaming, client streaming, and bidirectional streaming.
  • Language interoperability: Generate client libraries for multiple programming languages from a single .proto file.

gRPC on Baseten

gRPC models run as custom servers. Your own server process handles gRPC requests directly, instead of going through the standard Truss load() and predict() methods. For this to work, you must first package your gRPC server code into a Docker image. Once that is done, you can set up your Truss config.yaml to configure your deployment and push the server to Baseten.

Setup

Installation

To install the prerequisites:
  1. Install uv if you don’t have it. This guide uses uvx to run Truss commands without a separate install step.
  2. Install the Protocol Buffer compiler:
    Terminal
  3. Set up a virtual environment and install gRPC tools:
    Terminal

Protocol buffer definition

Your gRPC service starts with a .proto file that defines the service interface and message types. Create an example.proto file in your project root:
example.proto

Generate Protocol Buffer code

Generate the Python code from your .proto file:
Terminal
This generates the necessary Python files (example_pb2.py and example_pb2_grpc.py) for your gRPC service. For more information about Protocol Buffers, see the official documentation.

Model implementation

Create your gRPC server implementation in a file called model.py. Here’s a basic example:
model.py

Deployment

Create a Dockerfile

Since gRPC on Baseten requires a custom server setup, you’ll need to create a Dockerfile that bundles your gRPC server code and dependencies. Here’s a basic skeleton:
Dockerfile
Create a requirements.txt file with your gRPC dependencies:
requirements.txt

Build and push Docker image

Build and push your Docker image to a container registry:
Terminal
Replace your-registry with your actual container registry (for example, Docker Hub, Google Container Registry, AWS ECR). You can create a Docker Hub container registry by following their documentation.

Configure your Truss

Update your config.yaml to use the custom Docker image and configure the gRPC server:
config.yaml

Deploy with Truss

Deploy your model using the Truss CLI. gRPC models aren’t supported in development deployments, so use the default published deployment or --promote to also promote to production.
Terminal
For more detailed information about Truss deployment, see the truss push documentation.

Call your model

Use a gRPC client

Once deployed, you can call your model using any gRPC client. Here’s an example Python client:
client.py

Inference for specific environments and deployments

To target a specific environment or deployment, add the corresponding header to your metadata list:
client.py

Inference for regional environments

If your organization uses regional environments, use the regional hostname as the gRPC target. The environment is derived from the hostname, so do not set x-baseten-environment or x-baseten-deployment headers.
client.py

Test your deployment

Run your client to test the deployed model:
Terminal

Per-request logging

Baseten assigns a unique request ID to every predict call and returns it in the x-baseten-request-id response metadata. You can use this ID to filter your model’s logs down to a single request. For standard Truss models, request ID logging is automatic. For custom gRPC servers, you’ll need to extract the request ID from the incoming metadata and include it in your JSON log output. Extract the request ID from the x-baseten-request-id metadata key:
model.py
Logs must be JSON formatted and written to stdout. The request_id field must be a top-level key in the JSON object.

Full example

See this GitHub repository for a full example.

Scaling

While many gRPC requests follow the traditional request-response pattern, gRPC also supports bidirectional streaming and long-lived connections. The implication of this is that a single long-lived connection, even if no data is being sent, counts against the concurrency target for the deployment.

Promotion

Like HTTP deployments, you can promote a gRPC deployment to an environment through the REST API or UI. For more information, see Environments. When you promote a gRPC deployment, new connections are routed to the new deployment, but existing connections stay on the current deployment until they terminate. Depending on the length of the connection, old deployments can take longer to scale down than HTTP deployments.

Monitoring

As with HTTP deployments, Baseten exposes performance metrics for gRPC deployments.

Inference volume

Baseten tracks inference volume as the number of RPCs per minute. The platform publishes these metrics after the request completes. See gRPC status codes for a full list of codes.

End-to-end response time

Measured at different percentiles (p50, p90, p95, p99): End-to-end response time includes cold starts, queuing, and inference (excludes client-side latency). Reflects real-world performance.

Next steps

  • Custom servers: Configure docker_server and no_build for container-based deployments.
  • truss push: Deploy and promote your gRPC model.
  • WebSockets: Another transport for real-time, bidirectional communication.