test: attempt to avoid HF API Embedders errors, fail fast when unavoidable (#9766)

* test: better retry configurations for HF API Embedders integration tests

* shorter delay, test only on Ubunt

* try different settings

* fail fast via timeout
This commit is contained in:
Stefano Fiorucci 2025-09-05 13:15:47 +02:00 committed by GitHub
parent 329bcbb71c
commit ed8649743d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 3 deletions

View File

@ -4,6 +4,7 @@
import os
import random
import sys
from unittest.mock import MagicMock, patch
import pytest
@ -376,7 +377,8 @@ class TestHuggingFaceAPIDocumentEmbedder:
not os.environ.get("HF_API_TOKEN", None),
reason="Export an env var called HF_API_TOKEN containing the Hugging Face token to run this test.",
)
@pytest.mark.flaky(reruns=2, reruns_delay=10)
@pytest.mark.flaky(reruns=3, reruns_delay=10)
@pytest.mark.skipif(sys.platform != "linux", reason="We only test on Linux to avoid overloading the HF server")
def test_live_run_serverless(self):
docs = [
Document(content="I love cheese", meta={"topic": "Cuisine"}),
@ -389,6 +391,7 @@ class TestHuggingFaceAPIDocumentEmbedder:
meta_fields_to_embed=["topic"],
embedding_separator=" | ",
)
embedder._client.timeout = 10 # we want to fail fast if the server is not responding
result = embedder.run(documents=docs)
documents_with_embeddings = result["documents"]

View File

@ -4,6 +4,7 @@
import os
import random
import sys
from unittest.mock import MagicMock, patch
import pytest
@ -216,16 +217,18 @@ class TestHuggingFaceAPITextEmbedder:
@pytest.mark.integration
@pytest.mark.slow
@pytest.mark.flaky(reruns=2, reruns_delay=10)
@pytest.mark.flaky(reruns=3, reruns_delay=10)
@pytest.mark.skipif(
not os.environ.get("HF_API_TOKEN", None),
reason="Export an env var called HF_API_TOKEN containing the Hugging Face token to run this test.",
)
@pytest.mark.skipif(sys.platform != "linux", reason="We only test on Linux to avoid overloading the HF server")
def test_live_run_serverless(self):
embedder = HuggingFaceAPITextEmbedder(
api_type=HFEmbeddingAPIType.SERVERLESS_INFERENCE_API,
api_params={"model": "sentence-transformers/all-MiniLM-L6-v2"},
)
embedder._client.timeout = 10 # we want to fail fast if the server is not responding
result = embedder.run(text="The food was delicious")
assert len(result["embedding"]) == 384
@ -234,14 +237,16 @@ class TestHuggingFaceAPITextEmbedder:
@pytest.mark.integration
@pytest.mark.asyncio
@pytest.mark.slow
@pytest.mark.flaky(reruns=2, reruns_delay=10)
@pytest.mark.flaky(reruns=3, reruns_delay=10)
@pytest.mark.skipif(os.environ.get("HF_API_TOKEN", "") == "", reason="HF_API_TOKEN is not set")
@pytest.mark.skipif(sys.platform != "linux", reason="We only test on Linux to avoid overloading the HF server")
async def test_live_run_async_serverless(self):
model_name = "sentence-transformers/all-MiniLM-L6-v2"
embedder = HuggingFaceAPITextEmbedder(
api_type=HFEmbeddingAPIType.SERVERLESS_INFERENCE_API, api_params={"model": model_name}
)
embedder._client.timeout = 10 # we want to fail fast if the server is not responding
text = "This is a test sentence for embedding."
result = await embedder.run_async(text=text)