preload pyfunc v2 model to avoid pydantic schema generation error #535
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Due to a bug that was resolved in https://github.com/mlflow/mlflow/pull/8722/files , the current version of Mlflow used by Merlin can potentially introduce some side effect by removing package from sys.modules.
One such side effect occurs when the user's Mlflow Pyfunc code happens to contain a subdirectory named
typing
. When appending the Pyfunc code to sys.path, Mlflow also unintentionally remove thetyping
standard module due to name clash, which clear some module level cache.We have recently migrated from swagger to OpenAPI for code generation, and as a result, the Merlin Pyfunc model (in merlin.model) is now depending on Pydantic model. The removal of
typing
standard module from sys.modules affect pydantic ability to resolve some field types correctly, which results in schema generation error.Ideally, we should upgrade Mlflow version to resolve this. But the fix is only introduce is version >2.0, which makes migration difficult. Hence, the alternative is to import Merlin pyfunc model prior to calling pyfunc.load_model. Importing the Pyfunc model allows pydantic to cache the schema for the class, which allows Pyfunc model to be instantiated correctly even though the
typing
modules is removed from sys.modules.Modifications
Import Pyfunc models prior to calling pyfunc.load_model.
Tests
Checklist
Release Notes