mlflow/mlflow · error · NotImplementedError

`get_raw_model` is not implemented by the underlying model

Error message

`get_raw_model` is not implemented by the underlying model

What it means

`PythonModel.get_raw_model()` raises NotImplementedError when the wrapped model implementation does not define a `get_raw_model` method. MLflow only delegates to the wrapper if `hasattr(self._model_impl, 'get_raw_model')` is true; otherwise there is no raw model to expose and the error is thrown deliberately.

Source

Thrown at mlflow/pyfunc/__init__.py:1063

        info = {}
        if self._model_meta is not None:
            if hasattr(self._model_meta, "run_id") and self._model_meta.run_id is not None:
                info["run_id"] = self._model_meta.run_id
            if (
                hasattr(self._model_meta, "artifact_path")
                and self._model_meta.artifact_path is not None
            ):
                info["artifact_path"] = self._model_meta.artifact_path
            info["flavor"] = self._model_meta.flavors[FLAVOR_NAME]["loader_module"]
        return yaml.safe_dump({"mlflow.pyfunc.loaded_model": info}, default_flow_style=False)

    def get_raw_model(self):
        """
        Get the underlying raw model if the model wrapper implemented `get_raw_model` function.
        """
        if hasattr(self._model_impl, "get_raw_model"):
            return self._model_impl.get_raw_model()
        raise NotImplementedError("`get_raw_model` is not implemented by the underlying model")


def _get_pip_requirements_from_model_path(model_path: str):
    req_file_path = os.path.join(model_path, _REQUIREMENTS_FILE_NAME)
    if not os.path.exists(req_file_path):
        return []

    return [req.req_str for req in _parse_requirements(req_file_path, is_constraint=False)]


@trace_disabled  # Suppress traces while loading model
def load_model(
    model_uri: str,
    suppress_warnings: bool = False,
    dst_path: str | None = None,
    model_config: str | Path | dict[str, Any] | None = None,
) -> PyFuncModel:
    """

View on GitHub (pinned to 6a27f2decc)

Solutions

  1. Load the model and access the underlying object directly via `model._model_impl` or the flavor-specific attribute if you control the code
  2. Unwrap only models whose wrapper explicitly implements `get_raw_model` (check `hasattr` first)
  3. Re-log the model with a current MLflow version and a wrapper that implements `get_raw_model`
  4. If you just need predictions, call `model.predict(...)` instead of unwrapping the raw model

Example fix

// before
raw = mlflow.pyfunc.get_raw_model("runs:/abc/model")  # raises for custom wrapper
// after
m = mlflow.pyfunc.load_model("runs:/abc/model")
raw = m.get_raw_model() if hasattr(m._model_impl, "get_raw_model") else m._model_impl
Defensive patterns

Strategy: type-guard

Validate before calling

m = mlflow.pyfunc.load_model(model_uri)
if not hasattr(m._model_impl, "get_raw_model"):
    raise RuntimeError("model wrapper does not support get_raw_model")

Type guard

def has_raw_model(pyfunc_model) -> bool:
    return hasattr(pyfunc_model._model_impl, "get_raw_model")

Try / catch

try:
    raw = pyfunc_model.get_raw_model()
except NotImplementedError:
    raw = pyfunc_model._model_impl  # fallback unwrap

Prevention

When it happens

Trigger: Calling `mlflow.pyfunc.get_raw_model(model_uri)` (or the method on a loaded pyfunc) when the model was logged from a flavor/wrapper that never implemented `get_raw_model`, e.g. a plain custom PythonModel or an older logged model predating the API.

Common situations: Using get_raw_model on models logged with custom pyfunc wrappers, on models from older MLflow versions before the API existed, or on flavors that don't wrap a single underlying raw model (e.g. ensembles, pipelines).

Related errors


AI-assisted analysis of mlflow/mlflow@6a27f2decc (2026-08-29). Data as JSON: /api/errors/b76d5eac60e7af72. Report an issue: GitHub.