{"record":{"id":"3806f9dc397d782d","repo":"mem0ai/mem0","slug":"context-dimension-mismatch-expected-this-dim-3806f9","errorCode":null,"errorMessage":"${context} dimension mismatch. Expected ${this.dimension}, got ${vector.length}","messagePattern":"(.+?) dimension mismatch\\. Expected (.+?), got (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"mem0-ts/src/oss/src/vector_stores/s3_vectors.ts","lineNumber":862,"sourceCode":"  private normalizeScore(\n    distance?: number,\n    distanceMetric: \"cosine\" | \"euclidean\" = this.distanceMetric,\n  ): number | undefined {\n    if (distance === undefined || distance === null) {\n      return undefined;\n    }\n    if (!Number.isFinite(distance)) {\n      return undefined;\n    }\n    if (distanceMetric === \"euclidean\") {\n      return 1 / (1 + distance);\n    }\n    return Math.max(0, Math.min(1, 1 - distance));\n  }\n\n  private assertVectorDimension(vector: number[], context: string): void {\n    if (vector.length !== this.dimension) {\n      throw new Error(\n        `${context} dimension mismatch. Expected ${this.dimension}, got ${vector.length}`,\n      );\n    }\n  }\n\n  private assertBatchDimensions(vectors: number[][], context: string): void {\n    for (const vector of vectors) {\n      this.assertVectorDimension(vector, context);\n    }\n  }\n\n  private isNotFound(error: any): boolean {\n    return error?.name === \"NotFoundException\";\n  }\n\n  private isConflict(error: any): boolean {\n    return error?.name === \"ConflictException\";\n  }","sourceCodeStart":844,"sourceCodeEnd":880,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0-ts/src/oss/src/vector_stores/s3_vectors.ts#L844-L880","documentation":"Every vector written to or queried against the S3 Vectors index must have exactly this.dimension components (fixed by the embedding model / index creation). assertVectorDimension throws with the failing context (e.g. insert, query) when a vector's length differs, protecting the index from corrupt data and AWS-side rejects.","triggerScenarios":"Calling add/search with a custom embedding whose dimension differs from the index; switching embedding models (e.g. text-embedding-3-small 1536 -> bge 768) without recreating the S3 Vectors index/collection; mixing manually supplied vectors with model-generated ones.","commonSituations":"Changing the embedder config after data was already indexed; using embeddingModelDims that doesn't match the actual model output; passing truncated or padded vectors from a custom pipeline.","solutions":["Align the embedding model and embeddingModelDims config with the dimension the index was created with, then recreate the collection if the model changed.","If you supply vectors yourself, verify vector.length === configured dimension before calling add/search.","Re-index existing memories with the new embedding model into a fresh collection."],"exampleFix":"// before\nconst memory = new Memory({ vectorStore: { provider: 's3_vectors', config: { embeddingModelDims: 1536 } }, embedder: new OpenAIEmbedding({ model: 'text-embedding-3-large' }) }); // 3072-dim model\n\n// after\nconst memory = new Memory({ vectorStore: { provider: 's3_vectors', config: { embeddingModelDims: 3072 } }, embedder: new OpenAIEmbedding({ model: 'text-embedding-3-large' }) });","handlingStrategy":"type-guard","validationCode":"const expected = storeConfig.embeddingModelDims;\nif (vector.length !== expected) throw new Error(`Vector has ${vector.length} dims, index expects ${expected}; re-embed or recreate index`);","typeGuard":"function isCorrectDimension(vector: number[], dims: number): vector is number[] & { length: dims } {\n  return Array.isArray(vector) && vector.length === dims;\n}","tryCatchPattern":"try { await memory.add(text, { embeddingVector }); } catch (e) { if (e instanceof Error && e.message.includes('dimension mismatch')) { /* re-embed with the configured model or recreate index */ } else throw e; }","preventionTips":["Pin embeddingModelDims to the model's actual output","Recreate the collection when switching embedders","Add a startup assert comparing a sample embedding's length to the config"],"tags":["s3-vectors","dimensions","embeddings","config"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}