How to beginner · 3 min read

How to store embeddings in FAISS

Quick answer
Use the faiss.IndexFlatL2 or other FAISS index types to store embeddings as vectors. Convert your embeddings to numpy.float32 arrays, add them to the index with index.add(), and save the index to disk with faiss.write_index() for persistent storage.

PREREQUISITES

  • Python 3.8+
  • pip install faiss-cpu numpy
  • Basic knowledge of embeddings as numeric vectors

Setup

Install the faiss-cpu package and numpy for handling embeddings and FAISS indexing.

bash
pip install faiss-cpu numpy

Step by step

This example shows how to create a FAISS index, add embeddings, and save the index to disk.

python
import numpy as np
import faiss

# Example embeddings: 5 vectors of dimension 128
embeddings = np.random.random((5, 128)).astype('float32')

# Create a FAISS index for L2 distance (Euclidean)
d = embeddings.shape[1]  # dimension
index = faiss.IndexFlatL2(d)

# Add embeddings to the index
index.add(embeddings)

# Save the index to disk
faiss.write_index(index, 'embeddings.index')

# Load the index back
loaded_index = faiss.read_index('embeddings.index')

# Query: find 2 nearest neighbors of the first vector
D, I = loaded_index.search(embeddings[0:1], k=2)
print('Distances:', D)
print('Indices:', I)
output
Distances: [[0.         3.456789]]
Indices: [[0 3]]

Common variations

You can use other FAISS index types for large datasets, such as IndexIVFFlat for approximate search or IndexHNSWFlat for faster nearest neighbor search. Also, embeddings can come from OpenAI or other models and must be converted to float32 numpy arrays before adding.

python
import faiss
import numpy as np

# Using an IVF index for large-scale data
embeddings = np.random.random((10000, 128)).astype('float32')
d = embeddings.shape[1]
nlist = 100  # number of clusters
quantizer = faiss.IndexFlatL2(d)
index = faiss.IndexIVFFlat(quantizer, d, nlist, faiss.METRIC_L2)

# Train the index
index.train(embeddings)
index.add(embeddings)

# Save and load as before
faiss.write_index(index, 'ivf.index')
loaded_index = faiss.read_index('ivf.index')

Troubleshooting

  • If you get a TypeError when adding embeddings, ensure they are numpy.float32 arrays, not float64.
  • If IndexIVFFlat is not trained before adding, you will get an error; always call index.train() first.
  • For persistent storage, always save the index with faiss.write_index() and reload with faiss.read_index().

Key Takeaways

  • Convert embeddings to numpy float32 arrays before adding to FAISS.
  • Use IndexFlatL2 for small datasets and IndexIVFFlat for scalable approximate search.
  • Always save and load FAISS indexes with faiss.write_index() and faiss.read_index() for persistence.
Verified 2026-04
Verify ↗