mem0ai/mem0 · error · Error

Vector dimension mismatch at index ${i}. Expected ${this.dim

Error message

Vector dimension mismatch at index ${i}. Expected ${this.dimension}, got ${v.length}

What it means

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.

Source

Thrown at mem0-ts/src/oss/src/vector_stores/langchain.ts:71

    }
  }

  // --- Method Mappings ---

  async insert(
    vectors: number[][],
    ids: string[],
    payloads: Record<string, any>[],
  ): Promise<void> {
    if (!ids || ids.length !== vectors.length) {
      throw new Error(
        "IDs array must be provided and have the same length as vectors.",
      );
    }
    if (this.dimension) {
      vectors.forEach((v, i) => {
        if (v.length !== this.dimension) {
          throw new Error(
            `Vector dimension mismatch at index ${i}. Expected ${this.dimension}, got ${v.length}`,
          );
        }
      });
    }

    // Convert payloads to Langchain Document metadata format
    const { Document } = await import("@langchain/core/documents");
    const documents = payloads.map((payload, i) => {
      // Provide empty pageContent, store mem0 id and other data in metadata
      return new Document({
        pageContent: "", // Add required empty pageContent
        metadata: { ...payload, _mem0_id: ids[i] },
      });
    });

    // Use addVectors. Note: Langchain stores often generate their own internal IDs.
    // We store the mem0 ID in the metadata (`_mem0_id`).

View on GitHub (pinned to 001c235229)

Solutions

  1. Use the same embedding model for Memory's embedder and the Langchain store's bound embeddings.
  2. Set config.dimension explicitly to the actual model dimension and verify every insert path uses that model.
  3. If you changed models, recreate/reindex the underlying store.

Example fix

// before
const lcStore = new MemoryVectorStore(new OpenAIEmbeddings()); // 1536-dim
new Memory({ embedder: new OllamaEmbedder(), vectorStore: { provider: 'langchain', config: { client: lcStore } } });

// after
const embeddings = new OpenAIEmbeddings();
const lcStore = new MemoryVectorStore(embeddings);
new Memory({ embedder: openAiEmbedder /* same model */, vectorStore: { provider: 'langchain', config: { client: lcStore } } });
Defensive patterns

Strategy: validation

Validate before calling

const { embedding } = await embedder.embed('dimension probe');
if (config.dimension && embedding.length !== config.dimension) {
  throw new Error(`Embedder ${embedding.length}-d != configured ${config.dimension}-d`);
}

Try / catch

try {
  await store.insert(vectors, ids, payloads);
} catch (e) {
  if (e instanceof Error && /dimension mismatch/i.test(e.message)) {
    // log offending index from the message; align embedder/config, then retry
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling insert() with vectors from an embedding model whose dimension differs from config.dimension or from the embeddings object bound to the Langchain store.

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

Related errors


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