MLOpsML Experimentation & Pipeline Engineering

How do you log parameters, metrics, files, and model artifacts with MLflow?

MLflow logs the inputs, measurements, supporting files, and trained model produced by an ML run. Four common operations are: log_params for configuration, log_metric for measured values, log_artifact for ordinary files, and a model-flavor logger such as mlflow.sklearn.log_model for a loadable model.

Question 76 described *what a tracking system should retain*; Question 77 organized experiments and runs. Here we focus on how to log each kind of output correctly with working Python code.

Beginner-friendly example

A food-delivery company wants to predict delivery duration from distance. It trains a simple regression model on historical orders, then records:

  • Parameters: the training configuration, such as fit_intercept=True.
  • Metrics: validation mean absolute error (MAE) in minutes.
  • Files: a plain-text evaluation report.
  • Model artifact: a saved scikit-learn regression model that can later be loaded for prediction.

Suppose the validation labels are 20, 30, and 40 minutes, while predictions are 22, 27, and 41 minutes.

The absolute errors are 2, 3, and 1 minute, so:

MAE = (2 + 3 + 1) / 3 = 2.0 minutes.

That 2.0 is a metric, not a parameter. It is calculated from predictions and actual values, whereas fit_intercept=True is a setting chosen before training.

Complete minimal MLflow example

Install the dependencies in an isolated environment:

BASH
pip install mlflow scikit-learn numpy

Then use this self-contained demonstration:

PYTHON
from pathlib import Path from tempfile import TemporaryDirectory import mlflow import mlflow.sklearn import numpy as np from sklearn.linear_model import LinearRegression from sklearn.metrics import mean_absolute_error # Local demo: MLflow writes run data under this tracking directory. mlflow.set_tracking_uri("file:./mlruns") mlflow.set_experiment("delivery-time-regression") # Distance in km → actual delivery duration in minutes. X_train = np.array([[1.0], [2.0], [3.0], [4.0]]) y_train = np.array([10.0, 15.0, 20.0, 25.0]) X_valid = np.array([[1.5], [2.5], [3.5]]) y_valid = np.array([12.5, 17.5, 22.5]) with mlflow.start_run(run_name="distance-linear-baseline"): model = LinearRegression(fit_intercept=True) mlflow.log_params({ "model_family": "linear_regression", "fit_intercept": True, "training_rows": len(X_train), }) model.fit(X_train, y_train) predictions = model.predict(X_valid) mae = mean_absolute_error(y_valid, predictions) mlflow.log_metric("validation_mae_minutes", float(mae)) with TemporaryDirectory() as tmp: report_path = Path(tmp) / "evaluation.txt" report_path.write_text( f"Validation rows: {len(y_valid)}\n" f"Validation MAE (minutes): {mae:.3f}\n" ) mlflow.log_artifact(str(report_path), artifact_path="reports") info = mlflow.sklearn.log_model( sk_model=model, name="delivery_time_model", input_example=X_valid[:1], ) print("Logged model URI:", info.model_uri)

The example dataset follows an exact linear relationship, so the computed MAE may be extremely close to zero. The earlier 2.0-minute calculation is a separate hand-worked metric example, not an expected result from this code. A production application would use real training data and a held-out dataset.

In recent MLflow versions, the name argument names the logged model. Older examples often use artifact_path, but prefer the form supported by your installed MLflow version.

What is the difference between the logging functions?

Parameters: mlflow.log_param("learning_rate", 0.01) or mlflow.log_params({...}). These describe the configuration of a run. Do not treat them as a time series or repeatedly overwrite the same parameter key with different values.

Metrics: mlflow.log_metric("val_mae", 2.0). Metrics are numeric and can be logged repeatedly with steps for a learning curve:

PYTHON
for epoch, loss in enumerate([0.8, 0.5, 0.3], start=1): mlflow.log_metric("training_loss", loss, step=epoch)

Log this loop *inside an active run*. Choose meaningful metric names and include units or record the definition and evaluation dataset.

Files: mlflow.log_artifact("evaluation.txt", artifact_path="reports") uploads or copies one local file as a run artifact. mlflow.log_artifacts("plots/") logs a whole directory. Files can include reports, charts, JSON summaries, and similar outputs.

Models: mlflow.sklearn.log_model(...) packages a trained model with MLflow model metadata and flavor-specific loading information. A generic log_artifact("model.pkl") saves a file but does not by itself create the same standardized MLflow Model format.

For several frameworks, use the relevant flavor logger (for example mlflow.pytorch.log_model) rather than assuming a scikit-learn loader understands every model.

How do you retrieve the logged model?

A logged model's URI can be used to load the model:

PYTHON
loaded = mlflow.sklearn.load_model(info.model_uri) prediction = loaded.predict([[2.0]]) print(prediction)

Run this in an environment that can access the MLflow tracking/artifact storage and has compatible dependencies. Saving a model does not automatically deploy it to a production endpoint.

Operational checks

  1. Start a run intentionally. A with mlflow.start_run() context provides clear boundaries and records success or failure.
  2. Log actual measured metrics. Do not log fabricated metrics as though a model computed them.
  3. Check artifact availability. Confirm reports and models are retrievable, not merely present in the UI listing.
  4. Avoid sensitive data. Do not put API keys, raw customer records, or private credentials in params, tags, or exported files.
  5. Choose consistent metric names. validation_mae_minutes is more informative than an ambiguous score.
  6. Use durable storage in teams. Local file:./mlruns is suitable for a demo; a shared tracking server and configured persistent artifact store are generally preferable for collaboration.
  7. Record dataset/evaluation context. Reference input dataset identities and evaluation splits so the numeric metrics can be interpreted.

Common mistakes

  • Logging a hyperparameter as a metric and losing its fixed configuration meaning.
  • Logging just the final score without the training setup.
  • Saving a pickle as a generic file while assuming MLflow recorded model flavor metadata.
  • Logging thousands of redundant metrics without a meaningful step or monitoring purpose.
  • Comparing numerical metrics from different evaluation datasets without noting the difference (Question 79).
  • Assuming tracking or model logging automatically guarantees reproducibility, quality, or production deployment.

In short: Use `log_params` for configuration, `log_metric` for numeric results, `log_artifact` for files, and a framework-specific `log_model` for reusable trained models. Verify the records and artifacts after the run ends.

#MLflow log parameters metrics artifacts models#mlflow.sklearn.log_model example#MLflow log_artifact and log_metrics#MLflow complete experiment logging tutorial#MLflow tracking Python example#mlops#mlflow#experiment-tracking#model-artifacts#metrics

Learning Sequence

MLOps Curriculum

74% Completed
Up NextMLOps

How do you compare runs when datasets or evaluation protocols differ?

Explore Related Questions

Deepen your understanding across related MLOps topics.

Machinoai Logo
Machinoai

Your edge in the AI era. Deep dives into machine learning concepts, paper explainers, verified industry jobs, and developer tools.

© 2026 Machinoai. All rights reserved.