From 6df6958dea212ffae72f1db7a286ed386a1e4e8c Mon Sep 17 00:00:00 2001 From: AryanRG Date: Fri, 16 Oct 2026 23:37:39 +0530 Subject: [PATCH] fix: validate MUVERA dimensions --- fastembed/postprocess/muvera.py | 11 +++++++++-- tests/test_postprocess.py | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/fastembed/postprocess/muvera.py b/fastembed/postprocess/muvera.py index 31d3ad36..77833262 100644 --- a/fastembed/postprocess/muvera.py +++ b/fastembed/postprocess/muvera.py @@ -126,8 +126,14 @@ def __init__( reproducible results. Defaults to 42. Raises: - ValueError: If dim_proj > dim (cannot project to higher dimensionality) + ValueError: If dim, dim_proj, or r_reps are not positive, or if dim_proj > dim """ + if dim <= 0: + raise ValueError(f"dim must be positive, got {dim}") + if dim_proj <= 0: + raise ValueError(f"dim_proj must be positive, got {dim_proj}") + if r_reps <= 0: + raise ValueError(f"r_reps must be positive, got {r_reps}") if dim_proj > dim: raise ValueError( f"Cannot project to a higher dimensionality (dim_proj={dim_proj} > dim={dim})" @@ -186,7 +192,8 @@ def from_multivector_model( Muvera: A configured MUVERA instance ready to process embeddings from the given model. Raises: - ValueError: If dim_proj > model.embedding_size (cannot project to higher dimensionality) + ValueError: If the model dimension, dim_proj, or r_reps are not positive, or if + dim_proj > model.embedding_size Example: >>> from fastembed import LateInteractionTextEmbedding diff --git a/tests/test_postprocess.py b/tests/test_postprocess.py index 92f485f1..53c0f056 100644 --- a/tests/test_postprocess.py +++ b/tests/test_postprocess.py @@ -18,6 +18,20 @@ R_REPS = 20 +@pytest.mark.parametrize( + ("kwargs", "message"), + [ + ({"dim": 0, "dim_proj": 0}, "dim must be positive"), + ({"dim": 4, "dim_proj": 0}, "dim_proj must be positive"), + ({"dim": 4, "dim_proj": 2, "r_reps": 0}, "r_reps must be positive"), + ], +) +def test_muvera_rejects_non_positive_sizes(kwargs: dict[str, int], message: str) -> None: + """Configurations that cannot produce an embedding must fail during construction.""" + with pytest.raises(ValueError, match=message): + Muvera(**kwargs) + + def test_single_input(): model = LateInteractionTextEmbedding("colbert-ir/colbertv2.0", lazy_load=True) random_generator = np.random.default_rng(42)