Model registry version mock in Python

A simple in-memory model registry that stores model versions with metadata and supports version listing and latest retrieval.

Easy Python 3.9+ Aug 9, 2026 ML engineering pipelines 13 views 0 copies

Python code

32 lines
Python 3.9+
class ModelRegistry:
    def __init__(self):
        self.models = {}

    def register(self, name, version, model_type, metrics=None):
        if name not in self.models:
            self.models[name] = []
        entry = {
            "version": version,
            "model_type": model_type,
            "metrics": metrics or {}
        }
        self.models[name].append(entry)
        return f"Registered {model_type} '{name}' version {version}"

    def get_latest(self, name):
        if name not in self.models or not self.models[name]:
            return None
        return self.models[name][-1]

    def list_versions(self, name):
        if name not in self.models:
            return []
        return [entry["version"] for entry in self.models[name]]


if __name__ == "__main__":
    registry = ModelRegistry()
    print(registry.register("sentiment-model", "1.0.0", "LogisticRegression", {"accuracy": 0.87}))
    print(registry.register("sentiment-model", "1.1.0", "RandomForest", {"accuracy": 0.91}))
    print("Versions:", registry.list_versions("sentiment-model"))
    print("Latest:", registry.get_latest("sentiment-model"))

Output

stdout
Registered LogisticRegression 'sentiment-model' version 1.0.0
Registered RandomForest 'sentiment-model' version 1.1.0
Versions: ['1.0.0', '1.1.0']
Latest: {'version': '1.1.0', 'model_type': 'RandomForest', 'metrics': {'accuracy': 0.91}}

How it works

The register method stores each model version as a new dict entry in a list keyed by model name. Using a list preserves insertion order, so the last appended version is treated as the latest. The get_latest method returns the final element of the list, while list_versions extracts just the version strings. Metrics default to an empty dict when not provided, keeping the data structure consistent.

Common mistakes

  • Forgetting to initialize the models list for a new model name before appending
  • Assuming versions arrive in chronological order instead of relying on insertion order
  • Returning the full entry instead of just the version string from `get_latest`

Variations

  1. Use a dict keyed by (name, version) tuple for direct version lookups instead of a list
  2. Add a semantic version comparison to `get_latest` so versions are sorted numerically rather than by insertion

Real-world use cases

  • Prototyping an ML experiment tracker before integrating with a full platform like MLflow.
  • Unit-testing model-serving code that needs a lightweight registry stub.
  • Managing rolling model deployments across environments in a CI/CD pipeline.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from ML engineering pipelines

Related tutorials and quizzes for this topic.