Skip to main content
Welcome to this tutorial on using BentoML to gradually upgrade your machine learning models without disrupting live user traffic. In this example, we have two versions of our ML model—v1 and v2. Switching all user traffic to a new version instantly is impractical in production environments. Instead, we use a blue-green deployment strategy to gradually route traffic between the two model versions. In our previous setup, a single model was deployed. In a real-world scenario, you might host both model versions in the same BentoML service yet keep their traffic isolated on different endpoints. This design allows requests for v1 and v2 to be handled separately, ensuring a smooth transition for your users. Below is a diagram that illustrates the model serving flow using BentoML. It shows how incoming requests are distributed between the different endpoints based on the model version:
The image is a diagram illustrating model serving using BentoML, showing a flow from users to a dashboard, then to BentoML serving with endpoints for different model versions, and finally to a machine learning model.
In this updated architecture, all incoming requests are processed by one of two endpoints, each corresponding to a different model version.

Prediction Function for the House Price Model

In the VS Code editor, consider the following snippet that defines the prediction function for our house price model:
After stopping the BentoML service, clearing the screen, and closing the file, open the model_service_v3.py file to review its configuration. This file references both models (v1 and v2) by creating two separate model runners and exposing two distinct endpoints.

Defining Separate Endpoints for Each Model Version

In model_service_v3.py, you will find code defining separate APIs for each model version:
By merging both prediction functions into a single BentoML service, we can efficiently manage traffic for legacy integrations (using v1) and for new clients (using v2). The snippet below further defines the input schema for the v2 model along with an API endpoint for the v1 model again:
After running the BentoML service command and refreshing the service endpoint, both the v1 and v2 endpoints become available. This separation ensures compatibility with legacy clients while allowing new features and improvements to be tested using the v2 endpoint. Below, the following diagram shows the BentoML Prediction Service’s web interface. It displays the available API endpoints for house price prediction and provides additional information on infrastructure observability:
The image shows a web interface for a BentoML Prediction Service, displaying API endpoints for house price prediction and infrastructure observability. It includes sections for service APIs, infrastructure endpoints, and schemas.

Testing the API Endpoints

You can use curl to send requests to these endpoints:

Testing the v1 Endpoint

Testing the v2 Endpoint

Below is a comprehensive example demonstrating the curl commands and their expected outputs:
Managing both endpoints within a single service simplifies the transition and allows controlled traffic routing. In the future, depending on your production needs and traffic patterns, you may consider separating these endpoints into different services.

Additional Context: Extended Model Input Schema

Here is another version of the model input schema and its endpoint configuration. This variation provides additional parameters for more detailed predictions:
Test the improved service setup with the following curl requests:

Testing the v1 Endpoint (Extended)

Testing the v2 Endpoint (Extended)

This setup, which provides separate endpoints for different model versions using a single BentoML service, offers a controlled environment for traffic routing and facilitates a smoother transition from legacy models to new enhancements. Thank you for following this lesson on upgrading model versions with BentoML Serving. Below is a final example of using curl to test the v1 endpoint:

Watch Video

Practice Lab