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): use class-level AsyncHTTPHandler mock in vertex GPT-OSS tests
Julio Quinteros Prof0fc44c24468
diff --git a/tests/test_litellm/llms/vertex_ai/vertex_ai_partner_models/gpt_oss/test_vertex_ai_gpt_oss_transformation.py b/tests/test_litellm/llms/vertex_ai/vertex_ai_partner_models/gpt_oss/test_vertex_ai_gpt_oss_transformation.pyindex 71069b87509..fb5529d4752 100644--- a/tests/test_litellm/llms/vertex_ai/vertex_ai_partner_models/gpt_oss/test_vertex_ai_gpt_oss_transformation.py+++ b/tests/test_litellm/llms/vertex_ai/vertex_ai_partner_models/gpt_oss/test_vertex_ai_gpt_oss_transformation.py@@ -1,9 +1,8 @@import jsonimport osimport sys-from unittest.mock import MagicMock, patch+from unittest.mock import AsyncMock, MagicMock, patch-import httpximport pytestsys.path.insert(@@ -76,11 +75,6 @@ async def test_vertex_ai_gpt_oss_simple_request():Test that a simple request to vertex_ai/openai/gpt-oss-20b-maas lands at the correct URLwith the correct request body."""- from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler- from litellm.llms.vertex_ai.gemini.vertex_and_google_ai_studio_gemini import (- VertexLLM,- )-# Mock responsemock_response = MagicMock()mock_response.status_code = 200@@ -107,21 +101,17 @@ async def test_vertex_ai_gpt_oss_simple_request():}}- client = AsyncHTTPHandler()-- async def mock_post_func(*args, **kwargs):- return mock_response-- # Mock vertexai module to prevent import from triggering authenticationmock_vertexai = MagicMock()mock_vertexai.preview = MagicMock()mock_vertexai.preview.language_models = MagicMock()- with patch.object(client, "post", side_effect=mock_post_func) as mock_post, \- patch.object(VertexLLM, "_ensure_access_token", return_value=("fake-token", "pathrise-convert-1606954137718")), \- patch('litellm.llms.vertex_ai.gemini.vertex_and_google_ai_studio_gemini.VertexAIError', Exception), \- patch.dict('sys.modules', {'vertexai': mock_vertexai, 'vertexai.preview': mock_vertexai.preview}), \+ with patch("litellm.llms.custom_httpx.http_handler.AsyncHTTPHandler") as mock_http_handler, \+ patch("litellm.llms.vertex_ai.gemini.vertex_and_google_ai_studio_gemini.VertexLLM._ensure_access_token",+ return_value=("fake-token", "pathrise-convert-1606954137718")), \+ patch.dict("sys.modules", {"vertexai": mock_vertexai, "vertexai.preview": mock_vertexai.preview}), \patch.dict(os.environ, {"VERTEXAI_PROJECT": "pathrise-convert-1606954137718"}):+ mock_http_handler.return_value.post = AsyncMock(return_value=mock_response)+response = await litellm.acompletion(model="vertex_ai/openai/gpt-oss-20b-maas",messages=[@@ -136,22 +126,20 @@ async def test_vertex_ai_gpt_oss_simple_request():],vertex_ai_location="us-central1",vertex_ai_project="pathrise-convert-1606954137718",- client=client)-+# Verify the mock was called- mock_post.assert_called_once()-+ mock_http_handler.return_value.post.assert_called_once()+# Get the call arguments- call_args = mock_post.call_args- # For side_effect, the URL is passed as kwargs['url']+ call_args = mock_http_handler.return_value.post.call_argscalled_url = call_args.kwargs["url"]request_body = json.loads(call_args.kwargs["data"])-+# Verify the URLexpected_url = "https://us-central1-aiplatform.googleapis.com/v1/projects/pathrise-convert-1606954137718/locations/us-central1/endpoints/openapi/chat/completions"assert called_url == expected_url-+# Verify the request bodyexpected_request_body = {'model': 'openai/gpt-oss-20b-maas',@@ -168,7 +156,7 @@ async def test_vertex_ai_gpt_oss_simple_request():'stream': False}assert request_body == expected_request_body-+# Verify response structureassert response.model == "openai/gpt-oss-20b-maas"assert len(response.choices) == 1@@ -181,11 +169,6 @@ async def test_vertex_ai_gpt_oss_reasoning_effort():Test that reasoning_effort parameter is correctly passed in the request bodyfor GPT-OSS models."""- from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler- from litellm.llms.vertex_ai.gemini.vertex_and_google_ai_studio_gemini import (- VertexLLM,- )-# Mock responsemock_response = MagicMock()mock_response.status_code = 200@@ -212,21 +195,17 @@ async def test_vertex_ai_gpt_oss_reasoning_effort():}}- client = AsyncHTTPHandler()-- async def mock_post_func(*args, **kwargs):- return mock_response-- # Mock vertexai module to prevent import from triggering authenticationmock_vertexai = MagicMock()mock_vertexai.preview = MagicMock()mock_vertexai.preview.language_models = MagicMock()- with patch.object(client, "post", side_effect=mock_post_func) as mock_post, \- patch.object(VertexLLM, "_ensure_access_token", return_value=("fake-token", "pathrise-convert-1606954137718")), \- patch('litellm.llms.vertex_ai.gemini.vertex_and_google_ai_studio_gemini.VertexAIError', Exception), \- patch.dict('sys.modules', {'vertexai': mock_vertexai, 'vertexai.preview': mock_vertexai.preview}), \+ with patch("litellm.llms.custom_httpx.http_handler.AsyncHTTPHandler") as mock_http_handler, \+ patch("litellm.llms.vertex_ai.gemini.vertex_and_google_ai_studio_gemini.VertexLLM._ensure_access_token",+ return_value=("fake-token", "pathrise-convert-1606954137718")), \+ patch.dict("sys.modules", {"vertexai": mock_vertexai, "vertexai.preview": mock_vertexai.preview}), \patch.dict(os.environ, {"VERTEXAI_PROJECT": "pathrise-convert-1606954137718"}):+ mock_http_handler.return_value.post = AsyncMock(return_value=mock_response)+response = await litellm.acompletion(model="vertex_ai/openai/gpt-oss-20b-maas",messages=[@@ -242,20 +221,19 @@ async def test_vertex_ai_gpt_oss_reasoning_effort():reasoning_effort="low",vertex_ai_location="us-central1",vertex_ai_project="pathrise-convert-1606954137718",- client=client)-+# Verify the mock was called- mock_post.assert_called_once()-- # Get the call arguments- call_args = mock_post.call_args+ mock_http_handler.return_value.post.assert_called_once()++ # Get the call arguments+ call_args = mock_http_handler.return_value.post.call_argsrequest_body = json.loads(call_args.kwargs["data"])-+# Verify reasoning_effort is in the request bodyassert "reasoning_effort" in request_bodyassert request_body["reasoning_effort"] == "low"-+# Verify other expected fieldsexpected_request_body = {'model': 'openai/gpt-oss-20b-maas',@@ -273,7 +251,7 @@ async def test_vertex_ai_gpt_oss_reasoning_effort():'stream': False}assert request_body == expected_request_body-+# Verify response structureassert response.model == "openai/gpt-oss-20b-maas"assert len(response.choices) == 1