Once an MLflow run is finished, external scripts can access its parameters and metrics using python mlflow
client and mlflow.get_run(run_id)
method, but the Run
object returned by get_run
seems to be read-only.
Specifically, .log_param
.log_metric
, or .log_artifact
cannot be used on the object returned by get_run
, raising errors like these:
AttributeError: 'Run' object has no attribute 'log_param'
If we attempt to run any of the .log_*
methods on mlflow
, it would log them into to a new run with auto-generated run ID in the Default
experiment.
Example:
final_model_mlflow_run = mlflow.get_run(final_model_mlflow_run_id)
with mlflow.ActiveRun(run=final_model_mlflow_run) as myrun:
# this read operation uses correct run
run_id = myrun.info.run_id
print(run_id)
# this write operation writes to a new run
# (with auto-generated random run ID)
# in the "Default" experiment (with exp. ID of 0)
mlflow.log_param("test3", "This is a test")
Note that the above problem exists regardless of the Run
status (.info.status
can be both "FINISHED" or "RUNNING", without making any difference).
I wonder if this read-only behavior is by design (given that immutable modeling runs improve experiments reproducibility)? I can appreciate that, but it also goes against code modularity if everything has to be done within a single monolith like the with mlflow.start_run()
context...