Sub topic 12
Versioning Data, Models, and Metrics
This chapter assumes Python and pytest basics, Git and pull requests, model training with scikit-learn or a similar library, and basic YAML-based CI. You also need to understand that a model is produced from both code and data.
Why Git alone is not enough
Git versions source code well, but training data and model files can be large. They may also change without a visible code change. A pull request can therefore use a different dataset than the one used to produce the previous model.
A reliable ML result needs a record of its inputs and outputs:
- the exact source-code commit
- the version or content hash of the training data
- the preprocessing and training configuration
- the dependency versions
- the model artifact
- the evaluation dataset
- the measured metrics
A model version without these links is difficult to reproduce and unsafe to promote.
Version each artifact by identity
Use immutable identifiers. An identifier is a value that lets you distinguish one artifact from every other artifact. A content hash, such as SHA-256, is useful because the identifier changes when the file changes.
A practical artifact record might look like this:
| Artifact | Example identity | Why it matters |
|---|---|---|
| Training data | `sha256:8c31...` | Proves which rows trained the model |
| Model file | `sha256:4a90...` | Identifies the exact serialized model |
| Code | Git commit `a13f2b7` | Identifies transformations and training logic |
| Metrics | Run ID `run-2026-11-30-004` | Connects scores to all inputs |
Keep a manifest with every model. A manifest is a small structured file that records the model's lineage, or the chain of inputs that produced it.
flowchart TD
A[Code commit] --> D[Training run]
B[Data hash] --> D
C[Config and dependencies] --> D
D --> E[Model artifact]
D --> F[Metrics record]
E --> G[Promotion decision]
F --> GThe model registry or artifact store should preserve old versions rather than replacing them. A production deployment should point to a specific model version, not to a mutable name such as latest.
Create a reproducible manifest
The following script calculates SHA-256 hashes and writes a JSON manifest. It records file identities, the code commit, and metrics supplied on the command line.
import argparse
import hashlib
import json
from pathlib import Path
def sha256_file(path: Path) -> str:
digest = hashlib.sha256()
with path.open("rb") as file:
for block in iter(lambda: file.read(1024 * 1024), b""):
digest.update(block)
return digest.hexdigest()
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("--data", type=Path, required=True)
parser.add_argument("--model", type=Path, required=True)
parser.add_argument("--commit", required=True)
parser.add_argument("--accuracy", type=float, required=True)
parser.add_argument("--output", type=Path, default=Path("manifest.json"))
args = parser.parse_args()
manifest = {
"code_commit": args.commit,
"data": {
"path": str(args.data),
"sha256": sha256_file(args.data),
},
"model": {
"path": str(args.model),
"sha256": sha256_file(args.model),
},
"metrics": {"accuracy": args.accuracy},
}
args.output.write_text(json.dumps(manifest, indent=2) + "\n")
print(f"wrote {args.output}")
if __name__ == "__main__":
main()For example, a CI job could run:
python make_manifest.py \
--data data/train.csv \
--model artifacts/model.joblib \
--commit "$GITHUB_SHA" \
--accuracy 0.91Store the resulting manifest beside the model. Do not record only a filename. A file called model.joblib can refer to different bytes on different days.
Version metrics with their evaluation context
Metrics are meaningful only with their context. Record the dataset version, split definition, random seed, metric implementation, and threshold used by the quality gate. A score of 0.91 is not comparable if one run measures accuracy and another measures macro F1, or if their test sets differ.
Keep baseline metrics immutable. A candidate should be compared with the baseline that was active when the change was proposed. This prevents a weak candidate from becoming the new baseline simply because an earlier result was overwritten.
Handoff to delivery
CI should publish artifacts only after tests and quality gates pass. A later delivery or retraining process can then consume the manifest, verify the hashes, and deploy the referenced model. If verification fails, stop the handoff instead of silently retraining or downloading a different file.
Exercise
Why should a production deployment reference a model version or hash instead of the name latest?
Show answer
latest can point to different bytes over time. A fixed version or hash makes the deployment reproducible and lets operators identify exactly what is running.