From 8285a2a7b4c87cd0199c6edc7578d39cf1bdfcf6 Mon Sep 17 00:00:00 2001 From: Julio Quinteros Pro Date: Mon, 26 Jan 2026 03:00:32 -0300 Subject: [PATCH] Fix HuggingFace embedding tests for parallel test execution The tests were making real API calls instead of using mocks because conftest.py reloads litellm at module scope, causing the HTTPHandler class reference in the HuggingFace embedding handler to become stale. The patches were applied to the new class, but the handler used the old one. Fix: Add a reload_huggingface_modules fixture that reloads the relevant modules BEFORE the mock fixtures apply their patches. This ensures all references point to the same class object. --- .../test_huggingface_embedding_handler.py | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/tests/test_litellm/llms/huggingface/embedding/test_huggingface_embedding_handler.py b/tests/test_litellm/llms/huggingface/embedding/test_huggingface_embedding_handler.py index f6bc983df0..090792d4f0 100644 --- a/tests/test_litellm/llms/huggingface/embedding/test_huggingface_embedding_handler.py +++ b/tests/test_litellm/llms/huggingface/embedding/test_huggingface_embedding_handler.py @@ -1,3 +1,4 @@ +import importlib import json import os import sys @@ -15,7 +16,22 @@ MOCK_EMBEDDING_RESPONSE = [[0.1, 0.2, 0.3, 0.4, 0.5]] @pytest.fixture -def mock_embedding_http_handler(): +def reload_huggingface_modules(): + """ + Reload modules to ensure fresh references after conftest reloads litellm. + This ensures the HTTPHandler class being patched is the same one used by + the embedding handler during parallel test execution. + """ + import litellm.llms.custom_httpx.http_handler as http_handler_module + import litellm.llms.huggingface.embedding.handler as hf_embedding_handler_module + + importlib.reload(http_handler_module) + importlib.reload(hf_embedding_handler_module) + yield + + +@pytest.fixture +def mock_embedding_http_handler(reload_huggingface_modules): """Fixture to mock the HTTP handler for embedding tests""" with patch("litellm.llms.custom_httpx.http_handler.HTTPHandler.post") as mock_post: mock_response = MagicMock() @@ -27,7 +43,7 @@ def mock_embedding_http_handler(): @pytest.fixture -def mock_embedding_async_http_handler(): +def mock_embedding_async_http_handler(reload_huggingface_modules): """Fixture to mock the async HTTP handler for embedding tests""" with patch("litellm.llms.custom_httpx.http_handler.AsyncHTTPHandler.post", new_callable=AsyncMock) as mock_post: mock_response = MagicMock()