{"record":{"id":"37c9d6fb7c29c0f1","repo":"mem0ai/mem0","slug":"query-dimension-mismatch-expected-this-dimensio","errorCode":null,"errorMessage":"Query dimension mismatch. Expected ${this.dimension}, got ${query.length}","messagePattern":"Query dimension mismatch\\. Expected (.+?), got (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"mem0-ts/src/oss/src/vector_stores/memory.ts","lineNumber":358,"sourceCode":"          id: s.id,\n          payload: s.payload,\n          score: s.score,\n        }));\n\n      return results;\n    } catch (error) {\n      console.error(\"Error during keyword search:\", error);\n      return null;\n    }\n  }\n\n  async search(\n    query: number[],\n    topK: number = 10,\n    filters?: SearchFilters,\n  ): Promise<VectorStoreResult[]> {\n    if (query.length !== this.dimension) {\n      throw new Error(\n        `Query dimension mismatch. Expected ${this.dimension}, got ${query.length}`,\n      );\n    }\n\n    const rows = this.db.prepare(`SELECT * FROM vectors`).all() as any[];\n    const results: VectorStoreResult[] = [];\n\n    for (const row of rows) {\n      const vector = new Float32Array(\n        row.vector.buffer,\n        row.vector.byteOffset,\n        row.vector.byteLength / 4,\n      );\n      const payload = this.normalizePayload(JSON.parse(row.payload));\n      const memoryVector: MemoryVector = {\n        id: row.id,\n        vector: Array.from(vector),\n        payload,","sourceCodeStart":340,"sourceCodeEnd":376,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0-ts/src/oss/src/vector_stores/memory.ts#L340-L376","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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."],"exampleFix":"// before\nconst results = await store.search(otherEmbedder.embed('hello'), 5); // 384 vs 1536 -> throws\n\n// after\nconst results = await store.search(memoryEmbedder.embed('hello'), 5); // same model as insert","handlingStrategy":"validation","validationCode":"const expected = store.dimension ?? (await embedder.embed('probe')).length;\nif (query.length !== expected) {\n  throw new Error(`Query dim ${query.length} != store dim ${expected}; check embedder config`);\n}","typeGuard":"const matchesStoreDim = (q: number[], dim: number): boolean => Array.isArray(q) && q.length === dim;","tryCatchPattern":"try { results = await store.search(query, topK, filters); }\ncatch (e) {\n  if (e instanceof Error && e.message.startsWith('Query dimension mismatch')) {\n    // re-embed query with the same model used for inserts, then retry\n  } else throw e;\n}","preventionTips":["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."],"tags":["dimension-mismatch","embeddings","sqlite","vector-store","search"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}