docs/docs/genai/version-tracking/track-application-versions-with-mlflow.mdx
import FeatureHighlights from "@site/src/components/FeatureHighlights"; import ConceptOverview from "@site/src/components/ConceptOverview"; import TilesGrid from "@site/src/components/TilesGrid"; import TileCard from "@site/src/components/TileCard"; import { APILink } from "@site/src/components/APILink"; import { GitBranch, GitCommit, Shield, Code2, Database, Settings, BarChart3, PlayCircle } from "lucide-react"; import ImageBox from '@site/src/components/ImageBox'; import GitVersioningImage from '@site/static/images/git-versioning.png';
:::warning
:::
This guide demonstrates how to track versions of your LLM application or AI agent when your app's code resides in Git or a similar version control system. MLflow provides automatic Git-based versioning through the <APILink fn="mlflow.genai.enable_git_model_versioning" /> API, which seamlessly tracks your application versions based on Git state.
When enabled, MLflow automatically:
LoggedModel versionMLflow tracks three key components of your Git state:
main, feature-xyz)Git-based versioning transforms your version control system into a powerful application lifecycle management tool. Every commit becomes a potential application version, with complete code history and change tracking built-in.
<FeatureHighlights features={[ { icon: GitCommit, title: "Commit-Based Versioning", description: "Use Git commit hashes as unique version identifiers. Each commit represents a complete application state with full reproducibility." }, { icon: GitBranch, title: "Branch-Based Development", description: "Leverage Git branches for parallel development. Feature branches become isolated version streams that can be merged systematically." }, { icon: Shield, title: "Automatic Metadata Capture", description: "MLflow automatically captures Git commit, branch, and repository URL during runs. No manual version tracking required." }, { icon: Database, title: "Seamless Integration", description: "Works naturally with your existing Git workflow. No changes to development process or additional tooling required." } ]} />
With <APILink fn="mlflow.genai.enable_git_model_versioning" />, MLflow automatically manages version tracking based on your Git state. Each unique combination of branch, commit, and dirty state creates or reuses a LoggedModel version.
<ConceptOverview concepts={[ { icon: Code2, title: "Automatic Git Detection", description: "MLflow detects Git repositories and automatically captures commit hash, branch name, repository URL, and uncommitted changes." }, { icon: Settings, title: "Zero-Configuration Versioning", description: "Simply call enable_git_model_versioning() once—MLflow handles all version management and trace linking automatically." }, { icon: Database, title: "Smart Version Deduplication", description: "MLflow intelligently reuses existing LoggedModels when Git state matches, avoiding version proliferation." } ]} />
Install MLflow and required packages:
pip install 'mlflow>=3.4' openai
Set your OpenAI API key:
export OPENAI_API_KEY="your-api-key-here"
Create an MLflow experiment by following the getting started guide.
The simplest way to enable Git-based version tracking is to call <APILink fn="mlflow.genai.enable_git_model_versioning" /> at the start of your application:
import mlflow
# Enable Git-based version tracking
# This automatically creates/reuses a LoggedModel based on your Git state
context = mlflow.genai.enable_git_model_versioning()
# Check which version is active
print(f"Active version - Branch: {context.info.branch}, Commit: {context.info.commit[:8]}")
print(f"Repository dirty: {context.info.dirty}")
You can also use it as a context manager for scoped versioning:
with mlflow.genai.enable_git_model_versioning() as context:
# All traces within this block are linked to the Git-based version
# Your application code here
...
# Version tracking is automatically disabled when exiting the context
Now let's create a simple application that will be automatically versioned:
import mlflow
import openai
# Enable Git-based version tracking
context = mlflow.genai.enable_git_model_versioning()
# Enable MLflow's autologging to instrument your application with Tracing
mlflow.openai.autolog()
# Set up OpenAI client
client = openai.OpenAI()
# Use the trace decorator to capture the application's entry point
@mlflow.trace
def my_app(input: str) -> str:
"""Customer support agent application"""
# This call is automatically instrumented by `mlflow.openai.autolog()`
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You are a helpful customer support agent."},
{"role": "user", "content": input},
],
temperature=0.7,
max_tokens=150,
)
return response.choices[0].message.content
# Test the application - traces are automatically linked to the Git version
result = my_app(input="What is MLflow?")
print(result)
:::note
When you run this code, MLflow automatically:
:::
Run your application and observe how versions are tracked:
# Initial run - creates a LoggedModel for current Git state
result = my_app(input="What is MLflow?")
print(result)
result = my_app(input="What is Databricks?")
print(result)
# Both traces are linked to the same version since Git state hasn't changed
To see how MLflow tracks changes, modify your code (without committing) and run again:
# Make a change to your application code (e.g., modify temperature)
# The repository is now "dirty" with uncommitted changes
# Re-enable versioning - MLflow will detect the dirty state
context = mlflow.genai.enable_git_model_versioning()
print(f"Repository dirty: {context.info.dirty}") # Will show True
# This trace will be linked to a different version (same commit but dirty=True)
result = my_app(input="What is GenAI?")
print(result)
:::tip MLflow creates distinct versions for:
This ensures complete reproducibility of your application versions. :::
Go to the MLflow Experiment UI. In the Traces tab, you can see the version of the app that generated each trace. In the Models tab, you can see each LoggedModel alongside its parameters and linked traces.
<ImageBox src={GitVersioningImage} altText="Git Versioning" width="80%" />You can use <APILink fn="mlflow.search_traces" /> to query for traces from a LoggedModel:
import mlflow
# Using the context from enable_git_model_versioning()
context = mlflow.genai.enable_git_model_versioning()
traces = mlflow.search_traces(model_id=context.active_model.model_id)
print(traces)
You can use <APILink fn="mlflow.get_logged_model" /> to get details of the LoggedModel including Git metadata:
import mlflow
import datetime
# Get the active Git-based version
context = mlflow.genai.enable_git_model_versioning()
# Get LoggedModel metadata
logged_model = mlflow.get_logged_model(model_id=context.active_model.model_id)
# Inspect basic properties
print(f"\n=== LoggedModel Information ===")
print(logged_model)
# Access Git metadata from tags
print(f"\n=== Git Information ===")
git_tags = {k: v for k, v in logged_model.tags.items() if k.startswith("mlflow.git")}
for tag_key, tag_value in git_tags.items():
if tag_key == "mlflow.git.diff" and len(tag_value) > 100:
print(f"{tag_key}: <diff with {len(tag_value)} characters>")
else:
print(f"{tag_key}: {tag_value}")
Now that you understand the basics of Git-based application versioning with MLflow, you can explore these related topics:
<TilesGrid> <TileCard href="/genai/version-tracking/compare-app-versions" title="Compare App Versions" description="Learn systematic approaches to evaluate different versions using trace-based comparison" icon={BarChart3} /> <TileCard href="/genai/version-tracking/quickstart" title="Version Tracking Quickstart" description="Get started quickly with a hands-on guide to version tracking in MLflow" icon={PlayCircle} /> </TilesGrid>