Skip to content

Fix: Validate nested vector batches in JavaScript bindings - #813

Merged
ashvardanian merged 1 commit into
unum-cloud:main-devfrom
OsamaAnsar:fix/js-validate-vector-shape
Oct 24, 2026
Merged

ashvardanian merged 1 commit into
unum-cloud:main-devfrom
OsamaAnsar:fix/js-validate-vector-shape

Conversation

@OsamaAnsar

Copy link
Copy Markdown
Contributor

normalizeVectors flattens an array of rows by summing vec.length over the rows, without checking what a row is. As a result:

const idx = new Index({ dimensions: 3, metric: "cos" });
idx.add(1n, new Float32Array([1, 0, 0]));
idx.add(2n, new Float32Array([0, 1, 0]));

idx.search([1, 0, 0], 2);
// -> BatchMatches { keys: [], distances: [], counts: [], k: 2 }   (no error, empty result)

idx.add(8n, [0, 0, 1]);
// -> Error: The length of keys (1) must match the number of vectors (0) or be a single key.   (misleading)

idx.search([[1, 0], [0, 0, 0, 1]], 2);
// -> accepted; the rows are glued into two 3-dimensional vectors because only the total size (6) is a multiple of 3

For a flat number[], vec.length is undefined, so totalLength becomes NaN, new Float32Array(NaN) is empty and 0 % dimensions === 0 passes 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:

  • every row must be an Array or one of the supported typed arrays (Float32Array, Float64Array, Int8Array, Uint8Array), otherwise the existing Vectors must be a TypedArray or an array of arrays. error is thrown (this is what a flat number[] now gets);
  • every row must have exactly dimensions elements, otherwise Every 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. exactSearch shares normalizeVectors and gets the same behaviour.

Before / after

Published [email protected] prebuilt binary, Windows x64, Node 24, dimensions = 3:

call before after
idx.search([1, 0, 0], 2) empty BatchMatches, no error Error: 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) accepted, returns 2 result rows Error: Every vector must have 3 dimensions, but got one with 2.
idx.add([8n, 9n], [[1, 0], [0, 0, 0, 1]]) accepted 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) works works, same result
idx.search([new Float32Array([1, 0, 0])], 1) works works, same result

Tests and what I could (not) run

New test Malformed vector batches in javascript/usearch.test.js covers the cases above for search, add and exactSearch, and checks that well-formed batches (arrays of arrays, arrays of typed arrays, a single nested query, flat typed arrays, batch add) still work.

I have no C++ toolchain, so I compiled javascript/usearch.ts with tsc (CJS config) and ran javascript/usearch.test.js against the prebuilds/ from the published [email protected] npm package (Windows x64 only):

  • With the change: 39 tests, 36 pass, 0 fail, 3 skipped (existing view tests).
  • Only javascript/usearch.ts reverted to main-dev, new test kept: 6 of the 7 new subtests fail (the 7th, "well-formed batches are still accepted", passes as expected).
  • Baseline 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.

`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
ashvardanian force-pushed the fix/js-validate-vector-shape branch from f82546e to f9d2acc Compare October 24, 2026 21:30
@ashvardanian
ashvardanian merged commit 8480ece into unum-cloud:main-dev Oct 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants