mastra-ai/mastra · error · MastraError
Vector contains invalid value (null, undefined, NaN, or Infi
Error message
Vector contains invalid value (null, undefined, NaN, or Infinity) at position [${i}][${j}] What it means
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.
Source
Thrown at packages/core/src/vector/validation.ts:115
const vector = vectors[i];
if (!vector) {
throw new MastraError({
id: createVectorErrorId(storeName, 'UPSERT', 'INVALID_VECTOR'),
domain: ErrorDomain.MASTRA_VECTOR,
category: ErrorCategory.USER,
details: {
message: `Vector at index ${i} is null or undefined`,
vectorIndex: i,
},
});
}
for (let j = 0; j < vector.length; j++) {
const value = vector[j];
if (value === null || value === undefined || !Number.isFinite(value)) {
throw new MastraError({
id: createVectorErrorId(storeName, 'UPSERT', 'INVALID_VECTOR_VALUE'),
domain: ErrorDomain.MASTRA_VECTOR,
category: ErrorCategory.USER,
details: {
message: `Vector contains invalid value (null, undefined, NaN, or Infinity) at position [${i}][${j}]`,
vectorIndex: i,
componentIndex: j,
value: String(value),
},
});
}
}
}
}
/**
* Validates all upsert inputs including vector values
* Combines validateUpsertInput and validateVectorValuesView on GitHub (pinned to 75dd419e61)
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
Example fix
// before
const norm = Math.sqrt(vec.reduce((s, x) => s + x * x, 0));
const normalized = vec.map((x) => x / norm);
// after
const norm = Math.sqrt(vec.reduce((s, x) => s + x * x, 0));
const normalized = norm > 0 ? vec.map((x) => x / norm) : vec;
if (!normalized.every(Number.isFinite)) throw new Error('Non-finite embedding'); Defensive patterns
Strategy: validation
Validate before calling
const bad = vectors.findIndex((v) => Array.isArray(v) && !v.every(Number.isFinite));
if (bad !== -1) throw new Error(`non-finite value in vector ${bad}`); Type guard
function isFiniteVector(v: unknown): v is number[] {
return Array.isArray(v) && v.every((x) => typeof x === 'number' && Number.isFinite(x));
} Try / catch
try {
await store.upsert({ indexName, vectors });
} catch (e) {
if (e instanceof MastraError && e.id.includes('INVALID_VECTOR_VALUE')) {
const { vectorIndex, valueIndex } = e.details;
console.error(`Non-finite value at [${vectorIndex}][${valueIndex}]`);
}
throw e;
} Prevention
- Guard normalization against zero-norm division
- Validate embedding output with Number.isFinite right after generation
- Avoid BigInt/overflow-producing arithmetic when transforming vectors
When it happens
Trigger: 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.
Common situations: Custom embedding functions with unstable math; parsing embeddings from CSV/text where missing values become null; overflow in manual vector arithmetic.
Related errors
- Vectors array cannot be empty
- Metadata array length must match vectors array length
- IDs array length must match vectors array length
- Vector at index ${i} is null or undefined
- ${flag} must be a positive integer
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/699cd7834303e498.
Report an issue: GitHub.