ValueError
builtins.ValueError
Stack trace
ValueError: Embedding dimension mismatch: expected 1536 but got 1024
Why it happens
Embedding models produce fixed-size vectors, and vector stores or memory systems expect embeddings of a specific dimension. If you use embeddings from a different model or version than expected, the dimensions won't match, causing this error.
Detection
Check the embedding vector shape immediately after generation and assert it matches the expected dimension before inserting into memory or vector stores.
Causes & fixes
Using embeddings from a different model than the one the memory expects
Ensure the embedding model used matches the one configured for your memory or vector store, e.g., both use 'text-embedding-3-large' with 1536 dimensions.
Upgrading or changing the embedding model without updating the memory schema
Update the memory or vector store configuration to accept the new embedding dimension or revert to the previous embedding model.
Manually mixing embeddings from different sources or models in the same memory
Normalize embedding sources by using a single embedding model or separate memories per embedding dimension.
Code: broken vs fixed
from openai import OpenAI
import os
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# Generate embedding with one model
embedding = client.embeddings.create(input="Hello world", model="text-embedding-3-small")
# Insert into memory expecting 1536 dims
memory.insert("doc1", embedding["data"][0]["embedding"]) # Raises ValueError here from openai import OpenAI
import os
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# Generate embedding with the correct model matching memory
embedding = client.embeddings.create(input="Hello world", model="text-embedding-3-large") # 1536 dims
# Insert into memory expecting 1536 dims
memory.insert("doc1", embedding["data"][0]["embedding"]) # Works correctly
print("Embedding inserted successfully with matching dimension.") Workaround
Catch the ValueError on insertion, then log the embedding dimension and discard or convert embeddings that do not match the expected size before retrying.
Prevention
Standardize on a single embedding model across your entire AI memory pipeline and validate embedding dimensions at generation time to avoid mismatches.