diff --git a/fastembed/postprocess/muvera.py b/fastembed/postprocess/muvera.py index 3c6476795..806cd4adb 100644 --- a/fastembed/postprocess/muvera.py +++ b/fastembed/postprocess/muvera.py @@ -228,6 +228,9 @@ def process_document(self, vectors: NumpyArray) -> NumpyArray: Returns: NumpyArray: Fixed dimensional encodings of shape (r_reps * b * dim_proj,) + + Raises: + ValueError: If the document multivector is empty """ return self.process(vectors, fill_empty_clusters=True, normalize_by_count=True) @@ -243,6 +246,9 @@ def process_query(self, vectors: NumpyArray) -> NumpyArray: Returns: NumpyArray: Fixed dimensional encoding of shape (r_reps * b * dim_proj,) + + Raises: + ValueError: If the query multivector is empty """ return self.process(vectors, fill_empty_clusters=False, normalize_by_count=False) @@ -278,11 +284,15 @@ def process( Raises: AssertionError: If input vectors don't have expected dimensionality + ValueError: If the input multivector is empty """ assert ( vectors.shape[1] == self.dim ), f"Expected vectors of shape (n, {self.dim}), got {vectors.shape}" + if len(vectors) == 0: + raise ValueError("Cannot encode an empty multivector") + # Store results from each random projection output_vectors = [] diff --git a/tests/test_postprocess.py b/tests/test_postprocess.py index 82576f115..3dc80d411 100644 --- a/tests/test_postprocess.py +++ b/tests/test_postprocess.py @@ -1,4 +1,5 @@ import numpy as np +import pytest from fastembed import LateInteractionTextEmbedding from fastembed.postprocess import Muvera @@ -36,3 +37,15 @@ def test_single_input(): fde_query = muvera.process_query(multivector) assert fde_query.shape[0] == muvera.embedding_size assert np.allclose(fde_query[np.nonzero(fde_query)][:3], CANONICAL_QUERY_VALUES) + + +def test_empty_multivectors_raise_value_error(): + muvera = Muvera(dim=4, k_sim=2, dim_proj=2, r_reps=3) + empty = np.empty((0, 4)) + + with pytest.raises(ValueError, match="Cannot encode an empty multivector"): + muvera.process_document(empty) + + with pytest.raises(ValueError, match="Cannot encode an empty multivector"): + muvera.process_query(empty) +