Repository navigation
Fix: Validate nested vector batches in JavaScript bindings - #813
Merged
ashvardanian merged 1 commit intoOct 24, 2026
Merged
Conversation
`normalizeVectors` summed `vec.length` over the rows of a nested array without looking at what the rows are. A flat `number[]` has no `.length` on its elements, so the total became NaN, `new Float32Array(NaN)` was empty and `index.search([1, 0, 0], 2)` quietly returned an empty batch, while `index.add(8n, [0, 0, 1])` failed with a misleading "length of keys (1) must match the number of vectors (0)". Ragged input such as `[[1, 0], [0, 0, 0, 1]]` was glued into two 3-dimensional vectors because only the total size was checked. Require every row to be an array or a typed array with exactly `dimensions` elements. A flat array of numbers now throws the existing "Vectors must be a TypedArray or an array of arrays." error.
ashvardanian
force-pushed
the
fix/js-validate-vector-shape
branch
from
October 24, 2026 21:30
f82546e to
f9d2acc
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
normalizeVectorsflattens an array of rows by summingvec.lengthover the rows, without checking what a row is. As a result:For a flat
number[],vec.lengthisundefined, sototalLengthbecomesNaN,new Float32Array(NaN)is empty and0 % dimensions === 0passes the later check. Ragged rows are only checked by total size.This is input validation: malformed input now throws an error instead of silently producing an empty result or being reinterpreted.
Change
In
normalizeVectors, for an array input:Arrayor one of the supported typed arrays (Float32Array,Float64Array,Int8Array,Uint8Array), otherwise the existingVectors must be a TypedArray or an array of arrays.error is thrown (this is what a flatnumber[]now gets);dimensionselements, otherwiseEvery vector must have <dimensions> dimensions, but got one with <n>.is thrown.The flattened buffer is then built from
rows * dimensions. Flat typed arrays, arrays of typed arrays and arrays of plain number arrays with the right shape work as before, and so does the existing "multiple of the dimension" check for flat typed arrays.exactSearchsharesnormalizeVectorsand gets the same behaviour.Before / after
Published
[email protected]prebuilt binary, Windows x64, Node 24, dimensions = 3:idx.search([1, 0, 0], 2)BatchMatches, no errorError: Vectors must be a TypedArray or an array of arrays.idx.add(8n, [0, 0, 1])Error: The length of keys (1) must match the number of vectors (0) or be a single key.Error: Vectors must be a TypedArray or an array of arrays.idx.search([[1, 0], [0, 0, 0, 1]], 2)Error: Every vector must have 3 dimensions, but got one with 2.idx.add([8n, 9n], [[1, 0], [0, 0, 0, 1]])Error: Every vector must have 3 dimensions, but got one with 2.idx.search([[1, 0, 0, 0]], 1)Error: The size of the flattened vectors must be a multiple of ...Error: Every vector must have 3 dimensions, but got one with 4.idx.search([[1, 0, 0], [0, 1, 0]], 1)idx.search([new Float32Array([1, 0, 0])], 1)Tests and what I could (not) run
New test
Malformed vector batchesinjavascript/usearch.test.jscovers the cases above forsearch,addandexactSearch, and checks that well-formed batches (arrays of arrays, arrays of typed arrays, a single nested query, flat typed arrays, batchadd) still work.I have no C++ toolchain, so I compiled
javascript/usearch.tswithtsc(CJS config) and ranjavascript/usearch.test.jsagainst theprebuilds/from the published[email protected]npm package (Windows x64 only):viewtests).javascript/usearch.tsreverted tomain-dev, new test kept: 6 of the 7 new subtests fail (the 7th, "well-formed batches are still accepted", passes as expected).main-dev: 31 tests, 28 pass, 3 skipped.Not tested: Linux / macOS / arm64 binaries, building the addon from source, the ESM output.
Behaviour note: rows of other typed-array types (for example
Int16Array) used to be accepted by accident when nested in an array, although the top-level input and the TypeScript types only allow the four types above; they are now rejected with the "array of arrays" error.