Part 7: Production Lesson 3 of 6 in this part
Data validation and technical debt in ML systems
What you will learn. Write simple validation rules that stop bad rows reaching a model, compare training data with live data to catch shifts, and name the kinds of technical debt that build up in machine-learning systems.
Before this lesson: MLOps: versioning, pipelines and safe releases. Already know these? Carry on.
In plain words
A model is only as good as the rows it is given. Bad rows come in quietly: a sensor sends -4 for an age, a country appears with a new spelling, an amount is missing, a pipeline repeats a record, or a value is 300 when it is usually 50. Data validation means writing rules that every row must pass, and checking them before training and again before serving. A failed rule either stops the pipeline, drops the row or raises an alert, depending on how serious it is. A second check compares the live data with the training data: if the average of a feature has moved a long way, the model is being asked about a world it never saw. Beyond data, ML systems collect technical debt: shortcuts that make a change cheap now and expensive later. Naming the kinds of debt helps you notice them early.
- InputNew data arrivesFrom a file, database or event stream
- ProcessCheck every row against the rulesMissing values, ranges, allowed values, repeats
- ModelDecide: stop, drop the row or alertDepends on how serious the failure is
- OutputPass only clean rows onTo training or to the live model
The same rules run twice. Bad data should be stopped at both doors.
Row rules
- Range, allowed values, missing values
- Catch broken or impossible rows
Table rules
- Unique ids, row counts, column names
- Catch repeats and silent schema changes
Shift checks
- Compare live averages with training
- Catch a changed world
Use a mix. Each kind of rule catches things the others miss.
In the code
- Glue code to join libraries
- Tangled data pipelines
- Old experiments left in place
In the data and use
- Inputs other teams change without warning
- Other systems quietly using the predictions
- A model that influences its own future data
Most of it is in the code and data around the model, not in the model.
Real-world example
A retailer's model predicts the amount a customer will spend. One morning its predictions double. Nobody changed the model. A source system had started sending amounts in cents instead of euros, so an amount of 50 became 5000. A range check on the amount would have flagged the first batch in seconds, and the live-versus-training average would have shown a huge shift. The team now runs both checks on every batch and has a rule: a failure above a set share of rows stops the pipeline and sends a message to the data owner.
How it works
- Write the rules. For each column: is it required, what type, what range, what allowed values. For the table: ids unique, columns present, a reasonable number of rows.
- Run them on every batch. Count how many rows fail each rule and keep the counts.
- Choose a response. Stop for serious failures (a missing column), drop or repair a row for minor ones, and always log the counts.
- Compare live with training. For each important feature, compare the live average with the training average, and alert when the shift is larger than expected.
- Pay down the debt. Keep the pipeline simple, delete dead experiments, write down who uses the predictions, and test the whole path, not only the model.
Live simulation
Data checks are cheap insurance. See what they catch, and what they cannot.
The question. How do checks on incoming data protect a model, and what do they miss?
What to watch
- Each check is a small rule. Turn them on and off and see which rows fail which rule.
- Rename a column, as a data supplier might without warning.
- Compare a run with a column check against a run with only row checks.
This simulation needs JavaScript, which is switched off or not loaded. The lists above say what it shows, and the example programs below print the same kind of numbers.
Try this
- Turn on the column check, rename the column, and see the pipeline stop.
- Rename the column with only the email and country checks on, and see rows pass with nothing noticed.
What this leaves out. Ten made-up rows and five hand-written checks. Real pipelines use a validation library, check statistics as well as rules, and decide what to do with failing rows (reject, repair or quarantine).
Python program that prints the same headline numbers: examples/sim-ops/ops.py.
Python example
Python: example run The example programme behind this lesson's quoted output ran on 2026-11-19 (Python 3.12.10). 1 code block in the lesson passed a syntax check, 1 also ran on their own.
Ten rows with deliberate problems, four rules, a unique-id check, and a comparison of the live average with the training average. It uses only the standard library. Libraries such as Great Expectations or Pandera let you write the same kind of rules in a reusable way, but they were not run for this lesson.
"""Data validation: check every row against simple rules before it reaches a model, and compare training data with live data.
Run: python validate.py
"""
rows = [
{"id": 1, "age": 34, "country": "PT", "amount": 52.0},
{"id": 2, "age": 29, "country": "ES", "amount": 48.5},
{"id": 3, "age": -4, "country": "PT", "amount": 51.0}, # impossible age
{"id": 4, "age": 41, "country": "XX", "amount": 60.0}, # unknown country
{"id": 5, "age": 38, "country": "FR", "amount": None}, # missing amount
{"id": 6, "age": 52, "country": "FR", "amount": 55.5},
{"id": 6, "age": 52, "country": "FR", "amount": 55.5}, # duplicate id
{"id": 7, "age": 150, "country": "ES", "amount": 47.0}, # impossible age
{"id": 8, "age": 26, "country": "PT", "amount": 300.0}, # far outside the usual range
{"id": 9, "age": 45, "country": "ES", "amount": 50.0},
]
rules = {
"age between 0 and 120": lambda r: r["age"] is not None and 0 <= r["age"] <= 120,
"country is PT, ES or FR": lambda r: r["country"] in ("PT", "ES", "FR"),
"amount is present": lambda r: r["amount"] is not None,
"amount at most 200": lambda r: r["amount"] is None or r["amount"] <= 200,
}
print("rule rows failing")
for name, test in rules.items():
print("%-26s %d" % (name, sum(1 for r in rows if not test(r))))
seen, dups, clean = set(), 0, []
for r in rows:
repeated = r["id"] in seen
dups += repeated
seen.add(r["id"])
if not repeated and all(t(r) for t in rules.values()):
clean.append(r)
print("%-26s %d" % ("id is unique", dups))
print("rows kept: %d of %d" % (len(clean), len(rows)))
train = [52.0, 48.5, 51.0, 60.0, 55.5, 47.0, 50.0]
live = [r["amount"] for r in rows if r["amount"] is not None]
mean = lambda xs: sum(xs) / len(xs)
print("average amount: training %.2f, live %.2f, shift %.2f" % (mean(train), mean(live), mean(live) - mean(train)))rule rows failing
age between 0 and 120 2
country is PT, ES or FR 1
amount is present 1
amount at most 200 1
id is unique 1
rows kept: 4 of 10
average amount: training 52.00, live 79.94, shift 27.94Two rows fail the age rule, one the country rule, one has a missing amount, one has an amount above 200, and one repeats an id. Four of the ten rows are kept. The live average amount is 79.94 against 52.00 in training, a shift of 27.94.
.NET (C#) example
C#: compiled and run The C# sample compiled on 2026-11-19 with .NET SDK 10.0.400 (no extra packages). It also ran to the end without error.
The same rules written from scratch in C#, with no packages, printing the same output. In .NET you can also put validation attributes on your record types, or use a validation library such as FluentValidation.
// Row-by-row data validation and a simple training-versus-live check. Same output as validate.py. No packages.
var rows = new List<(int id, int age, string country, double? amount)>
{
(1, 34, "PT", 52.0), (2, 29, "ES", 48.5),
(3, -4, "PT", 51.0), // impossible age
(4, 41, "XX", 60.0), // unknown country
(5, 38, "FR", null), // missing amount
(6, 52, "FR", 55.5),
(6, 52, "FR", 55.5), // duplicate id
(7, 150, "ES", 47.0), // impossible age
(8, 26, "PT", 300.0), // far outside the usual range
(9, 45, "ES", 50.0),
};
var rules = new (string name, Func<(int id, int age, string country, double? amount), bool> test)[]
{
("age between 0 and 120", r => r.age >= 0 && r.age <= 120),
("country is PT, ES or FR", r => r.country is "PT" or "ES" or "FR"),
("amount is present", r => r.amount != null),
("amount at most 200", r => r.amount == null || r.amount <= 200),
};
Console.WriteLine("rule rows failing");
foreach (var (name, test) in rules) Console.WriteLine($"{name,-26} {rows.Count(r => !test(r))}");
var seen = new HashSet<int>(); int dups = 0; var clean = new List<(int id, int age, string country, double? amount)>();
foreach (var r in rows)
{
bool repeated = !seen.Add(r.id);
if (repeated) dups++;
if (!repeated && rules.All(t => t.test(r))) clean.Add(r);
}
Console.WriteLine($"{"id is unique",-26} {dups}");
Console.WriteLine($"rows kept: {clean.Count} of {rows.Count}");
double[] train = { 52.0, 48.5, 51.0, 60.0, 55.5, 47.0, 50.0 };
double[] live = rows.Where(r => r.amount != null).Select(r => r.amount!.Value).ToArray();
Console.WriteLine($"average amount: training {train.Average():F2}, live {live.Average():F2}, shift {live.Average() - train.Average():F2}");Kinds of technical debt in ML systems
A 2015 paper by Google engineers, "Hidden Technical Debt in Machine Learning Systems", pointed out that the model is a small part of a real system and most debt is in the code and data around it. Common forms:
- Glue code: large amounts of code that only join a general-purpose library to your system.
- Pipeline jungles: data preparation that grew step by step into a tangle nobody dares change.
- Dead experimental paths: old branches of code kept "in case" that add complexity.
- Undeclared consumers: other systems that quietly use your model's output, so you cannot change it safely.
- Feedback loops: a model whose predictions influence the data it will be trained on next.
- Configuration debt: settings spread across files with no checks.
Maths behind it
- Shares and percentages of failing rows (arithmetic)
- Averages and the shift between two averages (statistics)
- Counting pairs: n times n minus 1, over 2 (combinatorics)
Common mistakes
- Validating only at training time and not at serving time.
- Silently dropping bad rows without recording how many.
- Setting a range rule from last month's data so tightly that normal growth triggers alarms.
- Treating a shift in one feature average as proof the model is wrong, without looking at the rows behind it.
- Leaving old experiments and unused features in the pipeline.
Quick check
Why run the same validation before training and before serving?
Show answer
Bad data can enter at either point. Training on it spoils the model, and serving on it gives wrong answers even from a good model.
A validation run drops 60% of the rows. What should you do?
Show answer
Treat it as a signal. Find out why so many rows fail, at the source, and report what was dropped instead of quietly training on the rest.
What does a large shift between live and training averages tell you?
Show answer
The model is being asked about data unlike what it learned from. Look at the rows behind the shift, then consider fixing the source or retraining.
What is a feedback loop in an ML system?
Show answer
A model whose predictions change the data it will later learn from, which can make it reinforce its own mistakes.
Completion
Evidence of completion. A validation report for a small dataset (rule, rows failing, rows kept) and a comparison of one feature's average in training and live data with a sentence on what you would do.
Saved in this browser only. Reading and exercise are ticked separately because they are different achievements. A tick records your own work; it is not a test result. See all progress.