Skip to content

Fix SparseVector error for non-1D NumPy arrays - #175

Open
Aftabbs wants to merge 1 commit into
pgvector:masterfrom
Aftabbs:fix/sparsevector-ndarray-ndim
Open

Aftabbs wants to merge 1 commit into
pgvector:masterfrom
Aftabbs:fix/sparsevector-ndarray-ndim

Conversation

@Aftabbs

@Aftabbs Aftabbs commented Nov 13, 2026

Copy link
Copy Markdown

Summary

SparseVector accepted any NumPy array and only failed (or silently misbehaved) deep inside _from_dense. Vector, HalfVector, Bit and the scipy path of SparseVector all raise ValueError('expected ndim to be 1') for non-1D input; this makes the dense NumPy path consistent.

Root Cause / Motivation

_from_dense does len(value) and float(v) per element:

  • SparseVector(np.zeros((2, 2))) raised TypeError: only length-1 arrays can be converted to Python scalars
  • SparseVector(np.array(5.0)) raised TypeError: len() of unsized object
  • SparseVector(np.array([[1], [0], [2]])) was silently accepted as a 3-dimensional vector (each size-1 row coerced by float()), while Vector rejects the same input

Callers that catch ValueError for bad input (e.g. the SQLAlchemy test_bad_ndim pattern) never see these cases.

Changes

File What changed
pgvector/sparsevec.py Raise ValueError('expected ndim to be 1') in _from_dense for non-1D ndarrays
tests/test_sparse_vector.py test_init_ndarray_ndim covers 2D, column, and 0-d arrays (fails before the fix)
CHANGELOG.md Entry under 0.5.2

Testing

  • Existing tests pass (123 across sparse/vector/halfvec/bit)
  • New test fails without the change, passes with it

Impact

Consistent, catchable error for bad NumPy input across all vector types; no behavior change for valid 1D input.

🤖 Generated with Claude Code

SparseVector._from_dense only checked ndim for scipy inputs. 2D arrays
raised a raw TypeError from float(), and (n, 1) column arrays were
silently accepted as n-dimensional vectors, unlike Vector, HalfVector
and Bit which raise 'expected ndim to be 1'.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant