Skip to main content

How to Log a Dictionary to MLflow as a JSON or YAML File

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

A run's settings, a model's full configuration or a summary of results often already sit in a Python dictionary. mlflow.log_dict() saves one with the run as a JSON or YAML file in a single line, without writing anything to disk yourself. This guide logs the same dictionary in both formats and compares the files, with real output from MLflow 3.18.0.

Syntax​

Syntax
mlflow.log_dict(<dictionary>, "<file name>.json")
mlflow.log_dict(<dictionary>, "<folder>/<file name>.yaml")
PartWhat it means
<dictionary>Any dictionary of values JSON or YAML can hold: text, numbers, True/False, None, lists and other dictionaries
"<file name>"The file's path in the run. "The serialization format (JSON or YAML) is automatically inferred from the extension": .json for JSON, .yml or .yaml for YAML, and JSON for anything else
A folder in the namePuts the file in that folder of the run's artifacts, such as config/settings.yaml

It saves to the active run; run_id= sends it to another one.

Prerequisites​

Step 1: Log a Dictionary Twice​

The example dictionary holds a model's settings: its kind, number of trees, a depth limit that isn't set, and the features it uses. The script logs it as JSON and as YAML. Create log_dict.py:

The log_dict.py script in a macOS-style code window: it sets the artifacts-demo experiment, defines a settings dictionary with model random_forest, n_estimators 100, max_depth None and features rooms and size_m2, and inside a run named log-a-dict logs it with mlflow.log_dict as settings.json and as settings.yaml.

log_dict.py
import mlflow

mlflow.set_tracking_uri("http://127.0.0.1:8080")
mlflow.set_experiment("artifacts-demo")

settings = {
"model": "random_forest",
"n_estimators": 100,
"max_depth": None,
"features": ["rooms", "size_m2"],
}

with mlflow.start_run(run_name="log-a-dict"):
mlflow.log_dict(settings, "settings.json")
mlflow.log_dict(settings, "settings.yaml")
Terminal
uv run python log_dict.py
Output
2026/11/19 16:26:47 INFO mlflow.tracking.fluent: Experiment with name 'artifacts-demo' does not exist. Creating a new experiment.
🏃 View run log-a-dict at: http://127.0.0.1:8080/#/experiments/9/runs/32d06744932c453582a9d22732d1f0b5
🧪 View experiment at: http://127.0.0.1:8080/#/experiments/9

Step 2: Look at Both Files​

Open the run and click Artifacts, then settings.json:

The Artifacts tab of the log-a-dict run with settings.json selected: 122 bytes, its mlflow-artifacts path, and the JSON with model random_forest, n_estimators 100, max_depth null and the features list rooms and size_m2, in the order they were written.

The keys are in the order of the dictionary, None became JSON's null, and the list kept its order. Now settings.yaml:

The Artifacts tab of the log-a-dict run with settings.yaml selected: 83 bytes, with features first as a list of rooms and size_m2, then max_depth null, model random_forest and n_estimators 100, in alphabetical order.

Same values, but the keys are in alphabetical order: features, max_depth, model, n_estimators. MLflow writes YAML with PyYAML's yaml.dump, which sorts keys, and JSON with Python's json.dump, which doesn't. The YAML file is also smaller, 83 bytes against 122.

Which to Use: log_dict or log_params​

log_paramslog_dict
Where it showsThe run's Parameters, the runs table's columns and the compare pageA file on the Artifacts tab
SearchableYes, such as params.max_depth = '8'No
ValuesOne text value per nameAny nesting: lists, dictionaries in dictionaries

Log the few settings you compare runs by as parameters, and the full configuration as a dictionary file.

Common Mistakes​

MistakeWhat happensDo this instead
A value JSON can't store, such as a set {1, 2}No error: it's written as the text "{1, 2}", because MLflow converts what JSON can't store with str()Convert sets to lists before logging
Leaving out the extensionThe file is written as JSON, but its name doesn't say soEnd the name in .json, .yml or .yaml
Expecting the YAML file to keep your key orderKeys come out sortedUse .json when the order matters
Logging the same file name twice in one runThe second call replaces the fileUse one name per dictionary

Troubleshooting​

SymptomLikely causeFix
A value comes back as text like "2026-11-19 10:00:00"It was a date or another object JSON can't store, converted with str()Store it as a string on purpose, or convert it back after loading
The settings don't appear among the run's parameterslog_dict writes a file, not parametersLog the ones you search by with log_params too

FAQ​

How do I read the dictionary back?​

mlflow.artifacts.load_dict("runs:/<run ID>/settings.json") returns it as a dictionary: How to Read a JSON or Text Artifact from MLflow in Python.

Can I log a scikit-learn model's settings this way?​

Yes: mlflow.log_dict(model.get_params(), "model_params.json") saves every setting, defaults included. How to See Every Setting of a scikit-learn Model with get_params shows what that dictionary holds.

Is there a size limit?​

MLflow's reference for log_dict doesn't state one. The file is stored like any other artifact, on the tracking server's artifact storage.

Conclusion​

mlflow.log_dict(settings, "settings.json") and mlflow.log_dict(settings, "settings.yaml") saved one dictionary in two formats, chosen by the file name. JSON kept the key order and YAML sorted it; both turned None into null. Use it for full configurations and summaries, and log_params for the values you search and compare by.

Additional Resources​