{"record":{"id":"d7759ebc0f50e78c","repo":"mem0ai/mem0","slug":"model-must-be-an-instance-of-embeddings","errorCode":null,"errorMessage":"`model` must be an instance of Embeddings","messagePattern":"`model` must be an instance of Embeddings","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"mem0/embeddings/langchain.py","lineNumber":20,"sourceCode":"\nfrom mem0.configs.embeddings.base import BaseEmbedderConfig\nfrom mem0.embeddings.base import EmbeddingBase\n\ntry:\n    from langchain.embeddings.base import Embeddings\nexcept ImportError:\n    raise ImportError(\"langchain is not installed. Please install it using `pip install langchain`\")\n\n\nclass LangchainEmbedding(EmbeddingBase):\n    def __init__(self, config: Optional[BaseEmbedderConfig] = None):\n        super().__init__(config)\n\n        if self.config.model is None:\n            raise ValueError(\"`model` parameter is required\")\n\n        if not isinstance(self.config.model, Embeddings):\n            raise ValueError(\"`model` must be an instance of Embeddings\")\n\n        self.langchain_model = self.config.model\n\n    def embed(self, text, memory_action: Optional[Literal[\"add\", \"search\", \"update\"]] = None):\n        \"\"\"\n        Get the embedding for the given text using Langchain.\n\n        Args:\n            text (str): The text to embed.\n            memory_action (optional): The type of embedding to use. Must be one of \"add\", \"search\", or \"update\". Defaults to None.\n        Returns:\n            list: The embedding vector.\n        \"\"\"\n\n        return self.langchain_model.embed_query(text)\n","sourceCodeStart":2,"sourceCodeEnd":36,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0/embeddings/langchain.py#L2-L36","documentation":"Raised by LangchainEmbedding.__init__ when config.model is set but is not an instance of Langchain's Embeddings base class. The Langchain provider does not call an API itself; it wraps an already-configured Langchain embeddings object, so a string like 'text-embedding-3-small' is rejected.","triggerScenarios":"Passing model=\"text-embedding-3-small\" (a string) to BaseEmbedderConfig when provider is langchain; passing an LLM object or a LangChain vectorstore instead of an Embeddings implementation.","commonSituations":"Users porting an OpenAI embedder config verbatim to langchain; passing SentenceTransformerEmbeddings from an incompatible langchain major version whose base class moved (langchain vs langchain_core vs langchain_community split), making isinstance fail even for real embeddings classes.","solutions":["Pass an instance of a class deriving from langchain.embeddings.base.Embeddings (e.g. OpenAIEmbeddings(), HuggingFaceEmbeddings())","If the isinstance check fails despite a real embeddings object, align langchain package versions so the Embeddings base class is the one mem0 imported (pip install -U langchain langchain-community)","If you only have a model name, use the native provider (openai, huggingface, ollama) instead of langchain"],"exampleFix":"// before\nembedder = LangchainEmbedding(BaseEmbedderConfig(model=\"nomic-embed-text\"))  # ValueError\n\n# after\nfrom langchain_community.embeddings import OllamaEmbeddings\nembedder = LangchainEmbedding(BaseEmbedderConfig(model=OllamaEmbeddings(model=\"nomic-embed-text\")))","handlingStrategy":"type-guard","validationCode":"from langchain.embeddings.base import Embeddings\n\nmodel = config.get(\"model\")\nassert isinstance(model, Embeddings), (\n    \"langchain embedder model must be an Embeddings instance, \"\n    f\"got {type(model).__name__}\")","typeGuard":"from langchain.embeddings.base import Embeddings\n\ndef is_embeddings_instance(obj) -> bool:\n    \"\"\"True when obj can back LangchainEmbedding.\"\"\"\n    return isinstance(obj, Embeddings) and callable(getattr(obj, \"embed_documents\", None))","tryCatchPattern":"try:\n    embedder = LangchainEmbedding(config)\nexcept ValueError as e:\n    if \"must be an instance of Embeddings\" in str(e):\n        # user passed a string; convert intent into a real Embeddings object\n        raise TypeError(\"Pass e.g. OpenAIEmbeddings(model=name), not the model name\") from e\n    raise","preventionTips":["Never pass a model-name string to the langchain provider","Pin langchain package versions so the Embeddings base class does not move between packages","Unit-test embedder construction with the exact object you will ship"],"tags":["python","langchain","embeddings","type-mismatch","mem0"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}