{"record":{"id":"3b0940c375f2ce11","repo":"mem0ai/mem0","slug":"vector-dimension-mismatch-at-index-i-expected","errorCode":null,"errorMessage":"Vector dimension mismatch at index ${i}. Expected ${this.dimension}, got ${v.length}","messagePattern":"Vector dimension mismatch at index (.+?)\\. Expected (.+?), got (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"mem0-ts/src/oss/src/vector_stores/langchain.ts","lineNumber":71,"sourceCode":"    }\n  }\n\n  // --- Method Mappings ---\n\n  async insert(\n    vectors: number[][],\n    ids: string[],\n    payloads: Record<string, any>[],\n  ): Promise<void> {\n    if (!ids || ids.length !== vectors.length) {\n      throw new Error(\n        \"IDs array must be provided and have the same length as vectors.\",\n      );\n    }\n    if (this.dimension) {\n      vectors.forEach((v, i) => {\n        if (v.length !== this.dimension) {\n          throw new Error(\n            `Vector dimension mismatch at index ${i}. Expected ${this.dimension}, got ${v.length}`,\n          );\n        }\n      });\n    }\n\n    // Convert payloads to Langchain Document metadata format\n    const { Document } = await import(\"@langchain/core/documents\");\n    const documents = payloads.map((payload, i) => {\n      // Provide empty pageContent, store mem0 id and other data in metadata\n      return new Document({\n        pageContent: \"\", // Add required empty pageContent\n        metadata: { ...payload, _mem0_id: ids[i] },\n      });\n    });\n\n    // Use addVectors. Note: Langchain stores often generate their own internal IDs.\n    // We store the mem0 ID in the metadata (`_mem0_id`).","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0-ts/src/oss/src/vector_stores/langchain.ts#L53-L89","documentation":"insert() in the Langchain adapter validates each vector against this.dimension (from config.dimension or inferred from the store's embeddings.embeddingDimension) and throws naming the offending index when a vector's length differs. Since the wrapper forwards vectors directly to the underlying store, a dimension mismatch would fail deeper inside Langchain with a worse message.","triggerScenarios":"Calling insert() with vectors from an embedding model whose dimension differs from config.dimension or from the embeddings object bound to the Langchain store.","commonSituations":"Configuring Memory with one embedder but passing a Langchain store bound to a different embeddings model; switching embedding models without recreating the underlying store; mixing cached/historical vectors with a new embedder.","solutions":["Use the same embedding model for Memory's embedder and the Langchain store's bound embeddings.","Set config.dimension explicitly to the actual model dimension and verify every insert path uses that model.","If you changed models, recreate/reindex the underlying store."],"exampleFix":"// before\nconst lcStore = new MemoryVectorStore(new OpenAIEmbeddings()); // 1536-dim\nnew Memory({ embedder: new OllamaEmbedder(), vectorStore: { provider: 'langchain', config: { client: lcStore } } });\n\n// after\nconst embeddings = new OpenAIEmbeddings();\nconst lcStore = new MemoryVectorStore(embeddings);\nnew Memory({ embedder: openAiEmbedder /* same model */, vectorStore: { provider: 'langchain', config: { client: lcStore } } });","handlingStrategy":"validation","validationCode":"const { embedding } = await embedder.embed('dimension probe');\nif (config.dimension && embedding.length !== config.dimension) {\n  throw new Error(`Embedder ${embedding.length}-d != configured ${config.dimension}-d`);\n}","typeGuard":null,"tryCatchPattern":"try {\n  await store.insert(vectors, ids, payloads);\n} catch (e) {\n  if (e instanceof Error && /dimension mismatch/i.test(e.message)) {\n    // log offending index from the message; align embedder/config, then retry\n  }\n  throw e;\n}","preventionTips":["Bind the same embeddings instance to both the Langchain store and Memory's embedder.","Set config.dimension from a probe embedding at startup.","Reindex when changing embedding models."],"tags":["langchain","dimension-mismatch","embeddings","vector-store","typescript"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}