Skip to main content

How to Read a JSON or Text Artifact from MLflow in Python

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

When code needs a value from a file another run logged, such as a setting from its configuration, downloading the file first is a detour. mlflow.artifacts.load_dict() returns a JSON or YAML file as a Python dictionary, and load_text() returns any text file as a string, in one call each. This guide reads a run's settings back both ways, with real output from MLflow 3.18.0.

Syntax​

Syntax
<dictionary> = mlflow.artifacts.load_dict("runs:/<run ID>/<path in the run>")
<text> = mlflow.artifacts.load_text("runs:/<run ID>/<path in the run>")
PartWhat it means
"runs:/<run ID>/<path>"The file's address: the run's ID, then the file's place in the run's artifacts, such as settings.json or evaluation/notes.md
load_dict"Loads the artifact contents as a dictionary." A .yml or .yaml file is read as YAML, anything else as JSON, mirroring mlflow.log_dict
load_text"Loads the artifact contents as a string."

Both download the file into a temporary folder, read it, and delete the folder. The run's own run.info.artifact_uri, such as mlflow-artifacts:/9/<run ID>/artifacts, followed by /settings.json, works as an address too.

Prerequisites​

The Run Used Here​

The run log-a-dict logged one dictionary as settings.json and as settings.yaml: a model's kind, number of trees, a depth limit that isn't set, and its features. How to Log a Dictionary to MLflow as a JSON or YAML File creates it.

Step 1: Read Both Files​

The script reads settings.json as a dictionary and uses one of its values, then reads settings.yaml as text. Create load_files.py, which takes the run ID on the command line:

The load_files.py script in a macOS-style code window: it sets the tracking URI, takes the run ID from the command line, loads settings.json with mlflow.artifacts.load_dict from a runs:/ address and prints it and its n_estimators value, then prints settings.yaml loaded with mlflow.artifacts.load_text.

load_files.py
import sys

import mlflow

mlflow.set_tracking_uri("http://127.0.0.1:8080")
run_id = sys.argv[1]

settings = mlflow.artifacts.load_dict(f"runs:/{run_id}/settings.json")
print(settings)
print("Trees:", settings["n_estimators"])

print(mlflow.artifacts.load_text(f"runs:/{run_id}/settings.yaml"))
LineWhat it does
f"runs:/{run_id}/settings.json"Builds the file's address from the run ID
load_dict(...)Returns the JSON file as a dictionary
settings["n_estimators"]Reads one value, like any dictionary
load_text(...)Returns the YAML file exactly as it was written, as one string

Step 2: Run It​

Terminal
uv run python load_files.py 32d06744932c453582a9d22732d1f0b5
Output
{'model': 'random_forest', 'n_estimators': 100, 'max_depth': None, 'features': ['rooms', 'size_m2']}
Trees: 100
features:
- rooms
- size_m2
max_depth: null
model: random_forest
n_estimators: 100

A terminal running uv run python load_files.py: the dictionary with model random_forest, n_estimators 100, max_depth None and features rooms and size_m2, then Trees: 100, then the YAML text with features, max_depth null, model and n_estimators.

  • The dictionary came back with its Python types: 100 is a number and JSON's null is None again, in the order it was logged.
  • Trees: 100 reads one value straight from it.
  • The YAML text is the file as stored, keys in alphabetical order. load_dict(".../settings.yaml") would return it as a dictionary instead.

Your run ID will differ: copy it from the run page or the View run link.

Common Mistakes​

MistakeWhat happensDo this instead
A path that isn't in the runMlflowException: Failed to download artifacts from path 'nothere.json', please ensure that the path is correct.Copy the path from the run's Artifacts tab
load_dict on a file that isn't JSON or YAML, such as summary.txtMlflowException: Unable to form a JSON object from file contentUse load_text for plain text
Reading a table logged with log_table with load_dictYou get its raw layout, columns and data, rather than a tableUse mlflow.load_table() for tables

Troubleshooting​

SymptomLikely causeFix
Run with id=... not foundThe run ID is wrong, or the tracking URI points to another serverCheck both against the run page
The dictionary's keys are in alphabetical orderIt was read from a YAML file, which MLflow writes with sorted keysRead the .json copy if the order matters

FAQ​

Can I read images the same way?​

Yes: mlflow.artifacts.load_image("runs:/<run ID>/<path>.png") returns a picture as a PIL.Image object. It needs the Pillow package.

Is anything left on my disk?​

No. Both functions download into a temporary folder, read the file and delete the folder.

How do I read a table?​

mlflow.load_table("<file name>", run_ids=[...]) returns it as a pandas DataFrame: How to Load a Logged Table from MLflow with load_table.

Conclusion​

mlflow.artifacts.load_dict("runs:/<run ID>/settings.json") returned a logged JSON file as a dictionary, with None and numbers restored, and load_text() returned a YAML file as the text that was stored. Use runs:/ addresses with the path from the Artifacts tab, load_dict for JSON and YAML, and load_text for everything else.

Additional Resources​