{"record":{"id":"ef0aa14b02a251af","repo":"mlflow/mlflow","slug":"your-model-contains-a-class-imported-from-the-lang","errorCode":null,"errorMessage":"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","messagePattern":"Your model contains a class imported from the LangChain partner package `langchain-(.+?)`\\. 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","errorType":"console","errorClass":"UserWarning","httpStatus":null,"severity":"warning","filePath":"mlflow/langchain/runnables.py","lineNumber":306,"sourceCode":"\n_LC_PARTNER_MODULE_PATTERN = re.compile(\n    r\"langchain_(?!core|community|experimental|cli|text-splitters)([a-z0-9-]+)$\"\n)\n\n\ndef _warning_if_imported_from_lc_partner_pkg(runnable):\n    \"\"\"\n    Issues a warning if the model contains LangChain partner packages in its requirements.\n\n    Popular integrations like OpenAI have been migrated from the central langchain-community\n    package to their own partner packages (e.g. langchain-openai). However, the class loading\n    mechanism in MLflow does not handle partner packages and always loads the community version.\n    This can lead to unexpected behavior because the community version is no longer maintained.\n    \"\"\"\n    module = runnable.__module__\n    root_module = module.split(\".\")[0]\n    if m := _LC_PARTNER_MODULE_PATTERN.match(root_module):\n        warnings.warn(\n            \"Your model contains a class imported from the LangChain partner package \"\n            f\"`langchain-{m.group(1)}`. When loading the model back, MLflow will use the \"\n            \"community version of the classes instead of the partner packages, which may \"\n            \"lead to unexpected behavior. To ensure that the model is loaded correctly, \"\n            \"it is recommended to save the model with the 'model-from-code' method \"\n            \"instead: https://mlflow.org/docs/latest/models.html#models-from-code\"\n        )\n\n\ndef _save_runnable_with_steps(model, file_path: Path | str, loader_fn=None, persist_dir=None):\n    \"\"\"Save the model with steps. Currently it supports saving RunnableSequence and\n    RunnableParallel.\n\n    If saving a RunnableSequence, steps is a list of Runnable objects. We save each step to the\n    subfolder named by the step index.\n    e.g.  - model\n            - steps\n              - 0","sourceCodeStart":288,"sourceCodeEnd":324,"githubUrl":"https://github.com/mlflow/mlflow/blob/6a27f2decc0b76eb1b54af31849784addb357dbc/mlflow/langchain/runnables.py#L288-L324","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["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","Refactor the chain so partner-package classes are constructed at load time (pass a loader_fn / model_from_code entry point)","If pickle saving must continue, pin matching langchain/langchain_community versions and test load behavior equivalence"],"exampleFix":"// before\nfrom langchain_openai import ChatOpenAI\nmlflow.langchain.log_model(ChatOpenAI(...), name=\"model\")  # pickle; reloads as community class\n// after\n# model.py defines the chain using langchain_openai\nmlflow.langchain.log_model(\"model.py\", name=\"model\")  # models-from-code; partner classes preserved","handlingStrategy":"fallback","validationCode":"import re\n_LC_PARTNER = re.compile(r\"langchain_(\\w+)\")\ndef uses_partner_packages(runnables) -> bool:\n    return any(_LC_PARTNER.match(getattr(r, \"__module__\", \"\").split(\".\")[0]) for r in runnables)\n# if True: save via models-from-code instead of pickle","typeGuard":"def is_partner_pkg_obj(obj) -> bool:\n    import re\n    return bool(re.match(r\"langchain_\\w+\", type(obj).__module__.split(\".\")[0]))","tryCatchPattern":"import warnings\nwith warnings.catch_warnings(record=True) as w:\n    warnings.simplefilter(\"always\")\n    mlflow.langchain.log_model(chain, name=\"model\")\n    if any(\"partner package\" in str(x.message) for x in w):\n        # fall back to models-from-code serialization\n        mlflow.langchain.log_model(\"model.py\", name=\"model\")","preventionTips":["Default to models-from-code for LangChain models that use partner packages","Keep langchain, langchain_community, and partner packages on compatible versions","Test save/load round trips and compare outputs before deploying","Avoid persisting partner-package class instances in pickle when behavior fidelity matters"],"tags":["langchain","serialization","pickle","partner-packages","model-from-code"],"backgroundTag":"pickle-serialization-mismatch","analyzedSha":"6a27f2decc0b76eb1b54af31849784addb357dbc","analyzedAt":"2026-08-29T20:54:51.419Z","schemaVersion":2},"datasetVersion":"2026-08-29T22:17:34.462Z"}