EmbeddingError
langchain.embeddings.base.EmbeddingError
Stack trace
langchain.embeddings.base.EmbeddingError: Embedding dimension mismatch: expected 1536 but got 1024
Why it happens
This error happens because the embedding model used returns vectors of a different dimension than what LangChain expects or what downstream components require. It often occurs when mixing embedding models or using a model with a different embedding size than the vector store or index configuration.
Detection
Check the embedding vector shape immediately after generation and assert it matches the expected dimension before passing it to vector stores or similarity search components.
Causes & fixes
Using an embedding model that outputs vectors of a different dimension than the vector store expects
Ensure the embedding model and vector store are configured to use the same embedding dimension, or switch to a compatible embedding model.
Mixing embeddings from different models in the same vector store or index
Use embeddings from a single consistent model for all vectors stored together to avoid dimension conflicts.
Incorrect or outdated vector store configuration specifying wrong embedding dimension
Update the vector store or index configuration to match the embedding dimension of the model currently in use.
Code: broken vs fixed
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import FAISS
embeddings = OpenAIEmbeddings() # Default dimension 1536
texts = ["Hello world"]
# This will fail if FAISS index expects a different dimension
faiss_index = FAISS.from_texts(texts, embeddings) # triggers EmbeddingError dimension mismatch import os
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import FAISS
os.environ["OPENAI_API_KEY"] = os.environ.get("OPENAI_API_KEY", "") # Use env var for API key
embeddings = OpenAIEmbeddings() # Confirm default dimension 1536
texts = ["Hello world"]
# Ensure FAISS index is created with matching embedding dimension
faiss_index = FAISS.from_texts(texts, embeddings) # fixed: embedding dimension matches
print("FAISS index created successfully with matching embedding dimension.") Workaround
Catch EmbeddingError exceptions and log the embedding vector shape; if mismatch occurs, regenerate embeddings with a compatible model or reconfigure the vector store dynamically.
Prevention
Standardize on a single embedding model across your application and configure vector stores explicitly with the correct embedding dimension to avoid mismatches.