CertSafari
    Databricks Certified Machine Learning Professional· Lessons

    Domain 1 · Lesson 3/11

    MLflow nested runs and programmatic logging

    Advanced MLflow Usage

    14 min read
    11% of exam
    5 sources
    Published 21 Oct 2026
    Docs as of 17 Oct 2026

    What you will be able to do

    • Control which tracking server and experiment an MLflow run is written to, and predict where a run goes when no experiment is set
    • Log parameters, step-wise metrics, tags and artifacts from code, including to a run that has already finished
    • Log computed metrics and parameters in bulk with dictionaries, and attach saved files with log_artifact
    • Structure tuning trials, cross-validation folds and ensemble members as child runs under one parent with nested=True
    • Query an experiment's runs as a DataFrame and filter them by metric, parameter or tag

    Key concept

    Parent and child runs — A run opened with mlflow.start_run(nested=True) while another run is active becomes a child of that run. It is a separate run in the same experiment with its own parameters and metrics, and it is linked back to the parent so the whole job can be read as one tree.

    1.Where MLflow runs and experiments are stored

    MLflow tracking organizes model development into experiments and runs. A run is one execution of model code. An experiment is a collection of related runs that you compare and filter. Every logging call you make goes to a run, the run belongs to an experiment, and the experiment lives on a tracking server. On Databricks that server is hosted for you and keeps experiment data in your workspace, with no setup.

    Two settings decide where your data lands. The tracking URI picks the server and defaults to the current workspace. mlflow.set_tracking_uri("databricks://remote-workspace-url") points at a different one. The experiment picks the container on that server, and you set it with mlflow.set_experiment("/Shared/my-experiment"). A workspace experiment like that can take runs started from any notebook or from the API.

    The default is the notebook experiment. It has the same name and ID as the notebook, and only runs started inside that notebook can be logged to it. That suits solo exploration. When a job, a second notebook or an API client has to write to the same place, set a workspace experiment explicitly. One side effect is worth knowing: deleting a notebook experiment through the API moves the notebook itself to Trash.

    The two settings that route MLflow tracking data
    SettingCallDefault when unset
    Tracking URI (which server)mlflow.set_tracking_uri(...)The current Databricks workspace
    Experiment (which container)mlflow.set_experiment("/Shared/...")The notebook experiment

    Checkpoint 1 of 7· Fill the gap

    A scheduled job and a teammate's notebook both need to log their runs to the same experiment. Which call completes this sample so that runs from either one land in that shared location?

    experiment_name = "/Shared/name_of_experiment/"
    mlflow. ? (experiment_name)

    Sources12

    2.Logging parameters, step-wise metrics, tags and artifacts

    Autologging is the quickest way to start: MLflow records training code for many frameworks without any extra calls. It only knows what the framework exposes, though. To choose exactly which parameters and metrics are recorded, or to attach files such as a CSV or a plot, use the logging API. The two work together. Autologging captures the standard parameters and metrics, and explicit calls in the same run add things it can't know about, such as a business metric.

    Each kind of data has its own job. Parameters and metrics are key-value pairs. A parameter describes an input, such as a learning rate. A metric records a result, and you can log it again with a step to build a curve. Tags are key-value metadata you add so you can search for runs later. Artifacts are output files, and they appear in the run's Artifacts tab.

    Attaching to an existing run by ID, logging a parameter, then a metric once per steppython
    # Log from rank 0 only; all nodes share the same MLFLOW_RUN_ID.
    if os.environ.get("RANK", "0") == "0":
        with mlflow.start_run(run_id=os.environ["MLFLOW_RUN_ID"]):
            mlflow.log_param("learning_rate", 3e-4)
            for step, loss in enumerate(training_losses):
                mlflow.log_metric("train_loss", loss, step=step)

    Two details in that snippet matter for the exam. Passing run_id= to mlflow.start_run reopens a run that already exists instead of creating a new one. Use it when a metric computed later, perhaps in another notebook, belongs on a run whose process has already exited. Also, step=step keeps every value of train_loss as a separate point. Without steps, repeated values of one key are hard to tell apart, and run search compares runs on the last value logged.

    Custom logging means the names and values come from your code, so they can be computed at any point while the run is open. A revenue-impact figure worked out after evaluation, a threshold chosen at runtime and a summary of the best Optuna trial are all logged the same way as a learning rate.

    Logging a computed metric, then the winning hyperparameters under names built in codepython
    mlflow.log_metric("best_trial_logloss", study.best_trial.value)
        mlflow.log_params({f"best_{key}": value for key, value in study.best_trial.params.items()})

    log_param and log_metric take one key and one value. log_params, log_metrics and set_tags take a dictionary, which suits values produced in a loop or a comprehension. Here the best trial's hyperparameters are logged with a best_ prefix built in code, so the winning values are easy to tell apart from the search settings on the same run.

    Artifacts are files, so the file has to exist before it can be logged. mlflow.log_artifact takes a path rather than an in-memory object: save the plot, CSV or table first, then pass its path. Databricks recommends this for large objects such as big pandas tables, images, plots and models.

    Logging a file that was already written to a Unity Catalog volumepython
    mlflow.log_artifact("/Volumes/<catalog>/<schema>/<volume>/myLargeFilePath.txt")

    Checkpoint 2 of 7· Check yourself

    Your training notebook already uses MLflow autologging for a scikit-learn model. You also want a revenue-impact metric and a confusion-matrix PNG recorded on the same run. What is the right approach?

    Sources1345

    3.Nested runs for tuning trials, folds and ensembles

    Some experiments produce many related results that belong to one job: forty tuning trials, five cross-validation folds, or three base learners feeding a stacked ensemble. Putting all of them in one run collapses them, because a repeated metric key overwrites what you can compare. Starting forty unrelated top-level runs loses the fact that they belong together. Nested runs avoid both problems. A parent run holds the job, and each unit of work becomes a child run with its own metrics.

    The pattern is two calls to the same function. Open the parent with with mlflow.start_run():. Inside that block, open each child with mlflow.start_run(nested=True). Optionally give it a name, for example mlflow.start_run(run_name="base_learner", nested=True). With Optuna, the child call goes inside the objective function, so each trial gets its own run. For cross-validation it goes inside the fold loop, and for an ensemble it is called once per base learner. nested is an argument to start_run, never to log_metric, because nesting is a relationship between runs, not between individual values.

    Children are real, independent runs in the same experiment as their parent. A fold's AUC is logged to that fold's child and does not overwrite anything on the parent. After the loop ends and the children have closed, the parent is the active run again. That makes it the place to log the roll-up, such as mean AUC across folds. You don't need an experiment per fold or per trial. An experiment is a collection of related runs, and the parent-child link already provides the grouping.

    Ways to group related results, and why only nesting gives a linked tree
    ApproachWhat you get
    Parent start_run(), then start_run(nested=True) per trial/fold/learnerOne parent with one linked child run per unit, each holding its own metrics
    Reuse one run and log every trial into itOne run; per-trial values of the same key collapse into one series
    Separate top-level runs sharing a custom tagIndependent runs you must regroup by hand with a tag search
    One experiment per trial or foldResults scattered across experiments, with no parent to roll them up

    Distributed work follows the same model. In Ray, the driver opens the parent run, and each remote task gets the parent's run_id plus credentials so it can open its child run from a different process. The run_id is what ties a child to its parent, even when they run on different machines.

    Checkpoint 3 of 7· Check yourself

    For five cross-validation folds, a teammate suggests creating a separate MLflow experiment for each fold so the per-fold AUC values don't collide. Which design keeps each fold's AUC separate and still groups the folds as one job?

    Checkpoint 4 of 7· Exam question

    A data science team is using Optuna to tune hyperparameters for a gradient boosting model in a Databricks notebook. They want every trial's parameters and metrics logged as a distinct MLflow run, but all trials should be grouped visually under a single parent run representing the overall tuning job. Which approach achieves this?

    Checkpoint 5 of 7· Exam question

    A team trains three different base learners (a random forest, a gradient boosted tree, and a linear model) and then combines their predictions into a stacked ensemble. They want the MLflow experiment to show one top-level run for the ensemble and three linked runs underneath it, one per base learner. Which pattern satisfies this requirement?

    Sources51

    4.Querying runs and filtering child runs by parent

    Run data can be read as a DataFrame. mlflow.search_runs() returns a pandas DataFrame with one row per run, and the MLflow experiments data source returns the same data as a Spark DataFrame for larger analyses or dashboards.

    Filters use the same syntax as the experiment page's search box. Examples are metrics.r2 > 0.3, params.elasticNetParam = 0.5, and MIN(metrics.rmse) <= 1 to match on the minimum value instead of the last one. Tags use the form tags.<key>="<value>", with the value in quotes. Each child run records its parent's ID in the mlflow.parentRunId tag. So mlflow.search_runs(filter_string="tags.mlflow.parentRunId = '<parent_run_id>'") returns only the children of one parent, for example the five folds of a single model configuration.

    Checkpoint 6 of 7· Check yourself

    You need to analyse metrics from thousands of runs with Spark for a dashboard. Which way of reading MLflow run data gives you an Apache Spark DataFrame directly?

    Checkpoint 7 of 7· Exam question

    A team performs 5-fold cross-validation for a classifier and wants each fold's metrics logged separately while still being able to roll results up under one parent run per model configuration. Which of the following statements about implementing this with MLflow nested runs are correct?(Select 3)

    0 of 3 selected

    Sources13

    Sources

    (5)

    Every claim above is drawn from one of these pages, quoted as it was written on the date shown.

    1. 1.
      “Tracking URI: Determines which server to use (defaults to current Databricks workspace)”
      ↩︎ Where MLflow runs and experiments are stored
      “If no active experiment is set, runs are logged to the notebook experiment.”
      ↩︎ Where MLflow runs and experiments are stored
      “to log additional artifacts such as CSV files or plots, use the MLflow logging API.”
      ↩︎ Logging parameters, step-wise metrics, tags and artifacts
      “MLflow imposes a quota limit on the number of total parameters, tags, and metric steps for all existing and new runs”
      ↩︎ Logging parameters, step-wise metrics, tags and artifacts
      “An experiment is a collection of related runs.”
      ↩︎ Nested runs for tuning trials, folds and ensembles
      “The MLflow Python client search_runs API returns a pandas DataFrame.”
      ↩︎ Querying runs and filtering child runs by parent
      “Only MLflow runs initiated within a notebook can be logged to the notebook experiment.”
      ↩︎ Exam trap 4
      “or to log additional artifacts such as CSV files or plots, use the MLflow logging API.”
      ↩︎ Checkpoint
      “An experiment is a collection of related runs. In an experiment, you can compare and filter runs to understand how your model performs”
      ↩︎ Checkpoint
      “The MLflow Python client search_runs API returns a pandas DataFrame. The Read MLflow experiments data source returns an Apache Spark DataFrame.”
      ↩︎ Checkpoint
    2. 2.
      “If no experiment is active, Databricks creates a notebook experiment.”
      ↩︎ Where MLflow runs and experiments are stored
    3. 3.
      “Tags are key-value pairs that you can create and use later to search for runs.”
      ↩︎ Logging parameters, step-wise metrics, tags and artifacts
      “You can also search for runs by tag.”
      ↩︎ Querying runs and filtering child runs by parent
    4. 5.
      “Databricks recommends persisting the artifact as a file.”
      ↩︎ Logging parameters, step-wise metrics, tags and artifacts
      “This run acts as a hierarchical container for all subsequent child runs.”
      ↩︎ Nested runs for tuning trials, folds and ensembles
      “This run acts as a hierarchical container for all subsequent child runs.”
      ↩︎ Key concept
      “Each child run can independently log its own metrics.”
      ↩︎ Exam trap 1
      “This setup establishes the hierarchical parent-child relationship between runs.”
      ↩︎ Exam trap 2
      “Directly log the saved file by specifying the path”
      ↩︎ Exam trap 3

    Continue to part 2 of 2

    Custom pyfunc models with real-time feature engineering

    Spotted a mistake, or was something unclear? Tell us.