The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(tests): remove importlib.reload of http_handler that breaks client injection in later tests (#34336)
Mateo Wang9ab3847c0b21
diff --git a/tests/test_litellm/llms/bedrock/chat/agentcore/test_agentcore_transformation.py b/tests/test_litellm/llms/bedrock/chat/agentcore/test_agentcore_transformation.pyindex 448afd5f3a5..e5a2ea9b28f 100644--- a/tests/test_litellm/llms/bedrock/chat/agentcore/test_agentcore_transformation.py+++ b/tests/test_litellm/llms/bedrock/chat/agentcore/test_agentcore_transformation.py@@ -70,25 +70,33 @@ class TestAgentCoreAcceptHeader:"""End-to-end test: verify Accept header appears in the final HTTP requestwhen using JWT auth through litellm.completion().++ No exception swallowing: if completion() raises (for example because the+ injected client was silently ignored and a real network call was made),+ the test must fail with that error, not a misleading mock assertion."""from litellm.llms.custom_httpx.http_handler import HTTPHandlerclient = HTTPHandler()- with patch.object(client, "post", return_value=MagicMock()) as mock_post:- try:- litellm.completion(- model="bedrock/agentcore/arn:aws:bedrock-agentcore:us-west-2:888602223428:runtime/test_runtime",- messages=[{"role": "user", "content": "test"}],- api_key="test-jwt-token",- client=client,- )- except Exception:- pass+ mock_response = Mock(spec=httpx.Response)+ mock_response.status_code = 200+ mock_response.headers = {"content-type": "application/json"}+ mock_response.json.return_value = {+ "result": {"role": "assistant", "content": [{"text": "agent reply"}]}+ }++ with patch.object(client, "post", return_value=mock_response) as mock_post:+ response = litellm.completion(+ model="bedrock/agentcore/arn:aws:bedrock-agentcore:us-west-2:888602223428:runtime/test_runtime",+ messages=[{"role": "user", "content": "test"}],+ api_key="test-jwt-token",+ client=client,+ )- mock_post.assert_called_once()- headers = mock_post.call_args.kwargs["headers"]- assert "Accept" in headers- assert headers["Accept"] == "application/json, text/event-stream"+ mock_post.assert_called_once()+ headers = mock_post.call_args.kwargs["headers"]+ assert headers["Accept"] == "application/json, text/event-stream"+ assert response.choices[0].message.content == "agent reply"class TestAgentCoreJsonResponseParsing: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.pyindex af8321f24a1..c907e3249d1 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,4 +1,3 @@-import importlibimport jsonimport osimport sys@@ -16,22 +15,7 @@ MOCK_EMBEDDING_RESPONSE = [[0.1, 0.2, 0.3, 0.4, 0.5]]@pytest.fixture-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---def mock_embedding_http_handler(reload_huggingface_modules):+def mock_embedding_http_handler():"""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()@@ -43,7 +27,7 @@ def mock_embedding_http_handler(reload_huggingface_modules):@pytest.fixture-def mock_embedding_async_http_handler(reload_huggingface_modules):+def mock_embedding_async_http_handler():"""Fixture to mock the async HTTP handler for embedding tests"""with patch("litellm.llms.custom_httpx.http_handler.AsyncHTTPHandler.post",diff --git a/tests/test_litellm/llms/vertex_ai/rerank/test_vertex_ai_rerank_integration.py b/tests/test_litellm/llms/vertex_ai/rerank/test_vertex_ai_rerank_integration.pyindex 6af4cf698e2..7fea5ac0965 100644--- a/tests/test_litellm/llms/vertex_ai/rerank/test_vertex_ai_rerank_integration.py+++ b/tests/test_litellm/llms/vertex_ai/rerank/test_vertex_ai_rerank_integration.py@@ -3,26 +3,16 @@ Integration tests for Vertex AI rerank functionality.These tests demonstrate end-to-end usage of the Vertex AI rerank feature."""-import importlibfrom unittest.mock import MagicMockimport httpx+from litellm.llms.vertex_ai.rerank.transformation import VertexAIRerankConfig+class TestVertexAIRerankIntegration:def setup_method(self):- # Reload modules to ensure fresh references after conftest reloads litellm.- # This ensures the class being patched is the same one used by the tests.- import litellm.llms.vertex_ai.rerank.transformation as rerank_transformation_module-- importlib.reload(rerank_transformation_module)-- # Re-import after reload to get the fresh class- from litellm.llms.vertex_ai.rerank.transformation import (- VertexAIRerankConfig as FreshConfig,- )-- self.config = FreshConfig()+ self.config = VertexAIRerankConfig()self.model = "semantic-ranker-default@latest"def test_end_to_end_rerank_flow(self):