mem0ai/mem0 · error · Error

Query vector dimension mismatch. Expected ${this.dimension},

Error message

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

What it means

search() in the Langchain adapter throws when the query vector's length differs from this.dimension. The query is passed straight to similaritySearchVectorWithScore, so a mismatched dimension would otherwise produce wrong results or an obscure store-specific error.

Source

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

      console.warn(
        "Langchain store might not support custom IDs on insert. Trying without IDs.",
        e,
      );
      await this.lcStore.addVectors(vectors, documents);
    }
  }

  async keywordSearch(): Promise<null> {
    return null;
  }

  async search(
    query: number[],
    topK: number = 5,
    filters?: SearchFilters, // filters parameter is received but will be ignored
  ): Promise<VectorStoreResult[]> {
    if (this.dimension && query.length !== this.dimension) {
      throw new Error(
        `Query vector dimension mismatch. Expected ${this.dimension}, got ${query.length}`,
      );
    }

    // --- Remove filter processing logic ---
    // Filters passed via mem0 interface are not reliably translatable to generic Langchain stores.
    // let lcFilter: any = undefined;
    // if (filters && ...) { ... }
    // console.warn("LangchainVectorStore: Passing filters directly..."); // Remove warning

    // Call similaritySearchVectorWithScore WITHOUT the filter argument
    const results = await this.lcStore.similaritySearchVectorWithScore(
      query,
      topK,
      // Do not pass lcFilter here
    );

    // Map Langchain results [Document, score] back to mem0 VectorStoreResult

View on GitHub (pinned to 001c235229)

Solutions

  1. Re-embed queries with the same model used for inserts (use Memory.search rather than calling store.search with hand-made embeddings).
  2. Update config.dimension to match the actual embedder output dimension.
  3. If models were switched, reindex stored memories with the new embedder before searching.

Example fix

// before
const results = await store.search(staleQueryVector1536, 5); // dimension now 768

// after
const { embedding } = await memory.embedder.embed('query text');
const results = await store.search(embedding, 5);
Defensive patterns

Strategy: validation

Validate before calling

if (storeDimension && query.length !== storeDimension) {
  throw new Error(`Query is ${query.length}-d but store expects ${storeDimension}-d; re-embed the query`);
}

Try / catch

try {
  await store.search(query, topK);
} catch (e) {
  if (e instanceof Error && e.message.includes('Query vector dimension mismatch')) {
    // re-embed the query with the store's embedding model and retry
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling search() with a query embedded by a different model than the one whose dimension was configured/inferred — e.g. Memory's embedder changed after construction, or a manual search() call with a stale cached embedding.

Common situations: Swapping the embedding provider between add and search calls; using a persisted dimension from config while the live embedder differs; embedding the query with a different model version that changed dimensions (e.g. ada-002 1536 vs small model 1536 but MRL-truncated 256).

Related errors


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