{"record":{"id":"9c0670c2d5484f9a","repo":"mem0ai/mem0","slug":"provided-langchain-instance-in-the-model-field","errorCode":null,"errorMessage":"Provided Langchain 'instance' in the 'model' field does not appear to be a valid Langchain Embeddings instance (missing embedQuery or embedDocuments method).","messagePattern":"Provided Langchain 'instance' in the 'model' field does not appear to be a valid Langchain Embeddings instance \\(missing embedQuery or embedDocuments method\\)\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"mem0-ts/src/oss/src/embeddings/langchain.ts","lineNumber":21,"sourceCode":"import { 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[]> {\n    try {\n      // Use embedQuery for single text embedding\n      return await this.embedderInstance.embedQuery(text);\n    } catch (error) {\n      console.error(\"Error embedding text with Langchain Embedder:\", error);\n      throw error;\n    }\n  }\n","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0-ts/src/oss/src/embeddings/langchain.ts#L3-L39","documentation":"Thrown by the LangchainEmbedder constructor when config.model IS an object but lacks functioning embedQuery or embedDocuments methods — the duck-type check for a real LangChain Embeddings instance. This catches near-misses: a plain options object, a partially built instance, a mock, or an object from an incompatible @langchain/core major version whose method surface changed.","triggerScenarios":"Passing { model: { model: 'text-embedding-3-small' } } (config nested one level too deep); passing a custom wrapper that implements embed() but not embedQuery/embedDocuments; passing an object whose methods were stripped by structuredClone/serialization across worker boundaries.","commonSituations":"Wrapping an embedder in a custom class that does not extend LangChain's Embeddings base; version drift between @langchain/core in the host app and in dependencies; sending config through IPC/JSON so functions are lost.","solutions":["Extend @langchain/core/embeddings Embeddings (or use a built-in like OpenAIEmbeddings) so both methods exist as real functions","If wrapping, forward both methods: embedQuery = (t) => this.inner.embedQuery(t); embedDocuments = (d) => this.inner.embedDocuments(d);","Construct the instance in the same process that builds Memory — never serialize it through IPC/JSON"],"exampleFix":"// before\nclass MyEmbedder { async embed(text: string) { /* ... */ } }\nembedder: { provider: 'langchain', config: { model: new MyEmbedder() } }\n\n// after\nimport { Embeddings } from '@langchain/core/embeddings';\nclass MyEmbedder extends Embeddings {\n  async embedQuery(text: string) { /* ... */ return [0.1]; }\n  async embedDocuments(docs: string[]) { /* ... */ return docs.map(() => [0.1]); }\n}\nembedder: { provider: 'langchain', config: { model: new MyEmbedder() } }","handlingStrategy":"type-guard","validationCode":"if (typeof (instance as any).embedQuery !== 'function' || typeof (instance as any).embedDocuments !== 'function') {\n  throw new Error('Object is not a LangChain Embeddings instance: implement both embedQuery and embedDocuments');\n}","typeGuard":"const 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';\n\nif (!isLangchainEmbeddings(config.model)) throw new Error('invalid embedder instance');","tryCatchPattern":null,"preventionTips":["Custom wrappers must extend @langchain/core's Embeddings base or implement both methods","Construct the instance in-process; never send it through JSON/IPC where methods are stripped","Align @langchain/core versions across your app and dependencies to avoid a shifted method surface"],"tags":["langchain","embeddings","validation","type-guard","typescript"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}