{"record":{"id":"35eb99c9574da69a","repo":"mastra-ai/mastra","slug":"ids-array-length-must-match-vectors-array-length","errorCode":null,"errorMessage":"IDs array length must match vectors array length","messagePattern":"IDs array length must match vectors array length","errorType":"error_code","errorClass":"MastraError","httpStatus":null,"severity":"error","filePath":"packages/core/src/vector/validation.ts","lineNumber":54,"sourceCode":"  }\n\n  // Validate metadata length matches vectors length (skip if metadata is empty/not provided)\n  if (metadata && metadata.length > 0 && metadata.length !== vectors.length) {\n    throw new MastraError({\n      id: createVectorErrorId(storeName, 'UPSERT', 'METADATA_LENGTH_MISMATCH'),\n      domain: ErrorDomain.MASTRA_VECTOR,\n      category: ErrorCategory.USER,\n      details: {\n        message: 'Metadata array length must match vectors array length',\n        vectorsLength: vectors.length,\n        metadataLength: metadata.length,\n      },\n    });\n  }\n\n  // Validate ids length matches vectors length\n  if (ids && ids.length !== vectors.length) {\n    throw new MastraError({\n      id: createVectorErrorId(storeName, 'UPSERT', 'IDS_LENGTH_MISMATCH'),\n      domain: ErrorDomain.MASTRA_VECTOR,\n      category: ErrorCategory.USER,\n      details: {\n        message: 'IDs array length must match vectors array length',\n        vectorsLength: vectors.length,\n        idsLength: ids.length,\n      },\n    });\n  }\n}\n\n/**\n * Validates topK parameter for queries\n *\n * @param storeName - Name of the vector store (e.g., 'PG', 'CHROMA')\n * @param topK - Number of results to return\n * @throws MastraError if topK is not a positive integer","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/vector/validation.ts#L36-L72","documentation":"Thrown by validateUpsertInput when the optional ids array length differs from the vectors array length. Every vector must have exactly one corresponding id (or none at all, letting the store generate ids).","triggerScenarios":"Calling upsert with N vectors and M ids where N !== M; reusing a stale ids array after the vectors array changed length; omitting ids for some records while providing others.","commonSituations":"Merging batches from two sources where ids were deduplicated but vectors were not; regenerating embeddings after docs changed without regenerating ids.","solutions":["Ensure ids.length === vectors.length or omit ids entirely","Derive ids from the same source list as vectors so they stay in sync","Add an assertion at the call site comparing both lengths"],"exampleFix":"// before\nawait store.upsert({ indexName: 'docs', vectors, ids: ids.slice(0, 5) });\n// after\nconst idList = vectors.map((_, i) => ids[i] ?? crypto.randomUUID());\nawait store.upsert({ indexName: 'docs', vectors, ids: idList });","handlingStrategy":"validation","validationCode":"if (ids && ids.length !== vectors.length) {\n  throw new Error(`ids ${ids.length} != vectors ${vectors.length}`);\n}","typeGuard":"function idsMatch(vectors: unknown[], ids?: string[]): ids is string[] {\n  return Array.isArray(ids) && ids.length === vectors.length;\n}","tryCatchPattern":"try {\n  await store.upsert({ indexName, vectors, ids });\n} catch (e) {\n  if (e instanceof MastraError && e.id.includes('IDS_LENGTH_MISMATCH')) {\n    console.error('ids out of sync with vectors', e.details);\n  }\n  throw e;\n}","preventionTips":["Generate ids in the same map as vectors (vectors.map + crypto.randomUUID)","Regenerate ids whenever the document list changes","Omit ids entirely if you do not need deterministic ids"],"tags":["validation","vector-store","upsert","ids"],"backgroundTag":"array-length-mismatch","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}