Sub topic 9
Building a batch scoring pipeline
This chapter assumes you know basic ML vocabulary (training, features, prediction), can read Python, and know what a scheduled job is. It builds on the batch prediction chapter and shows how to turn a one-off scoring script into a pipeline you can run every day.
What a batch pipeline does
A batch scoring pipeline reads a snapshot of records, scores all of them in one run, and writes the results where other systems can read them later. Nobody waits for a single prediction. The run only has to finish before the results are needed, for example before a morning email campaign.
A minimal job
The job below loads a saved scikit-learn classifier, scores every customer row in one pass, and writes a file.
import joblib
import pandas as pd
model = joblib.load("model.joblib")
customers = pd.read_parquet("customers_2026-11-28.parquet")
feature_columns = ["tenure_months", "monthly_spend", "support_tickets"]
customers["churn_score"] = model.predict_proba(customers[feature_columns])[:, 1]
customers[["customer_id", "churn_score"]].to_parquet("scores_2026-11-28.parquet")Three details matter here:
- The input file is named by date. The job reads a fixed snapshot, so rerunning it gives the same input.
feature_columnslists the columns in the order used in training. A different order can give wrong scores without raising an error.- The output keeps only the key and the score. Downstream consumers join it back to other data by
customer_id.
Design choices that make it reliable
Make runs repeatable
Parameterize the job by run date and read the snapshot for that date, not "the latest data". Then you can rerun yesterday's job after a failure and get the same result. A job that is safe to run twice is called idempotent.
Write output atomically
If the job crashes halfway through writing, a downstream reader may see a partial file. Write to a temporary name, then rename it when the write is complete. Many teams also write a small marker file, such as _SUCCESS, so readers know the output is whole.
Validate before publishing
Check the output before it goes live. Typical checks are:
- The row count matches the input row count.
- Scores fall within 0 and 1.
- No scores are null.
- The score distribution is close to the previous run.
If a check fails, stop and keep the previous scores in place. Stale scores are usually better than wrong ones.
Process large inputs in chunks
If the data does not fit in memory, read and score it in pieces. Scoring is a pure function of each row, so chunks can also run in parallel.
import joblib
import pandas as pd
import pyarrow.parquet as pq
model = joblib.load("model.joblib")
feature_columns = ["tenure_months", "monthly_spend", "support_tickets"]
parts = []
for batch in pq.ParquetFile("customers_2026-11-28.parquet").iter_batches(batch_size=100_000):
chunk = batch.to_pandas()
chunk["churn_score"] = model.predict_proba(chunk[feature_columns])[:, 1]
parts.append(chunk[["customer_id", "churn_score"]])
pd.concat(parts).to_parquet("scores_2026-11-28.parquet")Common errors
| Problem | Cause | Fix |
|---|---|---|
| Scores differ from training metrics | Features computed differently in the pipeline | Reuse the training feature code |
| Rerun creates duplicates | Job appends instead of replacing | Overwrite the output for that run date |
| Downstream reads half a file | Non-atomic write | Write to a temp path, then rename |
| Silent drop in quality | Upstream data changed | Add input and output validation |
Scheduling and freshness
The schedule sets how stale a score can be. A daily job means a score can be up to a day old, plus the run time. Pick the interval from how fast the underlying data changes and how the scores are used, then confirm that the run finishes well inside that interval.
Exercise
Your nightly job crashed after writing half of its output file, and downstream systems read the file at 6 a.m. Name two changes that prevent this problem.
Show answer
Write the output to a temporary path and rename it only after the write completes (atomic write), and validate the output (row count, no null scores) before publishing. A success marker file that readers check also works.