mem0ai/mem0 · error · Error

Vector dimension mismatch. Expected ${this.dimension}, got $

Error message

Vector dimension mismatch. Expected ${this.dimension}, got ${vector.length}

What it means

update() rewrites a row's vector blob and payload, and first checks that the new vector's length equals the store's fixed dimension. Since every stored vector must remain comparable, an update with a differently-sized vector (typically from a changed embedding model) is rejected before the UPDATE statement runs.

Source

Thrown at mem0-ts/src/oss/src/vector_stores/memory.ts:412

    const row = this.db
      .prepare(`SELECT * FROM vectors WHERE id = ?`)
      .get(vectorId) as any;
    if (!row) return null;

    const payload = this.normalizePayload(JSON.parse(row.payload));
    return {
      id: row.id,
      payload,
    };
  }

  async update(
    vectorId: string,
    vector: number[],
    payload: Record<string, any>,
  ): Promise<void> {
    if (vector.length !== this.dimension) {
      throw new Error(
        `Vector dimension mismatch. Expected ${this.dimension}, got ${vector.length}`,
      );
    }
    const vectorBuffer = Buffer.from(new Float32Array(vector).buffer);
    this.db
      .prepare(`UPDATE vectors SET vector = ?, payload = ? WHERE id = ?`)
      .run(vectorBuffer, JSON.stringify(payload), vectorId);
  }

  async delete(vectorId: string): Promise<void> {
    this.db.prepare(`DELETE FROM vectors WHERE id = ?`).run(vectorId);
  }

  async deleteCol(): Promise<void> {
    this.db.exec(`DROP TABLE IF EXISTS vectors`);
    this.init();
  }

View on GitHub (pinned to 001c235229)

Solutions

  1. Embed the updated text with the same model used at collection creation, then call update().
  2. If migrating embedders, delete and re-add the memory (fresh vector + new id) instead of update().
  3. Recreate the store with the new dimension if all data will be re-embedded.

Example fix

// before
await store.update(memoryId, wrongDimVector, payload); // throws

// after
const vec = await sameModelAsInsert.embed newText;
await store.update(memoryId, vec, payload);
Defensive patterns

Strategy: validation

Validate before calling

if (vector.length !== store.dimension) {
  throw new Error(`update(): vector is ${vector.length}-dim, store expects ${store.dimension}`);
}

Type guard

const isValidUpdateVector = (v: unknown, dim: number): v is number[] => Array.isArray(v) && v.length === dim && v.every((n) => typeof n === 'number');

Try / catch

try { await store.update(id, vector, payload); }
catch (e) {
  if (e instanceof Error && e.message.startsWith('Vector dimension mismatch')) {
    // re-embed the new text with the original model, or delete+re-add the memory
  } else throw e;
}

Prevention

When it happens

Trigger: Calling update(vectorId, newVector, payload) where newVector comes from a different embedding model than the collection's dimension; memory-update flows (memory.update()) after the embedder config changed; passing a truncated or padded vector.

Common situations: Migrating embedding providers mid-life of a SQLite memory DB and updating old memories with new-model vectors; tests with synthetic vectors of arbitrary length.

Related errors


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