Back to Data Science Notes
Topic #106

Model Serving with Flask & FastAPI

Learn how to expose a trained machine learning model as a REST API using Flask or FastAPI, enabling real-time predictions via HTTP requests.

What it is

Model serving is the process of deploying a trained machine learning model into a production environment where it can receive input data and return predictions. In Python, this is commonly achieved by wrapping the model in a web framework like Flask (a lightweight WSGI web application framework) or FastAPI (a modern, high-performance framework for building APIs). The mental model is simple: the web server acts as a gatekeeper that accepts JSON payloads, passes them to the model for inference, and returns the result as a JSON response.

Related terms include inference (the act of making a prediction), endpoint (the URL path where the service listens), and serialization (converting data structures to JSON).

Why it matters

  • Decoupling: Separates the data science workflow from the application development stack.
  • Scalability: Allows multiple clients to access the same model instance simultaneously.
  • Integration: Enables easy connection with frontend applications, mobile apps, or other microservices.
  • Monitoring: Provides a standard interface for logging inputs, outputs, and latency metrics.

Syntax or steps

  1. Load the pre-trained model once at startup to avoid reloading on every request.
  2. Define an endpoint (e.g., /predict) that accepts POST requests.
  3. Parse the incoming JSON body into a format the model expects (usually a NumPy array or DataFrame).
  4. Run the model's .predict() method.
  5. Convert the output back to a JSON-serializable format and return it.

Example

Below is a minimal example using FastAPI, which is often preferred for its automatic documentation and performance. This assumes a scikit-learn model saved as model.pkl.

import pickle
import numpy as np
from fastapi import FastAPI
from pydantic import BaseModel

# 1. Load model once at startup
with open('model.pkl', 'rb') as f:
    model = pickle.load(f)

app = FastAPI()

# 2. Define input schema
class PredictionInput(BaseModel):
    features: list[float]

@app.post("/predict")
def predict(input_data: PredictionInput):
    # 3. Convert list to numpy array
    features_array = np.array(input_data.features).reshape(1, -1)
    
    # 4. Make prediction
    prediction = model.predict(features_array)
    
    # 5. Return JSON response
    return {"prediction": float(prediction[0])}

Explanation: The PredictionInput class uses Pydantic to validate that the client sends a list of floats. The @app.post("/predict") decorator registers the route. Inside the function, we reshape the input because scikit-learn models expect 2D arrays (samples, features), even for a single prediction.

Common mistakes

  • Loading the model inside the request handler: This causes massive latency. Always load the model globally or in a startup event.
  • Ignoring data types: Sending strings instead of numbers, or lists instead of arrays, will cause errors. Use validation libraries like Pydantic.
  • Not handling exceptions: If the model fails, the API should return a proper HTTP error code (e.g., 500) rather than crashing silently.
  • Security risks: Never use pickle.load() on untrusted files. For public APIs, consider safer formats like ONNX or joblib with strict controls.

When to use it

FeatureFlaskFastAPI
PerformanceModerate (WSGI)High (ASGI/Async)
Data ValidationManual or extensionsBuilt-in (Pydantic)
DocumentationRequires pluginsAuto-generated Swagger UI
Best ForSimple prototypes, legacy systemsProduction APIs, complex schemas

Use FastAPI when you need robust type checking and high throughput. Use Flask if your team is already familiar with it or if you are building a very simple wrapper without async requirements.

Practice

Guided Exercise: Modify the example above to accept two separate integer fields (age and income) instead of a generic list. Update the Pydantic model and the reshaping logic accordingly.

Challenge: Add a health check endpoint /health that returns {"status": "ok"} to allow monitoring tools to verify the server is running.

Quick check

Q: Why must the input be reshaped to (1, -1) before calling model.predict()?

A: Scikit-learn models expect a 2D array representing multiple samples. A single sample provided as a 1D list needs to be converted into a 2D array with one row and N columns.

Summary

Serving models via Flask or FastAPI transforms static artifacts into dynamic services. By validating inputs, loading models efficiently, and returning structured JSON, you create a reliable bridge between data science and software engineering.

Want to go beyond the notes?

Join Coding Now Tech Institute's Data Science course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Model Serving with Flask & FastAPI – FAQs

Quick answers about learning Model Serving with Flask & FastAPI in Data Science.

This free note from Coding Now Tech Institute explains Model Serving with Flask & FastAPI in Data Science — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Data Science topic on Coding Now Tech Institute, including Model Serving with Flask & FastAPI, is 100% free with no signup required.
With focused practice, most students grasp Model Serving with Flask & FastAPI in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now