{"record":{"id":"6e90ae12bebba8b3","repo":"mem0ai/mem0","slug":"langchain-core-is-required-to-pass-a-custom-llm-to","errorCode":null,"errorMessage":"langchain-core is required to pass a custom LLM to procedural memory. Install it with 'pip install langchain-core'.","messagePattern":"langchain-core is required to pass a custom LLM to procedural memory\\. Install it with 'pip install langchain-core'\\.","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"mem0/memory/main.py","lineNumber":3693,"sourceCode":"        logger.info(\"Creating procedural memory\")\n\n        parsed_messages = [\n            {\"role\": \"system\", \"content\": prompt or PROCEDURAL_MEMORY_SYSTEM_PROMPT},\n            *messages,\n            {\"role\": \"user\", \"content\": \"Create procedural memory of the above conversation.\"},\n        ]\n\n        try:\n            if llm is not None:\n                # langchain-core is only needed to adapt messages for a custom\n                # LangChain LLM. The default path uses self.llm and must not\n                # require the optional dependency, mirroring the sync version.\n                try:\n                    from langchain_core.messages.utils import (\n                        convert_to_messages,  # type: ignore\n                    )\n                except ImportError as e:\n                    raise ImportError(\n                        \"langchain-core is required to pass a custom LLM to procedural memory. \"\n                        \"Install it with 'pip install langchain-core'.\"\n                    ) from e\n\n                parsed_messages = convert_to_messages(parsed_messages)\n                response = await asyncio.to_thread(llm.invoke, input=parsed_messages)\n                procedural_memory = remove_code_blocks(response.content)\n            else:\n                procedural_memory = await asyncio.to_thread(self.llm.generate_response, messages=parsed_messages)\n                procedural_memory = remove_code_blocks(procedural_memory)\n        \n        except Exception as e:\n            logger.error(f\"Error generating procedural memory summary: {e}\")\n            raise\n\n        if metadata is None:\n            raise ValueError(\"Metadata cannot be done for procedural memory.\")\n","sourceCodeStart":3675,"sourceCodeEnd":3711,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0/memory/main.py#L3675-L3711","documentation":"Raised by AsyncMemory._create_procedural_memory when you pass a custom LLM (a LangChain LLM object) as the llm argument but langchain-core is not installed. The default path (llm=None) uses the internal self.llm and does not need langchain-core; only adapting your message list for a custom LangChain LLM requires it. The ImportError chains the original ImportError so the root cause is visible.","triggerScenarios":"Calling memory.create(...) or the procedural-memory flow with llm=<LangChain LLM instance> in an environment where 'pip install langchain-core' was never run; using an extra like mem0ai without the langchain extras; a venv that has langchain but not the split-out langchain-core package after a version reshuffle.","commonSituations":"Users wiring a custom LangChain chat model into Mem0's procedural memory summarization; CI environments installing only the minimal dependency set; upgrades from older LangChain versions where langchain-core did not exist as a separate package.","solutions":["pip install langchain-core in the same environment running mem0","Alternatively omit the llm argument and let Mem0 use its configured self.llm (no extra dependency needed)","Verify with python -c \"import langchain_core\" that the install landed in the active interpreter, not another venv"],"exampleFix":"# before\nawait memory._create_procedural_memory(messages, llm=my_langchain_llm)  # ImportError\n\n# after\npip install langchain-core\n# or drop the custom llm:\nawait memory._create_procedural_memory(messages)","handlingStrategy":"validation","validationCode":"import importlib.util\nif llm is not None and importlib.util.find_spec(\"langchain_core\") is None:\n    raise RuntimeError(\"langchain-core missing; pip install langchain-core or drop the custom llm\")","typeGuard":null,"tryCatchPattern":"try:\n    result = await memory._create_procedural_memory(messages, llm=my_lc_llm)\nexcept ImportError as e:\n    if \"langchain-core\" in str(e):\n        result = await memory._create_procedural_memory(messages)  # fall back to default LLM\n    else:\n        raise","preventionTips":["Install optional extras whenever you pass provider-specific objects into mem0","Pin optional deps in the same lockfile as mem0","Smoke-test custom-LLM paths in CI with the exact runtime image"],"tags":["dependency","import-error","langchain","procedural-memory","async"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}