MLflow 3.x experiment tracking and model registry conventions. Use when logging experiments, registering models, promoting model versions, or configuring MLflow tracking.
Always set from Pydantic config — never rely on defaults:
import mlflow
mlflow.set_tracking_uri(config.mlflow_tracking_uri)
mlflow.set_experiment(config.experiment_name)
ALWAYS use context manager — never manual end_run():
with mlflow.start_run(run_name="retrain-2024-01-15") as run:
mlflow.log_params({
"model_type": "random_forest",
"n_estimators": 100,
"max_depth": 10,
})
# Train model...
mlflow.log_metrics({
"accuracy": accuracy,
"f1_score": f1,
"precision": precision,
"recall": recall,
})
# Log model (not pickle!)
mlflow.sklearn.log_model(model, artifact_path="model")
# Log Evidently report as artifact
mlflow.log_artifact("drift_report.html", artifact_path="reports")
# Tag for traceability
mlflow.set_tags({
"pipeline_version": "1.0.0",
"trigger_reason": "drift_detected",
"data_hash": data_hash,
"git_sha": git_sha,
})
# Register model from a run
model_uri = f"runs:/{run.info.run_id}/model"
mlflow.register_model(model_uri, name="churn-classifier")
# Promote to production (using aliases in MLflow 3.x)
client = mlflow.MlflowClient()
client.set_registered_model_alias(
name="churn-classifier",
alias="production",
version=new_version,
)
# Load production model
model = mlflow.sklearn.load_model("models:/churn-classifier@production")
log_* calls MUST be inside with mlflow.start_run(): contextpipeline_version, data_hash, trigger_reasonmlflow.sklearn.log_model() — never pickle or joblib@production, @staging) instead of deprecated stagesrun_name with date/trigger info for easy identification