IT Vizag
← MLOps: Shipping ML Models

▶ Sign up free for the video, slides & prompts

The lesson below is open to all. Enter your email once to unlock every video, slide deck, prompt and code example on this device.

Checking…

MLOps: Shipping ML Models · Lesson 7 of 14 · 2 min read · 6 min video

Serving a model as an API

Wrap a model in FastAPI, validate inputs and return predictions fast.

📄 Download slides (PDF)· free sign-up

A model in a file helps no one. To use it, an app sends some data and gets a prediction back. The most common way to do that is a small web API.

FastAPI in one file

🔒 Code example: sign up free (above) to unlock it.

Run it with uvicorn src.api:app and FastAPI gives you interactive docs at /docs for free.

Rules that matter

  • Load the model once at startup, not on every request
  • Validate inputs: a typed schema rejects nonsense before it reaches the model
  • Use the same feature code as training. Different feature code in training and serving, called training–serving skew, is a classic silent bug
  • Return the model version with each prediction, so you can trace problems later
  • Add a /health endpoint so the platform knows the service is alive

Keep it fast

  • Keep the model in memory, and batch requests if traffic is heavy
  • Move heavy preprocessing into the pipeline if you can
  • Measure response times. Most apps want an answer within 100–300 ms

Log predictions

Save each request, prediction, model version and timestamp (minus personal data). You'll need them for monitoring and drift checks later in this course.

Tip: Put the feature code in one shared module that both train.py and api.py import. That one habit prevents most training–serving skew.

In one line

Serve a model through a small API that loads it once, validates inputs, shares the training feature code, returns the model version and logs every prediction.

Saved on this device. Sign in to keep progress everywhere.

Share with a friend