Skip to main content

Fixed: MLflow Doesn't Record the Git Commit of a Run Started with uv run

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

A run started through a project command, such as uv run log-run, gets no mlflow.source.git.commit tag, although the project is a Git repository. MLflow 3.18.0 looks for the commit next to the command's launcher in .venv, and Git ignores that folder. Reading the commit yourself and passing it to mlflow.start_run as a tag fixes it.

What I Was Doing​

In Part 10 of the MLflow tutorial series, I checked which code each training run came from. The shopper project starts its runs with commands such as uv run train, and none of its runs had a commit. To see the problem on its own, I made a small package, script-demo, with one command, log-run, that logs a run:

pyproject.toml (excerpt)
[project.scripts]
log-run = "script_demo.log_run:main"
src/script_demo/log_run.py
import mlflow


def main() -> None:
mlflow.set_tracking_uri("http://127.0.0.1:8090")
mlflow.set_experiment("script-demo")
with mlflow.start_run(run_name="from-a-script"):
mlflow.log_param("greeting", "hello")


if __name__ == "__main__":
main()

The folder is a Git repository with everything committed. I ran the command:

Terminal (in script-demo)
uv run log-run
Output
2026/11/28 13:55:46 INFO mlflow.tracking.fluent: Experiment with name 'script-demo' does not exist. Creating a new experiment.
🏃 View run from-a-script at: http://127.0.0.1:8090/#/experiments/2/runs/c5788c992ae94711b8f9d1a59234fd86
🧪 View experiment at: http://127.0.0.1:8090/#/experiments/2

What I Expected​

A mlflow.source.git.commit tag. MLflow's System Tags Reference lists it as the "Git commit hash", set "When run from git repo".

What I Got​

Terminal
uv run mlflow --env-file .env runs describe --run-id c5788c992ae94711b8f9d1a59234fd86
Output (excerpt)
"tags": {
"mlflow.user": "you",
"mlflow.source.name": "/Users/you/source/script-demo/.venv/bin/log-run",
"mlflow.source.type": "LOCAL",
"mlflow.runName": "from-a-script"
}

The terminal output of mlflow runs describe for the run from-a-script: mlflow.source.name ends in .venv/bin/log-run, and there's no mlflow.source.git.commit.

No commit, and the source is .venv/bin/log-run, the launcher uv installed for the command, not the code in src.

What I Checked​

Whether Git ignores the launcher's folder.

Terminal
git check-ignore -v .venv
Output
.gitignore:10:.venv .venv

Line 10 of .gitignore ignores .venv, as it should: the virtual environment doesn't belong in Git.

How MLflow 3.18.0 finds the commit. Its run context takes the source from sys.argv[0], the path of the program Python started with, and passes that path to get_git_commit in mlflow/utils/git_utils.py:

mlflow/utils/git_utils.py (excerpt, MLflow 3.18.0)
if os.path.isfile(path):
path = os.path.dirname(os.path.abspath(path))
repo = Repo(path, search_parent_directories=True)
if path in repo.ignored(path):
return None
return repo.head.commit.hexsha

The launcher's folder is .venv/bin. Git ignores it, so the function returns None and MLflow sets no tag.

Whether a script inside the repository gets the tag. Python can start the same code from its file in src with -m:

Terminal
uv run python -m script_demo.log_run

Described with mlflow runs describe, the run that command logged had the commit, with the source in src:

Output (excerpt)
"mlflow.source.name": "/Users/you/source/script-demo/src/script_demo/log_run.py",
"mlflow.source.type": "LOCAL",
"mlflow.source.git.commit": "7c227654e4eaae4d8ea45e8d602ba68e1fe4d05b",
"mlflow.source.git.branch": "main",

Why It Happens​

MLflow looks for the commit next to the file the program started from. A command installed by a package starts from a launcher in .venv/bin, and Git ignores .venv, so MLflow concludes the code isn't in Git.

The Fix​

Read the commit yourself and pass it to mlflow.start_run as the same tag. start_run's tags parameter is "An optional dictionary of string keys and values to set as tags on the run":

src/script_demo/log_run.py
import subprocess
from pathlib import Path

import mlflow


def git_commit_tag() -> dict[str, str]:
"""MLflow's commit tag for this code, or no tag outside a Git repository."""
try:
result = subprocess.run(
["git", "rev-parse", "HEAD"],
cwd=Path(__file__).parent,
capture_output=True,
text=True,
check=True,
)
except OSError, subprocess.CalledProcessError:
return {}
return {"mlflow.source.git.commit": result.stdout.strip()}


def main() -> None:
mlflow.set_tracking_uri("http://127.0.0.1:8090")
mlflow.set_experiment("script-demo")
with mlflow.start_run(run_name="from-a-script", tags=git_commit_tag()):
mlflow.log_param("greeting", "hello")


if __name__ == "__main__":
main()

log_run.py in a code editor: git_commit_tag runs git rev-parse HEAD next to the file and returns the mlflow.source.git.commit tag, which main passes to mlflow.start_run.

git rev-parse HEAD runs in the file's folder, inside the repository, so it finds the commit wherever the command starts. Outside Git, the function returns no tag and the run goes ahead.

After the Fix​

I committed the change and ran the same command:

Terminal
uv run log-run
Output
🏃 View run from-a-script at: http://127.0.0.1:8090/#/experiments/2/runs/40e85fa4cd5d40a8aa023ae232be40aa
🧪 View experiment at: http://127.0.0.1:8090/#/experiments/2
Terminal
uv run mlflow --env-file .env runs describe --run-id 40e85fa4cd5d40a8aa023ae232be40aa
Output (excerpt)
"mlflow.source.name": "/Users/you/source/script-demo/.venv/bin/log-run",
"mlflow.source.type": "LOCAL",
"mlflow.source.git.commit": "1354555e48dea8aaa128baf57eb6bd24d02d7405",
Terminal
git rev-parse HEAD
Output
1354555e48dea8aaa128baf57eb6bd24d02d7405

The terminal: the run from-a-script, started with uv run log-run, now has mlflow.source.git.commit 1354555e48dea8aaa128baf57eb6bd24d02d7405, the same hash git rev-parse HEAD prints.

The run started through the command now records the repository's commit.

Common Mistakes​

MistakeWhat happensDo this instead
Removing .venv from .gitignore so MLflow finds the commitGit starts tracking the virtual environmentKeep .venv ignored and pass the tag
Training before committingThe tag names the previous commitCommit, then run

Troubleshooting​

SymptomLikely causeFix
mlflow.source.name ends in .venv/bin/<command> and there's no commitThe run started through a package's commandPass the tag, or start the file with python -m

FAQ​

Can I fix it without changing the code?​

Yes, by starting the module with uv run python -m script_demo.log_run: MLflow then sees the file in src and records the commit and the branch itself. You lose the short command, so the tag is the better fix for a project's commands.

Does the branch tag matter?​

MLflow sets mlflow.source.git.branch when it finds the commit itself, as the -m run shows. The commit alone identifies the code exactly.

Conclusion​

MLflow 3.18.0 skips the commit for runs started through a package's command, because it looks next to the launcher in the Git-ignored .venv. Reading the commit with git rev-parse HEAD and passing {"mlflow.source.git.commit": ...} to mlflow.start_run gave the run started with uv run log-run its commit, 1354555….

Additional Resources​