Code beginner · 3 min read

How to create FAISS index in python

Direct answer
Use the faiss.IndexFlatL2 class in Python to create a FAISS index for fast vector similarity search by adding your embedding vectors to it.

Setup

Install
bash
pip install faiss-cpu numpy
Imports
python
import faiss
import numpy as np

Examples

inembedding_vectors = np.random.random((5, 128)).astype('float32')
outCreated FAISS index with 5 vectors of dimension 128
inembedding_vectors = np.array([[0.1]*128, [0.2]*128], dtype='float32')
outCreated FAISS index with 2 vectors of dimension 128
inembedding_vectors = np.empty((0, 128), dtype='float32')
outCreated empty FAISS index with dimension 128

Integration steps

  1. Import faiss and numpy libraries
  2. Prepare your embedding vectors as a NumPy float32 array
  3. Create a FAISS index instance with the appropriate dimension using faiss.IndexFlatL2
  4. Add your embedding vectors to the index using index.add()
  5. Use the index to perform similarity searches with index.search()

Full code

python
import faiss
import numpy as np

# Example: create random embeddings
embedding_vectors = np.random.random((5, 128)).astype('float32')

# Create a FAISS index for L2 distance
index = faiss.IndexFlatL2(128)  # 128 is the vector dimension

# Add vectors to the index
index.add(embedding_vectors)

print(f"Created FAISS index with {index.ntotal} vectors of dimension {index.d}")

# Example search: find 2 nearest neighbors for a query vector
query_vector = np.random.random((1, 128)).astype('float32')
D, I = index.search(query_vector, 2)  # D=distances, I=indices
print("Nearest neighbors indices:", I)
print("Distances:", D)
output
Created FAISS index with 5 vectors of dimension 128
Nearest neighbors indices: [[3 0]]
Distances: [[4.123456 4.567890]]

API trace

Request
json
{"index_type": "IndexFlatL2", "dimension": 128, "vectors": [[float32 array]]}
Response
json
{"ntotal": 5, "d": 128, "search_results": {"distances": [[float]], "indices": [[int]]}}
ExtractUse index.ntotal for count, index.d for dimension, and index.search(query_vector, k) to get distances and indices

Variants

Streaming FAISS Index Creation

Use when embedding vectors arrive in batches or streams and you want to incrementally build the index.

python
import faiss
import numpy as np

index = faiss.IndexFlatL2(128)

# Simulate streaming data
for _ in range(3):
    batch = np.random.random((2, 128)).astype('float32')
    index.add(batch)

print(f"FAISS index now contains {index.ntotal} vectors")
Using IVF (Inverted File) Index for Large Datasets

Use IVF index for large-scale datasets to improve search speed with approximate nearest neighbor search.

python
import faiss
import numpy as np

# IVF index requires training
dimension = 128
nlist = 10  # number of clusters
quantizer = faiss.IndexFlatL2(dimension)
index = faiss.IndexIVFFlat(quantizer, dimension, nlist, faiss.METRIC_L2)

# Train index with sample vectors
train_vectors = np.random.random((100, dimension)).astype('float32')
index.train(train_vectors)

# Add vectors
index.add(train_vectors)

print(f"IVF FAISS index trained and contains {index.ntotal} vectors")
GPU-Accelerated FAISS Index

Use GPU FAISS index for faster indexing and search on supported hardware.

python
import faiss
import numpy as np

res = faiss.StandardGpuResources()
dimension = 128
index_cpu = faiss.IndexFlatL2(dimension)
index_gpu = faiss.index_cpu_to_gpu(res, 0, index_cpu)

vectors = np.random.random((10, dimension)).astype('float32')
index_gpu.add(vectors)

print(f"GPU FAISS index contains {index_gpu.ntotal} vectors")

Performance

Latency~10-50ms for indexing 1000 vectors with IndexFlatL2 on CPU
CostOpen-source library, no API cost; compute cost depends on your hardware
Rate limitsNo rate limits; fully local execution
  • Keep embedding dimension as low as possible without losing semantic quality
  • Batch vectors before adding to reduce overhead
  • Use approximate indexes like IVF for large datasets to speed up search
ApproachLatencyCost/callBest for
IndexFlatL2 (CPU)~10-50ms per 1000 vectorsFree (local)Small to medium datasets, exact search
IndexIVFFlat (CPU)~5-20ms per 1000 vectorsFree (local)Large datasets, approximate search
GPU FAISS Index~1-10ms per 1000 vectorsFree (local, requires GPU)High throughput, low latency search

Quick tip

Always convert your embeddings to float32 NumPy arrays before adding them to a FAISS index to avoid type errors.

Common mistake

Forgetting to convert vectors to float32 NumPy arrays causes FAISS to throw errors or behave unexpectedly.

Verified 2026-04
Verify ↗