Skip to main content

How to Log Text to a File in MLflow with log_text

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Notes about a run, a text report, a summary someone else will read: when the content is already a string, mlflow.log_text() saves it with the run as a file in one call, without creating anything on your disk. This guide logs a Markdown note and a plain text file and looks at both in the MLflow UI, with real output from MLflow 3.18.0.

Syntax​

Syntax
mlflow.log_text("<text>", "<file name>")
mlflow.log_text("<text>", "<folder>/<file name>")
PartWhat it means
"<text>"The string to save. It must be text: convert anything else with str() first
"<file name>"The file's path in the run, including its extension, such as notes.md or reports/summary.txt. The extension decides how the MLflow UI previews it
The current runThe file goes to the active run; run_id= sends it to another one

MLflow writes the text as UTF-8 into a temporary file, uploads it, and deletes the temporary copy.

Prerequisites​

Step 1: Log a Note and a Summary​

The example logs two files: training notes in Markdown, with a heading, bold labels and a list, and a one-line plain text summary. Create log_text.py:

The log_text.py script in a macOS-style code window: a notes string in Markdown with a Training notes heading and two bold-labelled list items, then a run named log-text that logs it with mlflow.log_text as notes.md and logs the line Trained on 5 houses as summary.txt.

log_text.py
import mlflow

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

notes = """# Training notes

- **Data:** houses.csv, 5 rows
- **Model:** random forest with 100 trees
"""

with mlflow.start_run(run_name="log-text"):
mlflow.log_text(notes, "notes.md")
mlflow.log_text("Trained on 5 houses.\n", "summary.txt")

notes is a multi-line string, written between three double quotes. The .md extension marks it as Markdown.

Terminal
uv run python log_text.py
Output
🏃 View run log-text at: http://127.0.0.1:8080/#/experiments/9/runs/09d7b3bc4bed43269897c08e300da569
🧪 View experiment at: http://127.0.0.1:8080/#/experiments/9

Step 2: See the Files in the UI​

Open the run, click Artifacts, then notes.md:

The Artifacts tab of the log-text run with notes.md selected: 91 bytes, a T and M↓ toggle with M↓ selected, and the Markdown rendered as a Training notes heading followed by two bullets, Data: houses.csv, 5 rows and Model: random forest with 100 trees, with bold labels.

The UI renders Markdown files: a heading, bold labels and a bulleted list. The two buttons above it switch between the plain text (T) and the rendered view (M↓). summary.txt shows as plain text. Each file has a download button at the top right.

Common Mistakes​

MistakeWhat happensDo this instead
Passing a list or a numberTypeError: write() argument must be str, not listJoin or convert it to a string first, such as "\n".join(lines)
Calling log_text twice with the same name in one runThe second text replaces the firstBuild the whole text first, then log it once
Writing a file to disk only to log itExtra code and a leftover fileLog the string directly with log_text

Troubleshooting​

SymptomLikely causeFix
Markdown shows as plain textThe file doesn't end in .md, or T is selectedName it .md and click M↓
Accented or non-Latin characters look wrong in another programThat program isn't reading the file as UTF-8Open it as UTF-8, the encoding MLflow writes

FAQ​

Can I log HTML?​

Yes: MLflow's reference shows mlflow.log_text("<h1>header</h1>", "index.html"), and the UI lists HTML among the formats it can preview.

What's the difference from log_artifact?​

log_text takes a string in memory; log_artifact takes a file that already exists on disk. Use log_artifact for files another program produced: How to Log a File to MLflow with log_artifact.

How do I read the text back?​

mlflow.artifacts.load_text("runs:/<run ID>/notes.md") returns it as a string: How to Read a JSON or Text Artifact from MLflow in Python.

Conclusion​

mlflow.log_text(notes, "notes.md") saved a Markdown string with the run, and the MLflow UI rendered it with a switch to plain text; summary.txt stayed plain. Pass a string, name the file with its extension, and log each file once.

Additional Resources​