How to Fix 'embedding dimension mismatch' in LangChain (TypeScript)
Opening
embedding dimension mismatch means the vector you’re trying to store or compare does not have the same length as the vectors already in your index. In LangChain TypeScript, this usually shows up when you swap embedding models, reuse an old vector store, or query a collection built with a different dimension.
The error often appears during addDocuments(), similaritySearch(), or when creating a vector store from existing data. The underlying message usually comes from the vector database, not LangChain itself.
The Most Common Cause
The #1 cause is simple: you created the vector store with one embedding model, then later queried or inserted with another model that produces a different vector size.
Here’s the broken pattern.
| Broken code | Fixed code |
|---|---|
| ```ts | |
| import { OpenAIEmbeddings } from "@langchain/openai"; | |
| import { MemoryVectorStore } from "langchain/vectorstores/memory"; |
const store = new MemoryVectorStore( new OpenAIEmbeddings({ model: "text-embedding-3-small" }) );
// Later in another file or deploy: const newEmbeddings = new OpenAIEmbeddings({ model: "text-embedding-3-large", });
await store.addDocuments(docs, { embeddings: newEmbeddings });
|ts
import { OpenAIEmbeddings } from "@langchain/openai";
import { MemoryVectorStore } from "langchain/vectorstores/memory";
const embeddings = new OpenAIEmbeddings({ model: "text-embedding-3-small", });
const store = new MemoryVectorStore(embeddings);
await store.addDocuments(docs);
The key rule: **one vector store, one embedding dimension**. If you build a collection with `1536`-dimensional vectors, every insert and query must use the same embedding model output shape.
If you’re using a persistent DB like Pinecone, Qdrant, Weaviate, Chroma, or pgvector, the same rule applies. A collection created with one dimension cannot accept vectors of another dimension.
## Other Possible Causes
### 1) Reusing an old collection after changing models
This happens when your database still contains vectors from an older embedding model.
```ts
// Old index was built with text-embedding-ada-002 (1536 dims)
// New code uses text-embedding-3-large (3072 dims)
const embeddings = new OpenAIEmbeddings({
model: "text-embedding-3-large",
});
Fix:
- •Delete and rebuild the collection
- •Or keep using the original embedding model
2) Mixing embedding providers
Different providers almost never return the same dimensions.
import { OpenAIEmbeddings } from "@langchain/openai";
import { CohereEmbeddings } from "@langchain/cohere";
// Broken: index built with OpenAI, queries sent with Cohere
const indexEmbeddings = new OpenAIEmbeddings({ model: "text-embedding-3-small" });
const queryEmbeddings = new CohereEmbeddings({ model: "embed-english-v3.0" });
Fix:
- •Use the same provider for both indexing and querying
- •Or create separate indexes per provider
3) Wrong index configuration in your vector DB
Some databases require you to declare dimensions up front.
// Pinecone example
await pinecone.createIndex({
name: "support-tickets",
dimension: 1536,
metric: "cosine",
});
If your embeddings now produce 3072, inserts will fail with errors like:
- •
PineconeClientError: Vector dimension 3072 does not match the dimension of the index 1536 - •
Error: embedding dimension mismatch
Fix:
- •Match index dimension to the embedding model output
- •Recreate the index if needed
4) Accidentally truncating or transforming vectors
If you manually manipulate embeddings before inserting them, you can break their shape.
// Broken
const vector = await embeddings.embedQuery(text);
const shortened = vector.slice(0, 128); // do not do this
Fix:
- •Pass raw embedding arrays into LangChain/vector DBs
- •Do not truncate unless your whole pipeline is designed for that exact size
How to Debug It
- •
Print the embedding length at runtime
Check what your model actually returns before it hits the DB.
const vec = await embeddings.embedQuery("test"); console.log("Embedding length:", vec.length);Compare that to your stored index dimension.
- •
Check which model created the existing collection
Look at old deploys, migration scripts, and seed jobs. If the collection was created by
text-embedding-ada-002, but current code usestext-embedding-3-large, that’s your mismatch. - •
Inspect the exact failing call
The failure usually happens in one of these places:
- •
vectorStore.addDocuments() - •
vectorStore.similaritySearch() - •
PineconeStore.fromDocuments() - •
QdrantVectorStore.fromDocuments()
The stack trace tells you whether the problem is on insert or query.
- •
- •
Check database schema / index metadata
For pgvector:
\d your_tableFor Pinecone/Qdrant/Weaviate:
- •confirm collection/index dimension
- •confirm metric type
- •confirm no stale data remains from older runs
Prevention
- •
Pin one embedding model per project and treat it as part of your schema.
- •
Store embedding metadata alongside documents, including provider and model name.
- •
Add a startup check that compares expected dimension vs actual runtime output:
const expectedDim = 1536; const actualDim = (await embeddings.embedQuery("healthcheck")).length; if (actualDim !== expectedDim) { throw new Error( `Embedding dim mismatch: expected ${expectedDim}, got ${actualDim}` ); }
If you hit embedding dimension mismatch in LangChain TypeScript, don’t start by rewriting your retrieval logic. Check the embedding model first. In most cases, this is a schema mismatch disguised as an application bug.
Keep learning
- •The complete AI Agents Roadmap — my full 8-step breakdown
- •Free: The AI Agent Starter Kit — PDF checklist + starter code
- •Work with me — I build AI for banks and insurance companies
By Cyprian Aarons, AI Consultant at Topiax.
Want the complete 8-step roadmap?
Grab the free AI Agent Starter Kit — architecture templates, compliance checklists, and a 7-email deep-dive course.
Get the Starter Kit