mlflow/mlflow · warning · UserWarning

Your model contains a class imported from the LangChain part

Error message

Your model contains a class imported from the LangChain partner package `langchain-{m.group(1)}`. When loading the model back, MLflow will use the community version of the classes instead of the partner packages, which may lead to unexpected behavior. To ensure that the model is loaded correctly, it is recommended to save the model with the 'model-from-code' method instead: https://mlflow.org/docs/latest/models.html#models-from-code

What it means

A warning raised by _warning_if_imported_from_lc_partner_pkg during saving of LangChain runnables. If the runnable's class comes from a LangChain partner package (langchain-<partner>, matched by _LC_PARTNER_MODULE_PATTERN), the pickle-based save format will reload it from the community `langchain_community` package instead, because MLflow's deserialization mechanism does not handle partner packages. Behavior may differ on load since community classes are no longer maintained.

Source

Thrown at mlflow/langchain/runnables.py:306

_LC_PARTNER_MODULE_PATTERN = re.compile(
    r"langchain_(?!core|community|experimental|cli|text-splitters)([a-z0-9-]+)$"
)


def _warning_if_imported_from_lc_partner_pkg(runnable):
    """
    Issues a warning if the model contains LangChain partner packages in its requirements.

    Popular integrations like OpenAI have been migrated from the central langchain-community
    package to their own partner packages (e.g. langchain-openai). However, the class loading
    mechanism in MLflow does not handle partner packages and always loads the community version.
    This can lead to unexpected behavior because the community version is no longer maintained.
    """
    module = runnable.__module__
    root_module = module.split(".")[0]
    if m := _LC_PARTNER_MODULE_PATTERN.match(root_module):
        warnings.warn(
            "Your model contains a class imported from the LangChain partner package "
            f"`langchain-{m.group(1)}`. When loading the model back, MLflow will use the "
            "community version of the classes instead of the partner packages, which may "
            "lead to unexpected behavior. To ensure that the model is loaded correctly, "
            "it is recommended to save the model with the 'model-from-code' method "
            "instead: https://mlflow.org/docs/latest/models.html#models-from-code"
        )


def _save_runnable_with_steps(model, file_path: Path | str, loader_fn=None, persist_dir=None):
    """Save the model with steps. Currently it supports saving RunnableSequence and
    RunnableParallel.

    If saving a RunnableSequence, steps is a list of Runnable objects. We save each step to the
    subfolder named by the step index.
    e.g.  - model
            - steps
              - 0

View on GitHub (pinned to 6a27f2decc)

Solutions

  1. Save the model with the models-from-code method (script-based serialization) instead of pickle: https://mlflow.org/docs/latest/models.html#models-from-code
  2. Refactor the chain so partner-package classes are constructed at load time (pass a loader_fn / model_from_code entry point)
  3. If pickle saving must continue, pin matching langchain/langchain_community versions and test load behavior equivalence

Example fix

// before
from langchain_openai import ChatOpenAI
mlflow.langchain.log_model(ChatOpenAI(...), name="model")  # pickle; reloads as community class
// after
# model.py defines the chain using langchain_openai
mlflow.langchain.log_model("model.py", name="model")  # models-from-code; partner classes preserved
Defensive patterns

Strategy: fallback

Validate before calling

import re
_LC_PARTNER = re.compile(r"langchain_(\w+)")
def uses_partner_packages(runnables) -> bool:
    return any(_LC_PARTNER.match(getattr(r, "__module__", "").split(".")[0]) for r in runnables)
# if True: save via models-from-code instead of pickle

Type guard

def is_partner_pkg_obj(obj) -> bool:
    import re
    return bool(re.match(r"langchain_\w+", type(obj).__module__.split(".")[0]))

Try / catch

import warnings
with warnings.catch_warnings(record=True) as w:
    warnings.simplefilter("always")
    mlflow.langchain.log_model(chain, name="model")
    if any("partner package" in str(x.message) for x in w):
        # fall back to models-from-code serialization
        mlflow.langchain.log_model("model.py", name="model")

Prevention

When it happens

Trigger: Calling mlflow.langchain.save_model/log_model (save via _save_internal_runnables) on a chain/runnable whose module root matches `langchain_<partner>` — e.g., an object instantiated from langchain_openai, langchain_anthropic, or langchain_google_genai classes.

Common situations: Pipelines built directly with modern partner-package classes (e.g., ChatOpenAI from langchain_openai) saved with the default pickle serialization; upgrading langchain where partner classes diverged from community equivalents, causing load-time behavioral differences.

Related errors


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