Model Registration and hosting

intelliconnectq
New Contributor III

I have train & tested a model in databricks, now I want to register it and host it. But I am unable too do so. Please find attach snapshot of code & error

intelliconnectq_0-1762230437372.png

 

joelrobin
Databricks Employee
Databricks Employee

Hi @intelliconnectq The above code will fail with AttributeError: 'NoneType' object has no attribute 'info' on the line: model_uri = f"runs:/{mlflow.active_run().info.run_id}/xgboost-model" 

This happens because once the with mlflow.start_run(): block ends, the MLflow run is no longer active, so calling mlflow.active_run() returns None. You cannot fetch run info after the run is closed.​ 

Resolution:
You need to fetch the run ID inside the with block while the run is still active:
with mlflow.start_run() as run:
    mlflow.xgboost.log_model(xgb_model, "xgboost-model", signature=signature)
    mlflow.log_metric("rmse", rmse)
    mlflow.log_metric("mae", mae)
    run_id = run.info.run_id  # Capture run_id here

model_uri = f"runs:/{run_id}/xgboost-model"
registered_model = mlflow.register_model(model_uri, "catalog.schema.RealEstateValueEstimator")
 

View solution in original post

Thank you