0Pricing
MLOps Academy · Lesson

Write a Custom Predictor

Plug your own preprocessing and model code in.

Write a Custom Predictor is a free MLOps Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the MLOps Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

When Built-ins Fall Short

The built-in runtimes cover sklearn, PyTorch, and friends. But when your model needs special logic, you write your own. A custom predictor gives you full control. 🛠️

It Is Just a Container

A custom predictor is your own container image that speaks the KServe protocol. You package your code, model, and dependencies into one image and hand it to KServe.

The KServe Python SDK

The kserve Python package gives you a base class to extend. You subclass kserve.Model and fill in how your model loads and predicts.

import kserve

class MyModel(kserve.Model):
    def __init__(self, name):
        super().__init__(name)
        self.ready = False

Implement load()

The load method reads your model from disk into memory once at startup. When it finishes, you set ready to True so KServe knows it can serve.

def load(self):
    self.model = joblib.load("/mnt/models/model.joblib")
    self.ready = True

Implement predict()

The predict method takes the request payload, runs your model, and returns the result as a dict. This is where your real inference logic lives.

def predict(self, payload, headers=None):
    rows = payload["instances"]
    preds = self.model.predict(rows)
    return {"predictions": preds.tolist()}

Custom Preprocessing Fits Here

Need to reshape inputs or apply business rules before inference? Put that logic right inside predict or in a preprocess step. The runtime never had to know about it.

Start the Model Server

You wire your class into a ModelServer and start it. KServe's server handles HTTP, health, and the protocol so you only write model code. ModelServer runs the loop.

if __name__ == "__main__":
    model = MyModel("custom-model")
    model.load()
    kserve.ModelServer().start([model])

Package It in a Dockerfile

You build a container that installs kserve, copies your code, and runs the server. This Dockerfile produces the image KServe will launch.

FROM python:3.11-slim
RUN pip install kserve joblib scikit-learn
COPY model.py /app/model.py
CMD ["python", "/app/model.py"]

Point the Spec at Your Image

In the InferenceService, the predictor uses a plain containers block instead of a model format. You name your image and KServe runs it.

spec:
  predictor:
    containers:
      - name: kserve-container
        image: my-registry/custom-model:latest

Mounting Your Model

You can still set a storageUri, and KServe mounts the downloaded model at a known path your code reads. The container and the storageUri work together.

Same Protocol, Your Logic

Because you implement the standard endpoints, clients call your custom predictor exactly like a built-in one. The protocol stays identical, only the internals are yours.

Quick Check

You subclass kserve.Model for a custom predictor. Which method loads the model into memory?

Recap

You built a custom predictor by subclassing kserve.Model, filling in load and predict, and shipping it as a container. Same protocol, your own logic. Awesome work! 🎉

Frequently asked questions

Is the “Write a Custom Predictor” lesson free?

Yes — the full text of “Write a Custom Predictor” is free to read here on the web, and the MLOps Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the MLOps Academy course, upgrade to CoddyKit PRO.

What will I learn in “Write a Custom Predictor”?

Plug your own preprocessing and model code in. You practise MLOps Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start MLOps Academy?

No prior experience is required. MLOps Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Write a Custom Predictor” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this MLOps Academy lesson?

Yes. Every MLOps Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. The InferenceService Resource
  2. Scale to Zero and Back Up
  3. Write a Custom Predictor
  4. KServe vs Seldon Core
← Back to MLOps Academy