Sub topic 10
Limiting Lock-In with a Thin Tracking Interface
This chapter assumes you know basic Python, can run a training script from the command line, and understand hyperparameters, metrics, artifacts, and validation splits. It also assumes you have seen a client communicating with a server.
A tracking library becomes a source of lock-in when training code calls that library everywhere. For example, if every function calls mlflow.log_metric, changing to Weights & Biases requires editing the training code, tests, and utility modules. A thin tracking interface reduces this dependency.
Separate tracking intent from tracking implementation
Training code should express what it wants to record:
- Log the learning rate and model depth.
- Record validation AUC at each step.
- Attach the dataset version as metadata.
- Save the trained model.
It should not need to know whether those operations use MLflow, Weights & Biases, or another tracker. An interface is a small set of methods that describes these operations. An adapter is an implementation that translates the interface into one library's API.
from collections.abc import Mapping
from pathlib import Path
from typing import Protocol
class Tracker(Protocol):
def log_params(self, params: Mapping[str, object]) -> None:
...
def log_metric(self, name: str, value: float, step: int) -> None:
...
def set_tag(self, name: str, value: str) -> None:
...
def log_artifact(self, path: Path) -> None:
...
def finish(self) -> None:
...
class MlflowTracker:
def __init__(
self,
tracking_uri: str,
experiment: str,
run_name: str,
) -> None:
import mlflow
self._mlflow = mlflow
mlflow.set_tracking_uri(tracking_uri)
mlflow.set_experiment(experiment)
mlflow.start_run(run_name=run_name)
def log_params(self, params: Mapping[str, object]) -> None:
self._mlflow.log_params(dict(params))
def log_metric(self, name: str, value: float, step: int) -> None:
self._mlflow.log_metric(name, value, step=step)
def set_tag(self, name: str, value: str) -> None:
self._mlflow.set_tag(name, value)
def log_artifact(self, path: Path) -> None:
self._mlflow.log_artifact(str(path))
def finish(self) -> None:
self._mlflow.end_run()
def train_model(tracker: Tracker) -> None:
tracker.log_params({"max_depth": 6, "learning_rate": 0.1})
tracker.set_tag("data_version", "2026-11-01")
for step, auc in enumerate([0.84, 0.88, 0.91]):
tracker.log_metric("val_auc", auc, step)
model_path = Path("model.txt")
model_path.write_text("trained model placeholder\n", encoding="utf-8")
tracker.log_artifact(model_path)
if __name__ == "__main__":
tracker = MlflowTracker(
tracking_uri="http://127.0.0.1:5000",
experiment="churn-model",
run_name="baseline",
)
try:
train_model(tracker)
finally:
tracker.finish()train_model depends only on Tracker. The MLflow-specific import and method names stay inside MlflowTracker. A WandbTracker can implement the same methods and become a replacement at the application boundary.
Keep the interface small
A useful interface contains operations your team actually needs. Common methods include:
| Interface method | Typical meaning |
|---|---|
| `log_params` | Record values fixed for a run |
| `log_metric` | Record a numeric value at a step |
| `set_tag` | Add searchable descriptive metadata |
| `log_artifact` | Upload a file or directory |
| `finish` | Close the run and flush pending data |
Do not copy every feature from a provider into the interface. A method such as create_mlflow_registered_model makes the abstraction MLflow-shaped. If model registration matters, define a provider-neutral operation such as register_model(name, version) only after comparing how each provider supports it.
Understand what portability cannot solve
The interface does not make trackers identical. MLflow and Weights & Biases differ in naming, run lifecycle, offline behavior, artifact semantics, dashboards, and model registries. An adapter must choose how to map those differences.
For example, one tracker may support nested runs while another uses groups or references. A thin interface should document the chosen common behavior instead of silently pretending the features match.
Common failure modes
- The interface logs only metrics, while important parameters and artifacts still use direct provider calls.
- Adapters silently convert unsupported features, producing incomplete records.
- Metric names or step meanings differ between adapters, making comparisons invalid.
- Run cleanup is skipped after an exception, leaving runs open or buffered.
- The interface grows until it duplicates an entire provider SDK.
- Tests verify that a method was called, but not that the recorded values have the intended meaning.
A practical design test is to implement a small in-memory tracker for unit tests. If training code cannot run with that implementation, it probably contains hidden provider dependencies.
Exercise
You need to switch from MLflow to Weights & Biases. Which part should change first, and what behavior must remain unchanged?
Show answer
Replace or add the adapter at the application boundary. Parameter names, metric names, step semantics, tags, artifact meaning, and run cleanup must remain consistent so results stay comparable.