mem0ai/mem0 · error · Error
Query dimension mismatch. Expected ${this.dimension}, got ${
Error message
Query dimension mismatch. Expected ${this.dimension}, got ${query.length} What it means
search() rejects any query vector whose length differs from the store's configured dimension before scanning rows, because cosine/dot comparison between differently-sized Float32 arrays is meaningless (and would produce NaN scores). The dimension is fixed when the collection is created, so a mismatch indicates the query was embedded with a different model than the stored data.
Source
Thrown at mem0-ts/src/oss/src/vector_stores/memory.ts:358
id: s.id,
payload: s.payload,
score: s.score,
}));
return results;
} catch (error) {
console.error("Error during keyword search:", error);
return null;
}
}
async search(
query: number[],
topK: number = 10,
filters?: SearchFilters,
): Promise<VectorStoreResult[]> {
if (query.length !== this.dimension) {
throw new Error(
`Query dimension mismatch. Expected ${this.dimension}, got ${query.length}`,
);
}
const rows = this.db.prepare(`SELECT * FROM vectors`).all() as any[];
const results: VectorStoreResult[] = [];
for (const row of rows) {
const vector = new Float32Array(
row.vector.buffer,
row.vector.byteOffset,
row.vector.byteLength / 4,
);
const payload = this.normalizePayload(JSON.parse(row.payload));
const memoryVector: MemoryVector = {
id: row.id,
vector: Array.from(vector),
payload,View on GitHub (pinned to 001c235229)
Solutions
- Use the exact same embedding model/config for search queries as for add()/insert() — ideally route both through the same Memory instance.
- If the stored data is from an old model, wipe the DB and re-embed with the new model.
- In tests, pass a consistent fake embedder (e.g. deterministic N-dim) to both the store config and query embedding.
- Check query.length against store.dimension before calling search() in generic pipeline code.
Example fix
// before
const results = await store.search(otherEmbedder.embed('hello'), 5); // 384 vs 1536 -> throws
// after
const results = await store.search(memoryEmbedder.embed('hello'), 5); // same model as insert Defensive patterns
Strategy: validation
Validate before calling
const expected = store.dimension ?? (await embedder.embed('probe')).length;
if (query.length !== expected) {
throw new Error(`Query dim ${query.length} != store dim ${expected}; check embedder config`);
} Type guard
const matchesStoreDim = (q: number[], dim: number): boolean => Array.isArray(q) && q.length === dim;
Try / catch
try { results = await store.search(query, topK, filters); }
catch (e) {
if (e instanceof Error && e.message.startsWith('Query dimension mismatch')) {
// re-embed query with the same model used for inserts, then retry
} else throw e;
} Prevention
- Route add() and search() through the same Memory/embedder instance.
- In tests, use a deterministic fake embedder with fixed output length everywhere.
- Never hand-craft query vectors; always embed.
When it happens
Trigger: Embedding the search query with a different provider/model than the one used for inserts (e.g. store built with 1536-dim OpenAI vectors, query embedded with a 384-dim local model); calling search() with a raw hand-made vector of arbitrary length; environment-dependent embedder defaults (prod vs test).
Common situations: Swapping the embedder config after data was already stored; unit tests using fake embeddings of length N while the store was created with M; copy-pasting a query pipeline that uses a different embedder instance than the memory instance.
Related errors
- Vector dimension mismatch. Expected ${this.dimension}, got $
- Vector dimension mismatch. Expected ${this.dimension}, got $
- Baidu Mochow table '${label}' stores ${dimension}-dimensiona
- Vector dimension mismatch at index ${i}. Expected ${this.dim
- Query vector dimension mismatch. Expected ${this.dimension},
AI-assisted analysis of mem0ai/mem0@001c235229 (2026-08-15).
Data as JSON: /api/errors/37c9d6fb7c29c0f1.
Report an issue: GitHub.