High severity intermediate · Fix: 5-10 min

RuntimeError

faiss.RuntimeError

What this error means
FAISS throws a dimension mismatch error when the vector dimension of your embeddings does not match the dimension of the FAISS index.

Stack trace

traceback
RuntimeError: Error in faiss::Index::add: dimension mismatch: vector dimension is 1536 but index dimension is 768
QUICK FIX
Delete the existing FAISS index and recreate it using embeddings from your current embedding model to align dimensions.

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

1

Embedding model changed or upgraded, producing vectors of a different dimension than the existing FAISS index.

✓ Fix

Rebuild the FAISS index from scratch using embeddings generated by the current embedding model to ensure dimension consistency.

2

Reusing an old FAISS index file created with a different embedding dimension without reinitializing or recreating it.

✓ Fix

Delete or archive the old FAISS index and create a new index with the correct dimension matching your current embeddings.

3

Mixing embeddings from different models or sources with varying dimensions in the same FAISS index.

✓ Fix

Ensure all embeddings added to a single FAISS index come from the same model and have identical dimensions.

Code: broken vs fixed

Broken - triggers the error
python
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
Fixed - works correctly
python
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')
Rebuilt the FAISS index from scratch using embeddings generated by the current model to ensure the vector dimensions match and prevent the dimension mismatch error.

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.

Python 3.9+ · faiss-cpu >=1.7.0 · tested on 1.7.x
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.