diff --git a/python/python/lancedb/embeddings/ollama.py b/python/python/lancedb/embeddings/ollama.py index 3151c7c72..53247fda3 100644 --- a/python/python/lancedb/embeddings/ollama.py +++ b/python/python/lancedb/embeddings/ollama.py @@ -72,8 +72,23 @@ class OllamaEmbeddings(TextEmbeddingFunction): # TODO retry, rate limit, token limit if isinstance(texts, np.ndarray): texts = texts.tolist() - embeddings = self._compute_embedding(texts) - return list(embeddings) + # Ollama rejects None, so null and empty texts get no embedding + valid_indices = [ + idx + for idx, text in enumerate(texts) + if not (text is None or (isinstance(text, (str, bytes)) and not text)) + ] + embeddings: list[Union[np.array, None]] = [None] * len(texts) + if valid_indices: + computed = self._compute_embedding([texts[idx] for idx in valid_indices]) + if len(computed) != len(valid_indices): + raise ValueError( + f"Ollama returned {len(computed)} embeddings for " + f"{len(valid_indices)} texts" + ) + for idx, embedding in zip(valid_indices, computed): + embeddings[idx] = embedding + return embeddings def __getstate__(self) -> dict[str, Any]: state = super().__getstate__() diff --git a/python/python/tests/test_ollama_embeddings.py b/python/python/tests/test_ollama_embeddings.py index de36dc24b..f13f11857 100644 --- a/python/python/tests/test_ollama_embeddings.py +++ b/python/python/tests/test_ollama_embeddings.py @@ -22,15 +22,14 @@ def ollama_embeddings(): requests.append(body) if body["model"] == "missing-model": return httpx.Response(404, json={"error": "model not found"}) - return httpx.Response( - 200, - json={ - "embeddings": [ - [float(index), float(len(text))] - for index, text in enumerate(body["input"]) - ] - }, - ) + embeddings = [ + [float(index), float(len(text))] for index, text in enumerate(body["input"]) + ] + if body["model"] == "short-reply": + embeddings = embeddings[:-1] + elif body["model"] == "long-reply": + embeddings.append([0.0, 0.0]) + return httpx.Response(200, json={"embeddings": embeddings}) function = ( get_registry() @@ -83,6 +82,7 @@ def test_ollama_source_embeddings_inputs(ollama_embeddings, texts): [ pytest.param(np.array([["hello"], ["world"]]), id="numpy-two-dimensional"), pytest.param([["hello"], ["world"]], id="list-two-dimensional"), + pytest.param([[]], id="list-empty-list"), pytest.param(np.array([b"\xff"]), id="numpy-invalid-utf8"), pytest.param([b"\xff"], id="list-invalid-utf8"), ], @@ -102,8 +102,38 @@ def test_ollama_generate_embeddings_empty_array(ollama_embeddings): function, requests = ollama_embeddings assert function.generate_embeddings(np.array([], dtype=str)) == [] + assert requests == [] + + +def test_ollama_source_embeddings_skip_null_and_empty_texts(ollama_embeddings): + function, requests = ollama_embeddings + + assert function.compute_source_embeddings( + pa.array(["hello", None, "", "世界"]) + ) == [[0.0, 5.0], None, None, [1.0, 2.0]] + assert [request["input"] for request in requests] == [["hello", "世界"]] + + assert function.compute_source_embeddings([None, "", b""]) == [None, None, None] assert len(requests) == 1 - assert requests[0]["input"] == [] + + +@pytest.mark.parametrize( + "model, returned", + [ + pytest.param("short-reply", 1, id="short"), + pytest.param("long-reply", 3, id="long"), + ], +) +def test_ollama_source_embeddings_rejects_wrong_vector_count( + ollama_embeddings, model, returned +): + function, requests = ollama_embeddings + function.name = model + + with pytest.raises(ValueError, match=f"returned {returned} embeddings for 2 texts"): + function.compute_source_embeddings(["a", None, "b"]) + + assert [request["input"] for request in requests] == [["a", "b"]] def test_ollama_query_embeddings(ollama_embeddings):