When using MLflow in a Databricks notebook, what is the recommended way to start a new tracking run and ensure it is properly closed?
Select an answer to reveal the explanation.
Short Explanation and Infographic
The 'with' statement is like checking into a hotel — it automatically checks you out when you're done, even if something goes wrong.
Full explanation below image
Full Explanation
Using 'with mlflow.start_run():' as a context manager is the recommended pattern because Python's context manager protocol guarantees that mlflow.end_run() is called even if an exception occurs inside the block. This prevents orphaned runs (runs that remain in RUNNING state). While mlflow.start_run() and mlflow.end_run() can be called explicitly, this approach is error-prone if an exception interrupts execution. mlflow.close_run() is not a valid MLflow API method. Runs are not auto-managed in Databricks — explicit run management is required. The context manager pattern is also composable: you can nest runs for parent/child relationships by passing nested=True.