Skip to content

Fix undefined model_info variable in quickstart documentation#18671

Merged
harupy merged 2 commits intomasterfrom
copilot/fix-model-loading-error
Nov 5, 2025
Merged

Fix undefined model_info variable in quickstart documentation#18671
harupy merged 2 commits intomasterfrom
copilot/fix-model-loading-error

Conversation

Copy link
Contributor

Copilot AI commented Nov 4, 2025

Fix #18670

🛠 DevTools 🛠

Open in GitHub Codespaces

Install mlflow from this PR

# mlflow
pip install git+https://github.com/mlflow/mlflow.git@refs/pull/18671/merge
# mlflow-skinny
pip install git+https://github.com/mlflow/mlflow.git@refs/pull/18671/merge#subdirectory=libs/skinny

For Databricks, use the following command:

%sh curl -LsSf https://raw.githubusercontent.com/mlflow/mlflow/HEAD/dev/install-skinny.sh | sh -s pull/18671/merge

Step 6 of the quickstart documentation uses model_info.model_uri to load a logged model, but Step 5 never captures the return value from log_model(), causing a NameError when users follow the tutorial.

Changes

Updated Step 5 in both quickstart guides to capture the ModelInfo object:

# Before
mlflow.sklearn.log_model(sk_model=lr, name="iris_model")

# After  
model_info = mlflow.sklearn.log_model(sk_model=lr, name="iris_model")

Files Modified

  • docs/docs/classic-ml/tracking/quickstart/index.mdx
  • docs/docs/classic-ml/getting-started/quickstart.mdx

This allows Step 6 to reference model_info.model_uri as intended.

Original prompt

This section details on the original issue you should resolve

<issue_title>[DOC-FIX] Python code given in Step 6 - Load the model back for inference. fails</issue_title>
<issue_description>### Willingness to contribute

Yes. I can contribute a documentation fix independently.

URL(s) with the issue

Direct URL : https://mlflow.org/docs/latest/ml/tracking/quickstart/#step-6---load-the-model-back-for-inference

Description of proposal (what needs changing)

Python version : 3.13.0b3
MLFlow version : 3.5.1
Jupyter Notebook version : 7.4.7

Going through the MLFlow Tracking Quickstart page, I found that the Python code given at the end of the page under Step 6 does not execute. It has a error in the first line itself :

loaded_model = mlflow.pyfunc.load_model(model_info.model_uri)

The model_info variable isn't declared anywhere in the code snippets given on the page. The error is :

NameError                                 Traceback (most recent call last)
Cell In[59], line 2
      1 # Load the model back for predictions as a generic Python Function model
----> 2 loaded_model = mlflow.pyfunc.load_model(model_info.model_uri)
      4 predictions = loaded_model.predict(X_test)
      6 iris_feature_names = datasets.load_iris().feature_names

NameError: name 'model_info' is not defined

After some investigations, I figured out that at least two arguments should be supplied to mlflow.pyfunc.load_model which are the model URI and the name of the model. I thus modified the first line as follows :

loaded_model = mlflow.pyfunc.load_model("mlflow-artifacts:/916644062995257796/models/m-8cfa42826fdc479eb2512155cc91a54a/artifacts/MLmodel","iris_model")

The URI is the same as the MLModel URI on my local MLFlow server and the model's name is iris_model.

I also had to make a folder with the model name in my local Jupyter notebook directory. Now the error I am getting is :

Downloading artifacts: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:00<00:00, 12.20it/s]
---------------------------------------------------------------------------
MlflowException                           Traceback (most recent call last)
Cell In[58], line 2
      1 # Load the model back for predictions as a generic Python Function model
----> 2 loaded_model = mlflow.pyfunc.load_model("mlflow-artifacts:/916644062995257796/models/m-8cfa42826fdc479eb2512155cc91a54a/artifacts/MLmodel","iris_model")
      3 predictions = loaded_model.predict(X_test)
      5 iris_feature_names = datasets.load_iris().feature_names

File C:\Program Files\Python313\Lib\site-packages\mlflow\tracing\provider.py:567, in trace_disabled.<locals>.wrapper(*args, **kwargs)
    565 try:
    566     is_func_called = True
--> 567     result = f(*args, **kwargs)
    568 finally:
    569     enable()

File C:\Program Files\Python313\Lib\site-packages\mlflow\pyfunc\__init__.py:1140, in load_model(model_uri, suppress_warnings, dst_path, model_config)
   1137     model_requirements = _get_pip_requirements_from_model_path(local_path)
   1138     warn_dependency_requirement_mismatches(model_requirements)
-> 1140 model_meta = Model.load(os.path.join(local_path, MLMODEL_FILE_NAME))
   1142 if model_meta.metadata and model_meta.metadata.get(MLFLOW_MODEL_IS_EXTERNAL, False) is True:
   1143     raise MlflowException(
   1144         "This model's artifacts are external and are not stored in the model directory."
   1145         " This model cannot be loaded with MLflow.",
   1146         BAD_REQUEST,
   1147     )

File C:\Program Files\Python313\Lib\site-packages\mlflow\models\model.py:814, in Model.load(cls, path)
    812 is_model_dir = path.rsplit(sep, maxsplit=1)[-1] != MLMODEL_FILE_NAME
    813 mlmodel_file_path = f"{path}/{MLMODEL_FILE_NAME}" if is_model_dir else path
--> 814 mlmodel_local_path = _download_artifact_from_uri(artifact_uri=mlmodel_file_path)
    815 with open(mlmodel_local_path) as f:
    816     model_dict = yaml.safe_load(f)

File C:\Program Files\Python313\Lib\site-packages\mlflow\tracking\artifact_utils.py:117, in _download_artifact_from_uri(artifact_uri, output_path, lineage_header_info, tracking_uri, registry_uri)
    107 """
    108 Args:
    109     artifact_uri: The *absolute* URI of the artifact to download.
   (...)    114     registry_uri: The registry URI to be used when downloading artifacts.
    115 """
    116 root_uri, artifact_path = _get_root_uri_and_artifact_path(artifact_uri)
--> 117 repo = get_artifact_repository(
    118     artifact_uri=root_uri, tracking_uri=tracking_uri, registry_uri=registry_uri
    119 )
    121 try:
    122     if isinstance(repo, ModelsArtifactRepository):

File C:\Program Files\Python313\Lib\site-packages\mlflow\store\artifact\artifact_repository_registry.py:147, in get_artifact_repository(arti...

</details>

- Fixes mlflow/mlflow#18670

<!-- START COPILOT CODING AGENT TIPS -->
---

💬 We'd love your input! Share your thoughts on Copilot coding agent in our [2 minute survey](https://gh.io/copilot-coding-agent-survey).

Co-authored-by: harupy <17039389+harupy@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix documentation for loading model in MLFlow Fix undefined model_info variable in quickstart documentation Nov 5, 2025
Copilot AI requested a review from harupy November 5, 2025 00:03
@harupy
Copy link
Member

harupy commented Nov 5, 2025

@copilot Thanks for the update!

  • Search loaded_model = mlflow.pyfunc.load_model(model_info.model_uri) in .mdx files under the docs directory - If model_info is undefined, Update the log_model calls to model_info = mlflow.<flavor>.log_model(...).

Copy link
Contributor Author

Copilot AI commented Nov 5, 2025

@copilot Thanks for the update!

  • Search loaded_model = mlflow.pyfunc.load_model(model_info.model_uri) in .mdx files under the docs directory - If model_info is undefined, Update the log_model ...

I've verified all 8 .mdx files that use loaded_model = mlflow.pyfunc.load_model(model_info.model_uri):

✅ All files now have proper model_info = definitions before usage:

  • docs/classic-ml/tracking/quickstart/index.mdx - Fixed in commit 3d34ee1
  • docs/classic-ml/getting-started/quickstart.mdx - Fixed in commit 3d34ee1
  • docs/classic-ml/model/models-from-code/index.mdx - Already correct (3 definitions, 3 usages)
  • docs/classic-ml/model/signatures/index.mdx - Already correct (4 definitions, 5 usages)
  • docs/classic-ml/deep-learning/keras/guide/index.mdx - Already correct (3 definitions, 1 usage)
  • docs/classic-ml/deep-learning/spacy/guide/index.mdx - Already correct (3 definitions, 5 usages)
  • docs/classic-ml/deep-learning/pytorch/guide/index.mdx - Already correct (6 definitions, 1 usage)
  • docs/genai/flavors/chat-model-intro/index.mdx - Already correct (3 definitions, 3 usages)

Every usage of model_info.model_uri now has a prior model_info = mlflow.<flavor>.log_model(...) definition in the same code block.

@harupy harupy added the rn/none List under Small Changes in Changelogs. label Nov 5, 2025
@harupy harupy marked this pull request as ready for review November 5, 2025 00:14
@github-actions
Copy link
Contributor

github-actions bot commented Nov 5, 2025

@Copilot Thank you for the contribution! Could you fix the following issue(s)?

⚠ Invalid PR template

This PR does not appear to have been filed using the MLflow PR template. Please copy the PR template from here and fill it out.

@github-actions
Copy link
Contributor

github-actions bot commented Nov 5, 2025

Documentation preview for 3d34ee1 is available at:

Changed Pages (2)
More info
  • Ignore this comment if this PR does not change the documentation.
  • The preview is updated when a new commit is pushed to this PR.
  • This comment was created by this workflow run.
  • The documentation was built by this workflow run.

@harupy harupy enabled auto-merge November 5, 2025 00:29
@harupy harupy added this pull request to the merge queue Nov 5, 2025
Merged via the queue into master with commit 5032dae Nov 5, 2025
52 checks passed
@harupy harupy deleted the copilot/fix-model-loading-error branch November 5, 2025 01:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

rn/none List under Small Changes in Changelogs.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[DOC-FIX] Python code given in Step 6 - Load the model back for inference. fails

2 participants