{"record":{"id":"5ae3075c6946d0a0","repo":"mem0ai/mem0","slug":"langchain-embedder-provider-requires-an-initialize","errorCode":null,"errorMessage":"Langchain embedder provider requires an initialized Langchain Embeddings instance passed via the 'model' field in the embedder config.","messagePattern":"Langchain embedder provider requires an initialized Langchain Embeddings instance passed via the 'model' field in the embedder config\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"mem0-ts/src/oss/src/embeddings/langchain.ts","lineNumber":12,"sourceCode":"import type { Embeddings } from \"@langchain/core/embeddings\";\nimport { Embedder } from \"./base\";\nimport { EmbeddingConfig } from \"../types\";\n\nexport class LangchainEmbedder implements Embedder {\n  private embedderInstance: Embeddings;\n  private batchSize?: number; // Some LC embedders have batch size\n\n  constructor(config: EmbeddingConfig) {\n    // Check if config.model is provided and is an object (the instance)\n    if (!config.model || typeof config.model !== \"object\") {\n      throw new Error(\n        \"Langchain embedder provider requires an initialized Langchain Embeddings instance passed via the 'model' field in the embedder config.\",\n      );\n    }\n    // Basic check for embedding methods\n    if (\n      typeof (config.model as any).embedQuery !== \"function\" ||\n      typeof (config.model as any).embedDocuments !== \"function\"\n    ) {\n      throw new Error(\n        \"Provided Langchain 'instance' in the 'model' field does not appear to be a valid Langchain Embeddings instance (missing embedQuery or embedDocuments method).\",\n      );\n    }\n    this.embedderInstance = config.model as Embeddings;\n    // Store batch size if the instance has it (optional)\n    this.batchSize = (this.embedderInstance as any).batchSize;\n  }\n\n  async embed(text: string): Promise<number[]> {","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0-ts/src/oss/src/embeddings/langchain.ts#L1-L30","documentation":"Thrown by the LangchainEmbedder constructor when config.model is not an object. This provider is special: instead of a model name string, the 'model' field must be an already-initialized LangChain Embeddings instance (e.g. new OpenAIEmbeddings()), which the SDK delegates to via embedQuery/embedDocuments. A string, number, undefined, or array fails this check.","triggerScenarios":"embedder: { provider: 'langchain', config: { model: 'text-embedding-3-small' } } — passing a model name like other providers; passing the class itself (OpenAIEmbeddings) instead of an instance (new OpenAIEmbeddings()); forgetting the config entirely.","commonSituations":"Copying the OpenAI embedder config shape and just switching provider to 'langchain'; bundling for the browser where the instance serializes oddly; passing an instance created in a different package version with an incompatible shape.","solutions":["Pass an instantiated LangChain Embeddings object: config: { model: new OpenAIEmbeddings({ apiKey }) }","Make sure it is `new ...`, not the class reference","Confirm @langchain/core is installed at a compatible version and the instance is created before Memory construction"],"exampleFix":"// before\nembedder: { provider: 'langchain', config: { model: 'text-embedding-3-small' } }\n\n// after\nimport { OpenAIEmbeddings } from '@langchain/openai';\nembedder: { provider: 'langchain', config: { model: new OpenAIEmbeddings() } }","handlingStrategy":"type-guard","validationCode":"if (!embeddingsInstance || typeof embeddingsInstance !== 'object') {\n  throw new Error(\"Pass a LangChain Embeddings instance in config.model, e.g. new OpenAIEmbeddings()\");\n}","typeGuard":"import type { Embeddings } from '@langchain/core/embeddings';\nconst isLangchainEmbeddings = (m: unknown): m is Embeddings =>\n  !!m && typeof m === 'object' &&\n  typeof (m as Embeddings).embedQuery === 'function' &&\n  typeof (m as Embeddings).embedDocuments === 'function';","tryCatchPattern":null,"preventionTips":["The langchain provider takes an INSTANCE in model — never a string model name","Write `new OpenAIEmbeddings()`, not the class reference","Run isLangchainEmbeddings() on the value before passing it to catch mistakes at the call site"],"tags":["langchain","embeddings","validation","config","typescript"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}