mem0ai/mem0 · error · Error

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

Error message

Vector dimension mismatch. Expected ${this.dimension}, got ${vecs[i].length}

What it means

Before writing rows into the SQLite 'vectors' table, insert() verifies every vector's length against the dimension the store was created with (from the embedding config at init time). A mismatch means the embedding provider produced vectors of a different size than the schema was initialized for; storing them would corrupt similarity search over the Float32 blobs.

Source

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

      }
    }

    return true;
  }

  async insert(
    vectors: number[][],
    ids: string[],
    payloads: Record<string, any>[],
  ): Promise<void> {
    const stmt = this.db.prepare(
      `INSERT OR REPLACE INTO vectors (id, vector, payload) VALUES (?, ?, ?)`,
    );
    const insertMany = this.db.transaction(
      (vecs: number[][], vIds: string[], vPayloads: Record<string, any>[]) => {
        for (let i = 0; i < vecs.length; i++) {
          if (vecs[i].length !== this.dimension) {
            throw new Error(
              `Vector dimension mismatch. Expected ${this.dimension}, got ${vecs[i].length}`,
            );
          }
          const vectorBuffer = Buffer.from(new Float32Array(vecs[i]).buffer);
          stmt.run(vIds[i], vectorBuffer, JSON.stringify(vPayloads[i]));
        }
      },
    );
    insertMany(vectors, ids, payloads);
  }

  private tokenize(text: string): string[] {
    return text.toLowerCase().split(/\s+/).filter(Boolean);
  }

  async keywordSearch(
    query: string,
    topK: number = 10,

View on GitHub (pinned to 001c235229)

Solutions

  1. Align the embedding provider config with the vectors being inserted (same model everywhere).
  2. If you changed embedders intentionally, delete the SQLite DB file (or call reset/createCol) so the store re-initializes with the new dimension.
  3. If dimension must differ, pass an explicit dimension in the store config that matches your embedder output.
  4. Log vectors[0].length before insert when wiring up a new embedder to catch this early.

Example fix

// before
const store = new Memory({ vectorStore: { provider: 'memory', config: { path: 'mem.db', collectionName: 'mem', embeddingModel: { name: 'openai/text-embedding-3-small', ... } } } });
await memory.add('hi'); // embedder now returns 768-dim -> throws on insert

// after: recreate DB with the new embedder dimension
await fs.rm('mem.db');
const memory = new Memory({ embedder: { provider: 'ollama', config: { model: 'nomic-embed-text' } }, vectorStore: { provider: 'memory', config: { path: 'mem.db', collectionName: 'mem', dimension: 768 } } });
Defensive patterns

Strategy: validation

Validate before calling

const dim = (await embedder.embed('probe')).length;
if (vectors.some((v) => v.length !== dim)) {
  throw new Error(`Embedder outputs ${dim}, got vectors of length ${new Set(vectors.map(v => v.length))}`);
}

Type guard

const isDimension = (v: number[], dim: number): boolean => v.length === dim;

Try / catch

try { await store.insert(vectors, ids, payloads); }
catch (e) {
  if (e instanceof Error && e.message.startsWith('Vector dimension mismatch')) {
    // embedder changed: recreate store/DB with the new dimension, re-embed, retry
  } else throw e;
}

Prevention

When it happens

Trigger: Creating the store with one embedder dimension (e.g. openai text-embedding-3-small = 1536) then calling add()/insert() with vectors from a different model (e.g. 384-dim all-MiniLM); switching embedding providers between runs against the same SQLite file; passing hand-built vectors of the wrong length.

Common situations: Changing the embedding config without deleting/recreating the SQLite database file; mixing local (Ollama nomic-embed-text, 768) and hosted models across environments; custom embedders with a different output size.

Related errors


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