Error handling — when the .pkl is broken or wrong
Real users upload broken files. Your benchmark engine must handle: corrupt .pkl, wrong model type, feature mismatch, predict() crashes.
The output will appear here.
Production benchmark engines MUST handle broken inputs gracefully. 6 failure modes to handle: (1) Empty file — user uploaded nothing. (2) Too large — DoS protection. Cap at 100 MB. (3) Not a pickle — random bytes, wrong format. (4) Not a model — valid pickle but it's a list/dict/string, not an ML model. (5) Feature mismatch — model trained on 5 features, test data has 3. predict() crashes. (6) Prediction crash — model is broken, predict() raises. KEY PRINCIPLE: never raise an exception. Always return a dict with status='error' + message. FastAPI will serialize it to JSON — the user sees a clear error instead of a 500. The 5 tests at the bottom prove every failure mode is handled.
-
Change the size limit from 100 MB to 10 MB→ tighter limit — rejects large models -
Add a check for pickle's security (only allow certain classes)→ prevents arbitrary code execution -
Add a timeout: if predict() takes > 10 seconds, kill it→ prevents infinite loops -
Test with a model that has predict_proba (classifier)→ the function handles it — it just calls predict()