I built a FAISS index and it worked, but now index.search(query, k) (or index.add(...)) fails with an AssertionError pointing at assert d == self.d.
Why this happens
A FAISS index is created for one fixed vector dimension d, for example faiss.IndexFlatL2(384). Every vector you add or search with must have exactly that many values. FAISS checks this with an assertion, which is why you get a bare AssertionError without a helpful message. In practice, the cause is almost always one of these:
- The documents were embedded with one model (e.g. 384 dimensions) and the query with another (e.g. 768 or 1536).
- The index was created with a hard-coded dimension that doesn't match your model.
- The query has an extra or missing axis, such as shape
(1, 1, 384)or(384, 1)instead of(1, 384).
Answer 1: Print the shapes and compare them with the index
Before changing anything, find out which side is wrong:
print("index dimension:", index.d)
print("vectors added: ", index.ntotal)
print("query shape: ", query.shape)
The query must have shape (n_queries, index.d). If the second number differs, you have a model mismatch (Answer 2). If the shape has the wrong number of axes, you have a shape problem (Answer 3).
