test(backends): cover the _embed_texts fallback and empty-batch guard
#2187 fixed the default-backend ingest failure (#2190) and covered the
numpy path: an ndarray-returning EF plus a real EmbeddingCollection.upsert.
Two branches of the fixed function are still unexercised.
(The defect was not NumPy-2.x-specific. `np.float32` has never been a
`float` subclass in any NumPy major -- verified on 1.24.4, 1.26.4 and
2.4.4 -- so `list(arr)` output was always rejected. It was latent until
0e79797 added `requires_explicit_embeddings` and routed the default
Chroma backend through `_embed_texts` for the first time.)
`_embed_texts` branches on `hasattr(v, "tolist")`. The `float(x)` arm is
the one that serves embedders returning plain sequences — custom/BYO EFs,
and rows arriving as tuples. Nothing ran it, so dropping the `float()`
call there stays green and reaches users as the same production
ValueError, just on a non-default embedder.
The `if not texts: return []` guard is likewise untested. Callers pass
empty batches (a drawer set fully filtered by dedup), and loading the EF
is the expensive part — on the ONNX default it spins up a native session.
Both tests are mutation-verified against this tree:
- replacing the fallback with `list(v)` fails
test_embed_texts_handles_plain_sequence_embedders
- deleting the early return fails
test_embed_texts_short_circuits_on_empty_input
The plain-sequence EF yields `Decimal`, so the assertion proves a real
conversion rather than values passing through unchanged, and the
empty-batch test asserts by making `get_embedding_function` raise, so it
verifies the EF is never constructed rather than only checking the
return value.
Tests only — no production code changes. They live in `test_embedding.py`
for the reason #2187 documented: conftest's autouse
`_stable_embedding_function_for_tests` replaces `_embed_texts` outright
for every module outside `_REAL_EMBEDDING_TEST_MODULES`.
Refs #2190
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
co-authored by
Copilot App
parent
0ff93caf97
commit
656e0a4dee
@@ -269,3 +269,52 @@ def test_embedding_collection_upsert_accepts_numpy_backed_vectors(tmp_path, monk
|
||||
assert col.get(ids=["drawer-1"]).documents == ["verbatim drawer text"]
|
||||
finally:
|
||||
backend.close()
|
||||
|
||||
|
||||
def test_embed_texts_handles_plain_sequence_embedders(monkeypatch):
|
||||
"""The ``float(x)`` fallback must convert plain sequences, not just ndarrays.
|
||||
|
||||
``_embed_texts`` branches on ``hasattr(v, "tolist")``. The numpy side is
|
||||
covered above, but the fallback exists for embedders that hand back plain
|
||||
sequences (custom/BYO EFs, and rows that arrive as tuples), and nothing
|
||||
exercised it — so a regression there would surface only in the field, on a
|
||||
non-default embedder, as the same ChromaDB ``ValueError``.
|
||||
|
||||
Yields ``Decimal`` rather than ``float`` so the assertion proves a real
|
||||
conversion happened rather than passing values through unchanged.
|
||||
"""
|
||||
from decimal import Decimal
|
||||
|
||||
from mempalace.backends import embedding_wrapper as ew
|
||||
|
||||
class _PlainSequenceEmbeddingFunction:
|
||||
def __call__(self, input):
|
||||
return [(Decimal("0.5"), Decimal("0.25")) for _ in list(input or [])]
|
||||
|
||||
monkeypatch.setattr(
|
||||
embedding, "get_embedding_function", lambda *_, **__: _PlainSequenceEmbeddingFunction()
|
||||
)
|
||||
|
||||
vectors = ew._embed_texts(["a", "b"])
|
||||
|
||||
assert vectors == [[0.5, 0.25], [0.5, 0.25]]
|
||||
for row in vectors:
|
||||
assert isinstance(row, list)
|
||||
assert all(type(x) is float for x in row), f"got {type(row[0])}, not builtin float"
|
||||
|
||||
|
||||
def test_embed_texts_short_circuits_on_empty_input(monkeypatch):
|
||||
"""Empty input must return ``[]`` without constructing an embedding function.
|
||||
|
||||
Callers pass empty batches (a drawer set fully filtered by dedup), and
|
||||
loading the EF is the expensive part — on the ONNX default it spins up a
|
||||
native session. Guards the early return so it cannot be refactored away.
|
||||
"""
|
||||
from mempalace.backends import embedding_wrapper as ew
|
||||
|
||||
def _explode(*_, **__):
|
||||
raise AssertionError("get_embedding_function must not be called for an empty batch")
|
||||
|
||||
monkeypatch.setattr(embedding, "get_embedding_function", _explode)
|
||||
|
||||
assert ew._embed_texts([]) == []
|
||||
|
||||
Reference in New Issue
Block a user