How to Fix 'embedding dimension mismatch when scaling' in LangChain (TypeScript)
When you see embedding dimension mismatch when scaling, LangChain is telling you that the vector size coming out of your embedding model does not match the vector index or store you’re writing to. This usually shows up when you switch embedding models, reuse an old vector database, or mix documents embedded with one model and queries embedded with another.
In TypeScript projects, this often appears during ingestion or similarity search, especially with Pinecone, Qdrant, Supabase Vector, or Postgres pgvector. The failure is not in LangChain itself; it’s almost always a schema drift problem between your embeddings and your store.
The Most Common Cause
The #1 cause is changing the embedding model without rebuilding the index.
If your existing vector store was created with a 1536-dimensional model like text-embedding-3-small, and you later query it with a 3072-dimensional model like text-embedding-3-large, you’ll get dimension mismatch errors.
Here’s the broken pattern:
import { OpenAIEmbeddings } from "@langchain/openai";
import { PineconeStore } from "@langchain/pinecone";
import { Pinecone } from "@pinecone-database/pinecone";
const pinecone = new Pinecone({ apiKey: process.env.PINECONE_API_KEY! });
const index = pinecone.Index("support-docs");
// Broken: index was created for 1536-dim embeddings,
// but this model returns 3072 dims.
const embeddings = new OpenAIEmbeddings({
model: "text-embedding-3-large",
});
const vectorStore = await PineconeStore.fromExistingIndex(embeddings, {
pineconeIndex: index,
namespace: "prod",
});
await vectorStore.similaritySearch("How do I reset my password?", 4);
And here’s the fixed version:
import { OpenAIEmbeddings } from "@langchain/openai";
import { PineconeStore } from "@langchain/pinecone";
import { Pinecone } from "@pinecone-database/pinecone";
const pinecone = new Pinecone({ apiKey: process.env.PINECONE_API_KEY! });
const index = pinecone.Index("support-docs");
// Fix: use the same embedding model that created the index
const embeddings = new OpenAIEmbeddings({
model: "text-embedding-3-small",
});
const vectorStore = await PineconeStore.fromExistingIndex(embeddings, {
pineconeIndex: index,
namespace: "prod",
});
await vectorStore.similaritySearch("How do I reset my password?", 4);
If you want to change models, rebuild the index. Don’t try to “scale” or pad vectors manually. That just moves the bug around.
Other Possible Causes
1. Mixing old documents with new query embeddings
You may have re-ingested only part of your corpus after switching models.
// Broken: docs were embedded with model A,
// query uses model B
const docEmbeddings = new OpenAIEmbeddings({ model: "text-embedding-3-small" });
const queryEmbeddings = new OpenAIEmbeddings({ model: "text-embedding-3-large" });
Fix by reprocessing all documents and queries with one embedding config.
2. Wrong vector store dimension setting
Some stores require explicit dimensions at creation time.
// Qdrant / pgvector-style mistake
await client.createCollection("docs", {
vectors: {
size: 1536,
distance: "Cosine",
},
});
If your embedding model returns 3072 dimensions, this collection must be recreated with size: 3072.
3. Reusing an index across environments
A dev environment may point at a prod index with different dimensions.
const indexName = process.env.PINECONE_INDEX!; // same name in dev/prod
Check that each environment uses the right index name and schema version.
4. Custom embedding wrapper returning inconsistent output
If you wrap embeddings yourself, make sure every call returns identical length vectors.
class BadEmbeddings {
async embedQuery(text: string) {
return text.length > 100 ? [0.1, 0.2] : [0.1, 0.2, 0.3];
}
}
LangChain stores assume fixed-length vectors. Variable-length output will fail fast.
How to Debug It
- •
Print the embedding length
const vec = await embeddings.embedQuery("test"); console.log(vec.length);Compare that number to your store/index dimension.
- •
Inspect the existing collection/index schema
- •Pinecone: check index dimension in console or via API
- •Qdrant: inspect collection vectors size
- •pgvector: check column type and expected vector length
- •
Verify which embedding model is actually running
- •Log
modelconfig at startup - •Confirm env vars aren’t overriding it in CI/CD
- •Check for fallback defaults in shared config files
- •Log
- •
Test a fresh empty index If a new index works, your old data is stale. That means you need a full re-embed and reindex job, not a code patch.
Prevention
- •Keep embedding model choice versioned alongside the vector store schema.
- •Add a startup check that compares
embedQuery("ping").lengthagainst the target index dimension. - •When changing models, treat it like a migration:
- •create a new index/collection
- •backfill all documents
- •switch traffic after validation
A good rule: if your embeddings change, your storage schema changes too. In LangChain TypeScript systems, those two are coupled whether you like it or not.
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