ValueError
builtins.ValueError
Stack trace
ValueError: Embedding dimension mismatch: expected 1536 but got 768
File "/app/main.py", line 42, in add_documents
vectorstore.add(documents)
File "/usr/local/lib/python3.9/site-packages/llama_index/vector_stores/simple_vector_store.py", line 88, in add
raise ValueError(f"Embedding dimension mismatch: expected {self.embedding_dim} but got {embedding_dim}") Why it happens
LlamaIndex VectorStore expects all embeddings to have the same fixed dimension size. This error happens when you try to add documents with embeddings generated from a different model or configuration that produces vectors of a different size than the VectorStore was initialized with.
Detection
Check the embedding dimension attribute of your VectorStore and compare it with the dimension of embeddings generated for new documents before adding them to catch mismatches early.
Causes & fixes
Using different embedding models for existing VectorStore and new documents
Ensure you use the same embedding model and configuration consistently for both initial VectorStore creation and all subsequent document embeddings.
VectorStore initialized with default embedding dimension but new embeddings have a different size
Explicitly specify the embedding dimension when initializing the VectorStore to match the embedding model output dimension.
Embedding model configuration changed (e.g., switching from OpenAI text-embedding-ada-002 to a smaller model)
Regenerate the entire VectorStore with embeddings from the new model to maintain consistent dimensions.
Code: broken vs fixed
from llama_index import VectorStore
vectorstore = VectorStore(embedding_dim=1536)
new_embedding = get_embedding(text) # returns 768-dim vector
vectorstore.add([new_embedding]) # ValueError: Embedding dimension mismatch import os
from llama_index import VectorStore
from openai import OpenAI
embedding_dim = 1536 # Match embedding model output dimension
vectorstore = VectorStore(embedding_dim=embedding_dim)
# Assume get_embedding uses the same model that outputs 1536-dim vectors
new_embedding = get_embedding(text) # returns 1536-dim vector
vectorstore.add([new_embedding]) # No error
print('Added embedding with matching dimension successfully') Workaround
Catch the ValueError when adding embeddings, then check and convert or regenerate embeddings to the correct dimension before retrying the add operation.
Prevention
Standardize on a single embedding model and dimension across your entire pipeline and enforce dimension checks before adding to the VectorStore to prevent mismatches.