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.
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:
| Package | Installed size | Needed at inference? |
|---|---|---|
| scipy | 90.6 MB | No |
| catboost | 87.2 MB | No |
| pandas | 56.6 MB | No |
| numpy | 53.5 MB | Yes |
| scikit-learn | 45.2 MB | No |
| xgboost + lightgbm | ~50 MB | No |
| 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.
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.
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
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.
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.