mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-22 00:31:44 +00:00
test(vertex): cover model garden api base handling
This commit is contained in:
parent
18e7e0c39e
commit
90c089582b
1 changed files with 114 additions and 1 deletions
|
|
@ -1,8 +1,12 @@
|
|||
"""Vertex Model Garden: OpenAPI base URL for publisher/model ids vs per-endpoint path."""
|
||||
|
||||
import sys
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from litellm.llms.vertex_ai.vertex_model_garden.main import (
|
||||
VertexAIModelGardenModels,
|
||||
_vertex_model_garden_model_id_in_json_body,
|
||||
create_vertex_url,
|
||||
)
|
||||
|
|
@ -37,5 +41,114 @@ def test_create_vertex_url_openapi_vs_deployed_endpoint(
|
|||
|
||||
|
||||
def test_model_id_in_json_body_heuristic() -> None:
|
||||
assert _vertex_model_garden_model_id_in_json_body("xai/grok-4.1-fast-reasoning") is True
|
||||
assert (
|
||||
_vertex_model_garden_model_id_in_json_body("xai/grok-4.1-fast-reasoning")
|
||||
is True
|
||||
)
|
||||
assert _vertex_model_garden_model_id_in_json_body("5464397967697903616") is False
|
||||
|
||||
|
||||
def _mock_vertexai_module() -> MagicMock:
|
||||
mock_vertexai = MagicMock()
|
||||
mock_vertexai.preview = MagicMock()
|
||||
mock_vertexai.preview.language_models = MagicMock()
|
||||
return mock_vertexai
|
||||
|
||||
|
||||
def test_completion_preserves_custom_model_garden_api_base() -> None:
|
||||
custom_api_base = (
|
||||
"https://mg-endpoint-123.us-west1-456.prediction.vertexai.goog/v1/"
|
||||
"projects/123/locations/us-west1/endpoints/mg-endpoint-123"
|
||||
)
|
||||
mock_vertexai = _mock_vertexai_module()
|
||||
mock_openai_like_handler = MagicMock()
|
||||
mock_openai_like_handler.completion.return_value = "mock-response"
|
||||
|
||||
with (
|
||||
patch.dict(
|
||||
sys.modules,
|
||||
{"vertexai": mock_vertexai, "vertexai.preview": mock_vertexai.preview},
|
||||
),
|
||||
patch(
|
||||
"litellm.llms.vertex_ai.gemini.vertex_and_google_ai_studio_gemini."
|
||||
"VertexLLM._ensure_access_token",
|
||||
return_value=("fake-token", "resolved-project"),
|
||||
),
|
||||
patch(
|
||||
"litellm.llms.openai_like.chat.handler.OpenAILikeChatHandler",
|
||||
return_value=mock_openai_like_handler,
|
||||
),
|
||||
):
|
||||
response = VertexAIModelGardenModels().completion(
|
||||
model="gemma4",
|
||||
messages=[{"role": "user", "content": "hi"}],
|
||||
model_response=MagicMock(),
|
||||
print_verbose=MagicMock(),
|
||||
encoding=None,
|
||||
logging_obj=MagicMock(),
|
||||
api_base=custom_api_base,
|
||||
optional_params={},
|
||||
custom_prompt_dict={},
|
||||
headers=None,
|
||||
timeout=30,
|
||||
litellm_params={},
|
||||
vertex_project="configured-project",
|
||||
vertex_location="us-west1",
|
||||
)
|
||||
|
||||
assert response == "mock-response"
|
||||
mock_openai_like_handler.completion.assert_called_once()
|
||||
call_kwargs = mock_openai_like_handler.completion.call_args.kwargs
|
||||
assert call_kwargs["api_base"] == custom_api_base
|
||||
assert call_kwargs["model"] == ""
|
||||
assert call_kwargs["optional_params"]["stream"] is False
|
||||
|
||||
|
||||
def test_completion_builds_openapi_base_for_publisher_model_without_custom_api_base() -> (
|
||||
None
|
||||
):
|
||||
mock_vertexai = _mock_vertexai_module()
|
||||
mock_openai_like_handler = MagicMock()
|
||||
mock_openai_like_handler.completion.return_value = "mock-response"
|
||||
|
||||
with (
|
||||
patch.dict(
|
||||
sys.modules,
|
||||
{"vertexai": mock_vertexai, "vertexai.preview": mock_vertexai.preview},
|
||||
),
|
||||
patch(
|
||||
"litellm.llms.vertex_ai.gemini.vertex_and_google_ai_studio_gemini."
|
||||
"VertexLLM._ensure_access_token",
|
||||
return_value=("fake-token", "resolved-project"),
|
||||
),
|
||||
patch(
|
||||
"litellm.llms.openai_like.chat.handler.OpenAILikeChatHandler",
|
||||
return_value=mock_openai_like_handler,
|
||||
),
|
||||
):
|
||||
response = VertexAIModelGardenModels().completion(
|
||||
model="xai/grok-4.1-fast-reasoning",
|
||||
messages=[{"role": "user", "content": "hi"}],
|
||||
model_response=MagicMock(),
|
||||
print_verbose=MagicMock(),
|
||||
encoding=None,
|
||||
logging_obj=MagicMock(),
|
||||
api_base=None,
|
||||
optional_params={},
|
||||
custom_prompt_dict={},
|
||||
headers=None,
|
||||
timeout=30,
|
||||
litellm_params={},
|
||||
vertex_project="configured-project",
|
||||
vertex_location="us-west1",
|
||||
)
|
||||
|
||||
assert response == "mock-response"
|
||||
mock_openai_like_handler.completion.assert_called_once()
|
||||
call_kwargs = mock_openai_like_handler.completion.call_args.kwargs
|
||||
assert call_kwargs["api_base"] == (
|
||||
"https://us-west1-aiplatform.googleapis.com/v1/projects/configured-project/"
|
||||
"locations/us-west1/endpoints/openapi"
|
||||
)
|
||||
assert call_kwargs["model"] == "xai/grok-4.1-fast-reasoning"
|
||||
assert call_kwargs["optional_params"]["stream"] is False
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue