How to Fix 'embedding dimension mismatch during development' in LangChain (TypeScript)
You’re seeing this error because the vector store and the embeddings model do not agree on the size of each embedding vector. In LangChain TypeScript, it usually shows up when you change embedding models during development, reuse an old persisted index, or mix documents embedded with one model and queries embedded with another.
The Most Common Cause
The #1 cause is simple: you built your vector store with one embedding model, then later queried it with a different model that returns a different vector size.
Typical error messages look like this:
- •
Error: Vector dimension mismatch - •
Error: Embedding dimension mismatch - •
PineconeClientException: Vector dimension 1536 does not match index dimension 3072 - •
QdrantVectorStoreError: expected vectors of size X, got Y
Here’s the broken pattern:
| Broken | Fixed |
|---|---|
| Create index with one embedding model, query with another | Use the same embedding model for both indexing and querying |
// BROKEN
import { OpenAIEmbeddings } from "@langchain/openai";
import { MemoryVectorStore } from "langchain/vectorstores/memory";
const embeddingsV1 = new OpenAIEmbeddings({
modelName: "text-embedding-3-small", // 1536 dims
});
const store = await MemoryVectorStore.fromDocuments(docs, embeddingsV1);
// Later in dev...
const embeddingsV2 = new OpenAIEmbeddings({
modelName: "text-embedding-3-large", // different dimension
});
const results = await store.similaritySearch("refund policy", 4, embeddingsV2);
// FIXED
import { OpenAIEmbeddings } from "@langchain/openai";
import { MemoryVectorStore } from "langchain/vectorstores/memory";
const embeddings = new OpenAIEmbeddings({
modelName: "text-embedding-3-small",
});
const store = await MemoryVectorStore.fromDocuments(docs, embeddings);
const results = await store.similaritySearch("refund policy", 4);
If you’re using a persistent vector DB like Pinecone, Qdrant, or Weaviate, the same rule applies. The index dimension is fixed at creation time.
// FIXED PATTERN FOR PERSISTENT STORES
const embeddings = new OpenAIEmbeddings({ modelName: "text-embedding-3-small" });
await pineconeIndex.upsert([
{
id: "doc-1",
values: await embeddings.embedQuery("policy text"),
metadata: { source: "policy.pdf" },
},
]);
const queryVector = await embeddings.embedQuery("refund policy");
Other Possible Causes
1) You changed models but kept the old index
This happens constantly during development. You switch from text-embedding-ada-002 to text-embedding-3-large, but your existing Pinecone/Qdrant collection still has the old dimension.
// WRONG: old index still expects 1536-dim vectors
const index = pc.Index("support-kb");
// RIGHT: recreate the index with the new dimension or keep the same model
await pc.createIndex({
name: "support-kb",
dimension: 3072,
});
2) You are mixing local and remote embeddings
A common mistake is embedding documents locally with one provider and querying with another. Even if both are “OpenAI-compatible,” dimensions can differ.
// WRONG
const docEmbeddings = new HuggingFaceTransformersEmbeddings();
const queryEmbeddings = new OpenAIEmbeddings({ modelName: "text-embedding-3-small" });
// RIGHT
const embeddings = new HuggingFaceTransformersEmbeddings();
3) Your persisted cache contains stale vectors
If you serialize vectors to disk during dev, old data can survive code changes. LangChain won’t auto-migrate those vectors.
// Example stale cache path
const cachePath = "./.vector-cache";
// Fix by clearing cache after changing models
// rm -rf ./.vector-cache
4) Your splitter or pipeline changes are not the issue — but they hide the real problem
Chunk size does not change embedding dimension. Developers often blame text splitting because errors appear after re-indexing. The actual bug is usually that re-indexed chunks went into an old collection.
// NOT THE CAUSE OF DIMENSION MISMATCH
const splitter = new RecursiveCharacterTextSplitter({
chunkSize: 1000,
chunkOverlap: 200,
});
How to Debug It
- •
Print the embedding model name everywhere
- •Log the exact model used for document ingestion and query time.
- •If they differ, that’s your bug.
console.log("Doc embedding model:", docEmbeddingModel); console.log("Query embedding model:", queryEmbeddingModel); - •
Check the vector dimension returned by your embedder
- •Run a direct embed call and inspect length.
- •Compare it to your DB/index dimension.
const vec = await embeddings.embedQuery("test"); console.log(vec.length); - •
Inspect your existing vector store schema
- •Pinecone index dimension
- •Qdrant collection vector size
- •Weaviate class vectorizer config
- •
Delete and rebuild the index
- •If you’re in development and unsure, wipe the collection/index and re-ingest.
- •If the error disappears, you had stale vectors or a mismatched schema.
Prevention
- •Pick one embedding model per project and lock it in config.
- •Treat vector stores as schemaful systems; changing models means recreating indexes or collections.
- •Add startup checks that compare
embeddings.embedQuery("ping").lengthagainst your configured index dimension before serving traffic.
If you want a stable LangChain TypeScript setup, make embedding choice part of infrastructure code, not application code. That keeps this class of error out of your dev loop.
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