RuntimeError
faiss.RuntimeError
Stack trace
RuntimeError: Error in faiss::Index::add: dimension mismatch: vector dimension is 1536 but index dimension is 768
Why it happens
FAISS indexes require that all vectors added have the same fixed dimension as the index was initialized with. This error occurs when the embedding vectors you generate have a different dimension than the FAISS index expects, often due to using different embedding models or reinitializing the index incorrectly.
Detection
Check the dimension of your embeddings before adding them to the FAISS index and verify it matches the index's dimension property to catch mismatches early.
Causes & fixes
Embedding model changed or upgraded, producing vectors of a different dimension than the existing FAISS index.
Rebuild the FAISS index from scratch using embeddings generated by the current embedding model to ensure dimension consistency.
Reusing an old FAISS index file created with a different embedding dimension without reinitializing or recreating it.
Delete or archive the old FAISS index and create a new index with the correct dimension matching your current embeddings.
Mixing embeddings from different models or sources with varying dimensions in the same FAISS index.
Ensure all embeddings added to a single FAISS index come from the same model and have identical dimensions.
Code: broken vs fixed
from langchain.vectorstores import FAISS
from langchain.embeddings.openai import OpenAIEmbeddings
embeddings = OpenAIEmbeddings()
index = FAISS.load_local('faiss_index', embeddings)
# This will raise dimension mismatch if embeddings dimension changed
index.add_texts(['New document text']) # RuntimeError here import os
from langchain.vectorstores import FAISS
from langchain.embeddings.openai import OpenAIEmbeddings
os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY') # Ensure API key is set
embeddings = OpenAIEmbeddings()
# Rebuild index with current embeddings to fix dimension mismatch
texts = ['Existing document text', 'New document text']
index = FAISS.from_texts(texts, embeddings)
print('FAISS index rebuilt successfully with correct dimensions') Workaround
Catch the RuntimeError, then delete the existing FAISS index files and recreate the index with embeddings from the current model before retrying the add operation.
Prevention
Always create a new FAISS index when switching embedding models or versions, and enforce embedding dimension checks before adding vectors to the index to avoid silent mismatches.