High severity intermediate · Fix: 5-10 min

EmbeddingError

langchain.embeddings.base.EmbeddingError

What this error means
LangChain's EmbeddingError dimension mismatch occurs when the embedding vector size returned by the model does not match the expected dimension size.

Stack trace

traceback
langchain.embeddings.base.EmbeddingError: Embedding dimension mismatch: expected 1536 but got 1024
QUICK FIX
Verify and align the embedding model's output dimension with the vector store's expected dimension before indexing.

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

1

Using an embedding model that outputs vectors of a different dimension than the vector store expects

✓ Fix

Ensure the embedding model and vector store are configured to use the same embedding dimension, or switch to a compatible embedding model.

2

Mixing embeddings from different models in the same vector store or index

✓ Fix

Use embeddings from a single consistent model for all vectors stored together to avoid dimension conflicts.

3

Incorrect or outdated vector store configuration specifying wrong embedding dimension

✓ Fix

Update the vector store or index configuration to match the embedding dimension of the model currently in use.

Code: broken vs fixed

Broken - triggers the error
python
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
Fixed - works correctly
python
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.")
Ensured the embedding model and vector store use matching embedding dimensions by confirming the model's default dimension and using consistent configuration.

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.

Python 3.9+ · langchain >=0.1.0 · tested on 0.2.x
Verified 2026-04
Verify ↗

Community Notes

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