{"record":{"id":"699cd7834303e498","repo":"mastra-ai/mastra","slug":"vector-contains-invalid-value-null-undefined-na","errorCode":null,"errorMessage":"Vector contains invalid value (null, undefined, NaN, or Infinity) at position [${i}][${j}]","messagePattern":"Vector contains invalid value \\(null, undefined, NaN, or Infinity\\) at position \\[(.+?)\\]\\[(.+?)\\]","errorType":"error_code","errorClass":"MastraError","httpStatus":null,"severity":"error","filePath":"packages/core/src/vector/validation.ts","lineNumber":115,"sourceCode":"    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,\n          details: {\n            message: `Vector contains invalid value (null, undefined, NaN, or Infinity) at position [${i}][${j}]`,\n            vectorIndex: i,\n            componentIndex: j,\n            value: String(value),\n          },\n        });\n      }\n    }\n  }\n}\n\n/**\n * Validates all upsert inputs including vector values\n * Combines validateUpsertInput and validateVectorValues","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/vector/validation.ts#L97-L133","documentation":"validateVectorValues scans each vector's components and rejects null, undefined, NaN, and Infinity values. Embedding vectors must be finite floats; any non-finite component indicates a corrupted embedding or numeric bug.","triggerScenarios":"Embedding model returned NaN (e.g. zero-division in custom embeddings), JSON round-trip produced nulls, dividing by a zero norm when normalizing, or Infinity from overflowing math.","commonSituations":"Custom embedding functions with unstable math; parsing embeddings from CSV/text where missing values become null; overflow in manual vector arithmetic.","solutions":["Sanitize with Number.isFinite check per component before upsert and drop/repair offending vectors","Fix the embedding source: check for zero-norm division and model output validity","Use finite check during normalization: divide only if norm > 0"],"exampleFix":"// before\nconst norm = Math.sqrt(vec.reduce((s, x) => s + x * x, 0));\nconst normalized = vec.map((x) => x / norm);\n// after\nconst norm = Math.sqrt(vec.reduce((s, x) => s + x * x, 0));\nconst normalized = norm > 0 ? vec.map((x) => x / norm) : vec;\nif (!normalized.every(Number.isFinite)) throw new Error('Non-finite embedding');","handlingStrategy":"validation","validationCode":"const bad = vectors.findIndex((v) => Array.isArray(v) && !v.every(Number.isFinite));\nif (bad !== -1) throw new Error(`non-finite value in vector ${bad}`);","typeGuard":"function isFiniteVector(v: unknown): v is number[] {\n  return Array.isArray(v) && v.every((x) => typeof x === 'number' && Number.isFinite(x));\n}","tryCatchPattern":"try {\n  await store.upsert({ indexName, vectors });\n} catch (e) {\n  if (e instanceof MastraError && e.id.includes('INVALID_VECTOR_VALUE')) {\n    const { vectorIndex, valueIndex } = e.details;\n    console.error(`Non-finite value at [${vectorIndex}][${valueIndex}]`);\n  }\n  throw e;\n}","preventionTips":["Guard normalization against zero-norm division","Validate embedding output with Number.isFinite right after generation","Avoid BigInt/overflow-producing arithmetic when transforming vectors"],"tags":["validation","vector-store","upsert","numeric"],"backgroundTag":"invalid-vector-data","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}