mem0ai/mem0 · critical · Error

Vector at index ${index} has dimension ${vector.length}, but

Error message

Vector at index ${index} has dimension ${vector.length}, but index '${this.collectionName}' expects dimension ${this.embeddingModelDims}.

What it means

The OpenSearch index was created with a dense_vector field of fixed dimension this.embeddingModelDims. Inserting a vector whose length differs would fail OpenSearch's mapping check with an opaque server error, so the store pre-validates every vector and reports the vector's length, the collection name, and the expected dimension.

Source

Thrown at mem0-ts/src/oss/src/vector_stores/opensearch.ts:221

          properties: {
            user_id: { type: "keyword" },
          },
        },
      },
    });
  }

  private validateVector(vector: number[], index: number): void {
    if (!vector) {
      throw new Error(`Vector at index ${index} is null or undefined.`);
    }
    if (vector.length === 0) {
      throw new Error(
        `Vector at index ${index} is empty. Expected dimension ${this.embeddingModelDims}.`,
      );
    }
    if (vector.length !== this.embeddingModelDims) {
      throw new Error(
        `Vector at index ${index} has dimension ${vector.length}, but index ` +
          `'${this.collectionName}' expects dimension ${this.embeddingModelDims}.`,
      );
    }
  }

  async insert(
    vectors: number[][],
    ids: string[],
    payloads: Record<string, any>[],
  ): Promise<void> {
    await this.initialize();
    vectors.forEach((vector, index) => this.validateVector(vector, index));

    const operations = vectors.flatMap((vector, index) => {
      const id = ids[index] || String(index);
      return [
        { index: { _index: this.collectionName, _id: id } },

View on GitHub (pinned to 001c235229)

Solutions

  1. Align the embedding model dimension with embeddingModelDims in the store config, then recreate the index.
  2. If the model changed, delete and recreate the OpenSearch index with the new dimension and re-index memories.
  3. Guarantee a single embedding model per index; route different dims to different collections.

Example fix

// before
// index created with embeddingModelDims: 1536
const vec = await embed('text', 'text-embedding-3-large'); // 3072
await store.insert([vec], ids, payloads);

// after
const store = new OpenSearch({ embeddingModelDims: 3072 }); // recreate index
await store.insert([vec], ids, payloads);
Defensive patterns

Strategy: validation

Validate before calling

const expected = storeConfig.embeddingModelDims;
if (vectors.some((v) => v.length !== expected)) {
  throw new Error(`Vector dims do not match configured ${expected}`);
}

Type guard

const matchesDims = (v: number[], dims: number): boolean => Array.isArray(v) && v.length === dims;

Try / catch

catch (e) { if (e.message.includes('expects dimension')) { /* rebuild index with new dims and re-embed */ } }

Prevention

When it happens

Trigger: Embedding with a model of a different dimension than configured (e.g. 3072-dim text-embedding-3-large vectors into an index built for 1536); mixing embedding providers between write and read; manually constructed vectors of wrong size.

Common situations: Changing the embedding model without recreating the OpenSearch index; per-tenant models with different dims sharing one index; local dev using a small test model against a prod-configured index.

Related errors


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