Follow our step-by-step guide to serve your ML model with FastAPI. Deploy efficient, scalable machine learning apps in minutes.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
This section explains how to load a pre-trained machine learning model (for example, a model saved using joblib or pickle) and how to embed prediction logic within your FastAPI backend. Here, we assume that you have a model file called model.pkl that has been previously trained for tasks like classification or regression.
model.py) that will handle model loading and prediction functions.predict(input\_data), which processes input data and returns predictions.
// Import necessary libraries for model operations
import pickle
// Load the pre-trained model on module initialization for efficiency
with open("model.pkl", "rb") as model\_file:
model = pickle.load(model\_file)
// Define a function that accepts input data and outputs predictions
def predict(input\_data):
// Process the input\_data if necessary (e.g., converting to the required data type/format)
// Make prediction using the loaded model
prediction = model.predict([input\_data])
return prediction
In this part, you integrate the prediction logic into a FastAPI application so that the model can be served as an HTTP endpoint. FastAPI is a modern, fast (high-performance) web framework for building APIs with Python 3.6+ that uses type hints for data validation and automatic documentation generation.
app.py)./predict) that accepts data in JSON format and returns model predictions.
// Import necessary modules from FastAPI and Pydantic
from fastapi import FastAPI
from pydantic import BaseModel
from model import predict // Import the predict function from our model module
// Define the data schema for the prediction request using Pydantic
class PredictionRequest(BaseModel):
// For example, if the model takes two features as input:
feature1: float
feature2: float
// Extend this with additional fields as required by your model
// Initialize the FastAPI application
app = FastAPI()
// Create a POST endpoint to receive data and return predictions
@app.post("/predict")
async def prediction\_endpoint(request: PredictionRequest):
// Convert request data to a format expected by the predict function, here as a list of features
input\_data = [request.feature1, request.feature2]
prediction = predict(input\_data)
return {"prediction": prediction[0]} // Return the first (or only) prediction
Often, the raw data received by your endpoint may require transformation or scaling to be compatible with your machine learning model. You can encapsulate this processing inside the endpoint logic or within a dedicated preprocessing function.
// Optional: Define a preprocessing function if needed
def preprocess(input\_data):
// Insert transformation logic here, such as normalization or encoding
processed_data = input_data // Replace this with actual transformation logic
return processed\_data
// Updated endpoint using preprocessing
@app.post("/predict")
async def prediction\_endpoint(request: PredictionRequest):
// Format the received data
raw\_data = [request.feature1, request.feature2]
processed_data = preprocess(raw_data)
prediction = predict(processed\_data)
return {"prediction": prediction[0]}
FastAPI automatically generates interactive API documentation using tools like Swagger UI and ReDoc. This feature is invaluable for testing endpoints and understanding API usage without additional setup.
http://127.0.0.1:8000/docs for Swagger UI or http://127.0.0.1:8000/redoc for ReDoc./predict endpoint by providing input values and seeing the predicted outputs.
// Run the FastAPI application using Uvicorn
// This code is typically run in the terminal, not within the script itself
// Command: uvicorn app:app --reload
While the above steps produce a functional machine learning API, there are further considerations for performance and scalability in production environments.
By following this approach, you can effectively serve a machine learning model with FastAPI while ensuring the system is maintainable, scalable, and easy to understand.
From startups to enterprises and everything in between, see for yourself our incredible impact.
Need a dedicated strategic tech and growth partner? Discover what RapidDev can do for your business! Book a call with our team to schedule a free, no-obligation consultation. We’ll discuss your project and provide a custom quote at no cost.Â