mem0ai/mem0 · error · ImportError

langchain-core is required to pass a custom LLM to procedura

Error message

langchain-core is required to pass a custom LLM to procedural memory. Install it with 'pip install langchain-core'.

What it means

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.

Source

Thrown at mem0/memory/main.py:3693

        logger.info("Creating procedural memory")

        parsed_messages = [
            {"role": "system", "content": prompt or PROCEDURAL_MEMORY_SYSTEM_PROMPT},
            *messages,
            {"role": "user", "content": "Create procedural memory of the above conversation."},
        ]

        try:
            if llm is not None:
                # langchain-core is only needed to adapt messages for a custom
                # LangChain LLM. The default path uses self.llm and must not
                # require the optional dependency, mirroring the sync version.
                try:
                    from langchain_core.messages.utils import (
                        convert_to_messages,  # type: ignore
                    )
                except ImportError as e:
                    raise ImportError(
                        "langchain-core is required to pass a custom LLM to procedural memory. "
                        "Install it with 'pip install langchain-core'."
                    ) from e

                parsed_messages = convert_to_messages(parsed_messages)
                response = await asyncio.to_thread(llm.invoke, input=parsed_messages)
                procedural_memory = remove_code_blocks(response.content)
            else:
                procedural_memory = await asyncio.to_thread(self.llm.generate_response, messages=parsed_messages)
                procedural_memory = remove_code_blocks(procedural_memory)
        
        except Exception as e:
            logger.error(f"Error generating procedural memory summary: {e}")
            raise

        if metadata is None:
            raise ValueError("Metadata cannot be done for procedural memory.")

View on GitHub (pinned to 001c235229)

Solutions

  1. pip install langchain-core in the same environment running mem0
  2. Alternatively omit the llm argument and let Mem0 use its configured self.llm (no extra dependency needed)
  3. Verify with python -c "import langchain_core" that the install landed in the active interpreter, not another venv

Example fix

# before
await memory._create_procedural_memory(messages, llm=my_langchain_llm)  # ImportError

# after
pip install langchain-core
# or drop the custom llm:
await memory._create_procedural_memory(messages)
Defensive patterns

Strategy: validation

Validate before calling

import importlib.util
if llm is not None and importlib.util.find_spec("langchain_core") is None:
    raise RuntimeError("langchain-core missing; pip install langchain-core or drop the custom llm")

Try / catch

try:
    result = await memory._create_procedural_memory(messages, llm=my_lc_llm)
except ImportError as e:
    if "langchain-core" in str(e):
        result = await memory._create_procedural_memory(messages)  # fall back to default LLM
    else:
        raise

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of mem0ai/mem0@001c235229 (2026-08-15). Data as JSON: /api/errors/6e90ae12bebba8b3. Report an issue: GitHub.