diff --git a/python/python/lancedb/embeddings/bedrock.py b/python/python/lancedb/embeddings/bedrock.py index 4456f943b..d089ca987 100644 --- a/python/python/lancedb/embeddings/bedrock.py +++ b/python/python/lancedb/embeddings/bedrock.py @@ -176,11 +176,15 @@ class BedRockText(_BedrockEmbeddingFunction, TextEmbeddingFunction): Returns ------- - list[list[float]] - The embeddings for the given texts + list[list[float] | None] + The embeddings for the given texts. Empty or null texts are not sent + to Bedrock (which rejects empty ``inputText``); their entry is None. """ results = [] for text in texts: + if text is None or (isinstance(text, str) and not text): + results.append(None) + continue response = self._generate_embedding(text, *args, **kwargs) results.append(response) return results diff --git a/python/python/tests/test_embeddings_bedrock.py b/python/python/tests/test_embeddings_bedrock.py index 9340c3f58..acb075266 100644 --- a/python/python/tests/test_embeddings_bedrock.py +++ b/python/python/tests/test_embeddings_bedrock.py @@ -328,3 +328,48 @@ def test_bedrock_text_cohere_v3(bedrock_runtime, mocker): {"texts": ["document"], "input_type": "search_document"}, {"texts": ["query"], "input_type": "search_query"}, ] + + +class _EmptyRejectingBedrockClient: + """Mimics bedrock-runtime, which rejects an empty ``inputText``.""" + + def __init__(self): + self.calls = [] + + def invoke_model(self, body, modelId, accept, contentType): + payload = json.loads(body) + self.calls.append(payload) + text = payload["inputText"] + if not isinstance(text, str): + raise ValueError("inputText: expected a string") + if not text: + raise ValueError("inputText: Minimum length is 1") + body = json.dumps({"embedding": [float(len(text))]}).encode() + return {"body": io.BytesIO(body)} + + +@pytest.mark.parametrize("empty", [None, ""]) +def test_empty_or_null_texts_are_skipped(empty): + func = get_registry().get("bedrock-text").create() + client = _EmptyRejectingBedrockClient() + func.__dict__["client"] = client + + result = func.compute_source_embeddings(["alpha", empty, "be"]) + + assert result == [[5.0], None, [2.0]] + assert [c["inputText"] for c in client.calls] == ["alpha", "be"] + + +def test_add_surfaces_non_string_error_with_drop(tmp_path, monkeypatch): + client = _EmptyRejectingBedrockClient() + monkeypatch.setattr(boto3.Session, "client", lambda *args, **kwargs: client) + model = get_registry().get("bedrock-text").create(max_retries=0) + + class Row(LanceModel): + text: bool = model.SourceField() + vector: Vector(model.ndims()) = model.VectorField() + + table = lancedb.connect(tmp_path).create_table("texts", schema=Row) + with pytest.raises(RuntimeError, match="expected a string"): + table.add([{"text": False}], on_bad_vectors="drop") + assert table.count_rows() == 0