Lesson 9 of 9 · 30m
Shipping Models as Production Software
Package artifacts, validate schemas, monitor drift and outcomes, roll back safely, and operate machine learning without research-lab mythology.
The notebook is not the product. The product is a decision pipeline that receives incomplete production data, produces scores under latency constraints, logs evidence, and fails safely when reality changes. Most ML operational maturity is ordinary software engineering with a few statistical failure modes added. Treat the model as a versioned dependency of a system, not a sacred object.
Freeze the artifact and its contract
A deployable artifact should include the full preprocessing pipeline, model, schema expectations, and metadata:
model.joblib
model-card.json
requirements.txt
schema.json{"model_version": "conversion-2026.11.30", "trained_at": "2026-11-30T18:00:00Z",
"sklearn_version": "1.5.2",
"input_columns": ["account_age_days", "plan_code", "first_week_events"],
"output": {"name": "conversion_probability", "type": "float64"}}If preprocessing is outside the artifact, deployment has already diverged. Version both data schema and model artifact; they evolve together but not automatically.
Validate input before scoring
Do not let malformed production input silently produce plausible numbers:
required = {'account_age_days', 'plan_code', 'first_week_events'}
missing = required - set(payload)
if missing: raise ValueError(f'missing fields: {sorted(missing)}')
if payload['account_age_days'] < 0:
raise ValueError('account_age_days must be non-negative')Validation should cover type, range, nullability, and categorical domain policy. Some systems prefer fallback scores over hard failure. Either is valid if deliberate; accidental coercion is not.
Log features, predictions, and versions
You cannot debug drift if you do not know what the model saw:
{"event": "model_prediction", "model_version": "conversion-2026.11.30",
"request_id": "req_01J...", "score": 0.184, "latency_ms": 7,
"feature_schema_version": 3}Be careful with sensitive raw features. Log hashes, aggregates, or sampled payloads according to privacy requirements. Observability is not permission to create a shadow data lake of customer attributes.
Monitor inputs, outputs, and outcomes
Three different signals matter:
input drift feature distributions, null rates, unseen categories
output drift score distribution, threshold volume, prediction latency
outcome quality precision/recall proxy, conversion, overrides, downstream KPIsInput drift often appears first. Outcome labels arrive late. If you only monitor business outcomes, you detect failure after customers experience it. If you only monitor inputs, you may alert on harmless seasonal change.
Roll out like a dangerous migration
Use shadow mode, canaries, and rollback:
shadow new model scores but does not act
canary small cohort receives decisions; compare guardrails
ramp increase traffic while monitoring drift and KPIs
rollback previous artifact remains deployable and testedThe previous artifact is only a rollback path if dependencies and feature schemas are still compatible. Practice rollback before the incident.
Field notes
- Pickled artifacts are code-bearing files; load only trusted artifacts and pin dependency versions.
- Model latency includes feature retrieval, not just estimator inference.
- Never deploy a model that cannot handle production missingness merely because training data was clean.
- Alert thresholds should reflect operational cost; drift metrics without action policies become noise.
- Document intended use, prohibited use, retraining data windows, and known failure modes in a model card.
Was this lesson helpful?