Section 6.2: Experiment Tracking and Serving
A tested project still keeps its results in a terminal's scrollback and its model in a file only its own scripts can open. This section adds MLflow 3.17.1, which records every training run's parameters and metrics in a SQLite file and its model in a folder beside it, and lets you compare runs by query, then registers the pipeline as a versioned model with a signature, gives one version the alias champion, and loads it back by that name. Then it serves the champion over HTTP with FastAPI: a /health route first, then a /predict route behind a pydantic schema, with the model loaded once when the service starts.
Section 6.1's suite proved the pipeline behaves. Here the pipeline leaves the project's own process, and every boundary it crosses is strict in its own way. MLflow writes scikit-learn models with skops, which refuses to save this pipeline until its numpy.dtype type is trusted. The registered model's loader refuses a year written as an integer where training saw floats, and a single listing with a blank rating breaks both loaders, until every request is cast to the training dtypes, after which Lesson 3.2.3's three listings price at 384235, 426163 and 434348 exactly. The service's schema answers a bad listing with 422 and four reasons before the model sees it, and converts a room count sent as the text "3" without a word.
Lessons
- MLflow - Tracking Experiments - MLflow in the project, a run with its parameters and metrics, what MLflow prints on every run, the tracking store, comparing runs with
search_runs, and the MLflow UI. - Logging and Loading Models With MLflow - logging the pipeline with a signature, skops and its trusted types, the registry and its versions, an alias and
promote.py, loading by alias as pyfunc and as sklearn, and explicit dtypes. - FastAPI - the First Endpoint - declaring FastAPI and Uvicorn, the app and its first route, serving with Uvicorn, reading responses with
curl -i, the generated docs at/docs, and testing withTestClient. - The Prediction Service - a listing schema with pydantic, what lax mode converts, loading the model once in a lifespan,
POST /predictand its 422s, and testing the service without the registry.
By the end you will be able to:
- Record every training run with MLflow, compare runs by their parameters and metrics, and find the run behind a model file.
- Register a model with a signature, move an alias between versions, and load the version an alias names, knowing what each loader refuses.
- Serve a model with FastAPI and Uvicorn, read its responses with
curl -i, and test it withTestClientwithout starting a server. - Put a schema in front of a model, load the model once per service, and say which wrong inputs the schema refuses and which it converts.