High severity intermediate · Fix: 2-5 min

ValueError

builtins.ValueError

What this error means
This error occurs when the embedding vector dimension returned by the model does not match the expected dimension of the memory or vector store.

Stack trace

traceback
ValueError: Embedding dimension mismatch: expected 1536 but got 1024
QUICK FIX
Verify and unify the embedding model used for both embedding generation and memory storage to ensure dimension consistency.

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

1

Using embeddings from a different model than the one the memory expects

✓ Fix

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.

2

Upgrading or changing the embedding model without updating the memory schema

✓ Fix

Update the memory or vector store configuration to accept the new embedding dimension or revert to the previous embedding model.

3

Manually mixing embeddings from different sources or models in the same memory

✓ Fix

Normalize embedding sources by using a single embedding model or separate memories per embedding dimension.

Code: broken vs fixed

Broken - triggers the error
python
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
Fixed - works correctly
python
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.")
Changed the embedding model to match the expected dimension of the memory, preventing the dimension mismatch error.

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.

Python 3.9+ · openai >=1.0.0 · tested on 1.x
Verified 2026-04 · text-embedding-3-large, text-embedding-3-small
Verify ↗

Community Notes

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