Sub topic 15
Storing and Naming Models and Outputs
Assume you know basic Python, Git, and how a training script produces a fitted model. This chapter focuses on artifacts: files created by a machine learning workflow that you may need to inspect, compare, deploy, or reproduce later.
What belongs in an artifact
A trained model is only one part of a useful result. Store the model together with the information needed to understand it:
- The serialized model, such as
model.joblibormodel.pt. - Evaluation metrics, such as accuracy or mean absolute error.
- The configuration used for training.
- The data version or dataset identifier.
- The source-code commit.
- The feature schema and expected input columns.
- Training logs or a short summary.
Treat an artifact as immutable after creation. If you improve the model, create a new artifact instead of replacing the old one. This preserves the evidence for earlier experiments.
| Artifact | Example | Purpose |
|---|---|---|
| Model | `model.joblib` | Load the fitted estimator |
| Metrics | `metrics.json` | Compare model quality |
| Configuration | `config.yaml` | Reproduce the training settings |
| Metadata | `metadata.json` | Identify code and data versions |
Choose a naming scheme
A name should identify a run without requiring you to open the file. Include a model name, version or run identifier, and sometimes a timestamp.
For example:
artifacts/
└── churn/
└── run-20261130T100035Z-a13f/
├── model.joblib
├── metrics.json
├── config.yaml
└── metadata.jsonA run identifier is usually safer than putting every parameter in the directory name. Parameter-rich names become long, unstable, and difficult to change. Store the complete parameters in config.yaml and metadata.json.
Use UTC timestamps when time is part of a name. Avoid names such as best_model_final_v7_really_final. “Best” can change when a new run finishes. Record the chosen model in a registry, database, or small pointer file instead.
Write metadata beside the model
A metadata file connects the binary model to its training context. JSON is convenient because many tools can read it.
from datetime import datetime, timezone
import json
from pathlib import Path
def write_metadata(output_dir: str, model_name: str, metrics: dict,
code_commit: str, data_version: str) -> None:
directory = Path(output_dir)
directory.mkdir(parents=True, exist_ok=True)
metadata = {
"model_name": model_name,
"created_at": datetime.now(timezone.utc).isoformat(),
"metrics": metrics,
"code_commit": code_commit,
"data_version": data_version,
}
(directory / "metadata.json").write_text(
json.dumps(metadata, indent=2) + "\n",
encoding="utf-8",
)
write_metadata(
"artifacts/churn/run-20261130T100035Z-a13f",
"churn_classifier",
{"roc_auc": 0.91},
"a13f9c2",
"customers-2026-11-29",
)The model file and metadata should be produced by the same run. Do not manually edit metadata later to make an old result look newer.
Organize promotion and retention
Separate experimental artifacts from models used by an application. A registry can label a particular immutable run as staging or production. The label can move, but the underlying run should remain available.
Define retention rules before storage grows without limit. Keep production models and their metadata for as long as rollback and audit requirements demand. Delete abandoned experiment artifacts only when their records are no longer useful.
Exercise
Why should a production label point to an immutable run directory instead of a file named best_model.joblib?
Show answer
The run directory preserves the exact model, metrics, configuration, code commit, and data version, so the model can be audited or restored without ambiguity.