System design

How a prediction actually happens

Training runs offline. Conversion runs in CI. Only inference runs at request time — which is the only reason three model families fit in a serverless function at all.

The constraint that shaped everything

Vercel gives a Python serverless function a 250 MB unzipped bundle. Installed together, the libraries these models were trained with weigh far more than that:

PackageInstalled sizeNeeded at inference?
scipy90.6 MBNo
catboost87.2 MBNo
pandas56.6 MBNo
numpy53.5 MBYes
scikit-learn45.2 MBNo
xgboost + lightgbm~50 MBNo
Training stack total ~330 MB over budget
onnxruntime + numpy ~95 MB ships

A trained tree ensemble is just a set of split thresholds. It does not need the library that grew it in order to evaluate — it needs something that can walk the tree. ONNX Runtime does that for all four model families out of one ~16 MB wheel. Every export is replayed against its original and the build fails on any drift beyond 1e-3, so what serves traffic is the model that was trained.

This is why the site was a mock.

The previous deployment shipped Flask and Werkzeug only — no ML libraries at all. The models existed on a laptop, and the deployed page hardcoded running: true beside links to routes that returned 404. The dependency budget above is the wall it hit.

Pipeline

OFFLINE · already done CI · GitHub Actions REQUEST TIME · Vercel Training data 116k loans · 913k sales rows · Rossmann Trained artifacts (.pkl) RandomForest · CatBoost · LGBM · Ridge · XGB Never runs on a laptop no torch, no training at request time Convert to ONNX skl2onnx · onnxmltools · catboost export Verify parity vs original fails the build above 1e-3 drift Precompute baselines 500 store/item means from 913k rows Flask + ONNX Runtime ~95 MB · sessions cached per instance Encode → scale → predict scaler.onnx then model.onnx Labelled JSON response model · measured · rule · assumption

Request path

A single prediction is two ONNX graph executions. The scaler is exported as its own graph rather than reimplemented in Python, because a hand-rolled standardisation that disagrees with the fitted one by a fraction is the kind of bug that never announces itself.

POST /api/loan/predict
  │
  ├─ build_loan_vector(payload)
  │    ├─ numeric  → clamp to training range, fall back to median
  │    └─ categorical → explicit one-hot against fitted dummy columns
  │         (the old app used get_dummies(drop_first=True) on one row,
  │          which silently zeroed every categorical it was given)
  │
  ├─ loan_scaler.onnx          → standardise 30 features
  ├─ loan_random_forest.onnx   → P(mx=1), P(mx=0)
  │
  ├─ p_default = P(mx=0)       ← target is inverted: mx=1 marks a GOOD loan
  └─ JSON: probability, band, ranked drivers, inference_ms
The inverted target.

In approach_train.csv, mx = 1 means a good loan, not a delinquent one. Reading column 1 of predict_proba as the risk — the obvious thing to do — returns the probability the loan is fine. The service reads column 0. This is the single easiest way to ship a model that is confidently backwards, and it is why the inversion is asserted in code and documented here.

Why CI builds the models

The conversion needs CatBoost, LightGBM, XGBoost and scikit-learn — the whole 330 MB stack. Putting that in a GitHub Actions workflow keeps it off both the laptop and the serverless bundle, and makes the model build reproducible: the workflow pins every library to the version the pickles were verified to load under, so a rebuild produces the same graphs rather than whatever the toolchain drifted to.

The parity check is the gate. tools/convert_models.py exits non-zero on any drift, and the commit step only runs on success — an unverified graph cannot reach main, and therefore cannot reach production. The live manifest reports the measured drift of every deployed graph.