{"record":{"id":"a548e6ce548610f3","repo":"mastra-ai/mastra","slug":"vector-at-index-i-is-null-or-undefined","errorCode":null,"errorMessage":"Vector at index ${i} is null or undefined","messagePattern":"Vector at index (.+?) is null or undefined","errorType":"error_code","errorClass":"MastraError","httpStatus":null,"severity":"error","filePath":"packages/core/src/vector/validation.ts","lineNumber":100,"sourceCode":"        topK,\n      },\n    });\n  }\n}\n\n/**\n * Validates vector components for NaN/Infinity values\n *\n * @param storeName - Name of the vector store (e.g., 'PG', 'CHROMA')\n * @param vectors - Array of vectors to validate\n * @throws MastraError if any vector contains NaN, Infinity, null, or undefined\n */\nexport function validateVectorValues(storeName: string, vectors: number[][]): void {\n  for (let i = 0; i < vectors.length; i++) {\n    const vector = vectors[i];\n\n    if (!vector) {\n      throw new MastraError({\n        id: createVectorErrorId(storeName, 'UPSERT', 'INVALID_VECTOR'),\n        domain: ErrorDomain.MASTRA_VECTOR,\n        category: ErrorCategory.USER,\n        details: {\n          message: `Vector at index ${i} is null or undefined`,\n          vectorIndex: i,\n        },\n      });\n    }\n\n    for (let j = 0; j < vector.length; j++) {\n      const value = vector[j];\n\n      if (value === null || value === undefined || !Number.isFinite(value)) {\n        throw new MastraError({\n          id: createVectorErrorId(storeName, 'UPSERT', 'INVALID_VECTOR_VALUE'),\n          domain: ErrorDomain.MASTRA_VECTOR,\n          category: ErrorCategory.USER,","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/vector/validation.ts#L82-L118","documentation":"validateVectorValues iterates the vectors array and throws if any element is null or undefined (a missing vector slot). A sparse or partially-filled number[][] cannot be embedded, so upsert fails fast with the offending index.","triggerScenarios":"Passing an array like [[0.1, 0.2], , [0.3]] (holes), or an array built with fixed length where some slots were never assigned, or null entries from a failed embedding per-document.","commonSituations":"Batch embedding where some items failed and were replaced with null placeholders; Array(n) used to preallocate without filling.","solutions":["Filter out null/undefined entries and their associated metadata/ids before upsert","Find the producer that left the hole and fix or retry failed embeddings","Assert every element is an array before calling upsert"],"exampleFix":"// before\nawait store.upsert({ indexName: 'docs', vectors: embeddingResults });\n// after\nconst valid = vectors.filter((v): v is number[] => Array.isArray(v));\nawait store.upsert({ indexName: 'docs', vectors: valid });","handlingStrategy":"type-guard","validationCode":"const hole = vectors.findIndex((v) => v == null);\nif (hole !== -1) throw new Error(`null vector at index ${hole}`);","typeGuard":"function allVectorsPresent(v: unknown[]): v is number[][] {\n  return v.every((x): x is number[] => Array.isArray(x));\n}","tryCatchPattern":"try {\n  await store.upsert({ indexName, vectors });\n} catch (e) {\n  if (e instanceof MastraError && e.id.includes('INVALID_VECTOR')) {\n    console.error(`Bad vector at index ${e.details.vectorIndex}`);\n  }\n  throw e;\n}","preventionTips":["Never preallocate with Array(n) without filling","Filter failed embeddings and their metadata together before upsert","Log failed embedding jobs instead of inserting null placeholders"],"tags":["validation","vector-store","upsert","null-check"],"backgroundTag":"invalid-vector-data","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}