High severity intermediate · Fix: 5-10 min

ValueError

builtins.ValueError

What this error means
This error occurs when the embedding vector dimension of new data does not match the dimension expected by the existing LlamaIndex VectorStore.

Stack trace

traceback
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}")
QUICK FIX
Recreate the VectorStore using embeddings from the same model and dimension as your new documents.

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

1

Using different embedding models for existing VectorStore and new documents

✓ Fix

Ensure you use the same embedding model and configuration consistently for both initial VectorStore creation and all subsequent document embeddings.

2

VectorStore initialized with default embedding dimension but new embeddings have a different size

✓ Fix

Explicitly specify the embedding dimension when initializing the VectorStore to match the embedding model output dimension.

3

Embedding model configuration changed (e.g., switching from OpenAI text-embedding-ada-002 to a smaller model)

✓ Fix

Regenerate the entire VectorStore with embeddings from the new model to maintain consistent dimensions.

Code: broken vs fixed

Broken - triggers the error
python
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
Fixed - works correctly
python
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')
Explicitly set and match the embedding dimension in VectorStore initialization and ensure embeddings come from the same model to avoid dimension mismatch.

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.

Python 3.9+ · llama-index >=0.5.0 · tested on 0.6.x
Verified 2026-04
Verify ↗

Community Notes

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