test(unit): migrate wave 1 phase 3 anthropic, apiserpent, azure and azure_ai legacy tests

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
yuneng 2026-09-20 08:19:05 +00:00
parent 6ef7b86748
commit fcabb626ac
22 changed files with 11 additions and 132 deletions

View file

@ -2,6 +2,8 @@ from collections.abc import Iterator
from typing import Final
import pytest
import litellm
from pytest_socket import enable_socket, socket_allow_hosts
LOOPBACK_HOSTS: Final = ["127.0.0.1", "::1"]
@ -21,3 +23,12 @@ def block_external_sockets() -> Iterator[None]:
@pytest.hookimpl(trylast=True)
def pytest_runtest_setup() -> None:
_allow_loopback_only()
@pytest.fixture
def local_model_cost_map(monkeypatch: pytest.MonkeyPatch) -> Iterator[None]:
monkeypatch.setenv("LITELLM_LOCAL_MODEL_COST_MAP", "True")
monkeypatch.setattr(litellm, "model_cost", litellm.get_model_cost_map(url=""))
litellm.get_model_info.cache_clear()
yield
litellm.get_model_info.cache_clear()

View file

@ -133,88 +133,6 @@ def test_azure_image_generation_flattens_extra_body():
assert data["size"] == "1024x1024"
def test_azure_image_generation_creates_token_provider_from_credentials():
"""
Test that azure_ad_token_provider is created from tenant_id, client_id, client_secret.
This test verifies the fix in images/main.py where we now create the
azure_ad_token_provider from credentials in litellm_params if it's not already provided.
"""
# Simulate the fix in images/main.py
litellm_params_dict = {
"tenant_id": "test-tenant-id",
"client_id": "test-client-id",
"client_secret": "test-client-secret",
"azure_scope": None,
}
azure_ad_token_provider = None
# This is the logic we added in images/main.py
if azure_ad_token_provider is None:
tenant_id = litellm_params_dict.get("tenant_id")
client_id = litellm_params_dict.get("client_id")
client_secret = litellm_params_dict.get("client_secret")
azure_scope = (
litellm_params_dict.get("azure_scope")
or "https://cognitiveservices.azure.com/.default"
)
# Verify the credentials are extracted correctly
assert tenant_id == "test-tenant-id"
assert client_id == "test-client-id"
assert client_secret == "test-client-secret"
assert azure_scope == "https://cognitiveservices.azure.com/.default"
# Verify the condition to create token provider is met
assert (
tenant_id and client_id and client_secret
), "Credentials should be present to create token provider"
def test_azure_image_generation_headers_without_api_key():
"""
Test that when api_key is None, the api-key header is not added to headers.
This prevents the httpx TypeError: "Header value must be str or bytes, not <class 'NoneType'>"
that was occurring when api_key was None and being set in headers.
This is a unit test for the fix in images/main.py where we now check:
if api_key is not None:
default_headers["api-key"] = api_key
"""
from litellm.images.main import image_generation
# Test the header building logic directly
api_key = None
default_headers = {
"Content-Type": "application/json",
}
# This is the fix: only add api-key if it's not None
if api_key is not None:
default_headers["api-key"] = api_key
# Verify api-key is not in headers when api_key is None
assert "api-key" not in default_headers
# Verify Content-Type is still there
assert default_headers["Content-Type"] == "application/json"
# Test with a valid api_key
api_key = "valid-key-123"
default_headers_with_key = {
"Content-Type": "application/json",
}
if api_key is not None:
default_headers_with_key["api-key"] = api_key
# Verify api-key is added when api_key is valid
assert "api-key" in default_headers_with_key
assert default_headers_with_key["api-key"] == "valid-key-123"
def test_azure_image_generation_drop_params_response_format():
"""
Test that unsupported params like response_format are dropped when drop_params=True.

View file

@ -426,41 +426,6 @@ async def test_async_realtime_beta_without_api_version_raises():
)
@pytest.mark.asyncio
async def test_realtime_protocol_env_var_fallback():
"""
Test that LITELLM_AZURE_REALTIME_PROTOCOL env var is used as fallback.
Fixes #22127: no way to set realtime_protocol from config.
"""
from litellm.realtime_api.main import _arealtime
from litellm.types.router import GenericLiteLLMParams
with patch.dict(os.environ, {"LITELLM_AZURE_REALTIME_PROTOCOL": "v1"}):
# Create a GenericLiteLLMParams without realtime_protocol
litellm_params = GenericLiteLLMParams()
# The env var should be picked up as fallback
realtime_protocol = (
{}.get("realtime_protocol")
or litellm_params.get("realtime_protocol")
or os.environ.get("LITELLM_AZURE_REALTIME_PROTOCOL")
or "beta"
)
assert realtime_protocol == "v1"
@pytest.mark.asyncio
async def test_realtime_protocol_from_litellm_params():
"""
Test that realtime_protocol is read from litellm_params (config.yaml extra field).
Fixes #22127: realtime_protocol in litellm_params was not used.
"""
from litellm.types.router import GenericLiteLLMParams
# Simulate config.yaml with realtime_protocol as an extra field
litellm_params = GenericLiteLLMParams(realtime_protocol="GA")
assert litellm_params.get("realtime_protocol") == "GA"
@pytest.mark.asyncio
async def test_arealtime_transcription_intent_defaults_to_ga(monkeypatch):
"""

View file

@ -352,21 +352,6 @@ def test_azure_model_router_stamps_selected_model_on_hidden_params():
)
def test_azure_model_router_stamp_does_not_leak_across_responses():
"""
ModelResponse declares _hidden_params as a class-level dict, so the stamp has to be written
as a fresh dict. Mutating in place would bleed the selected model into unrelated responses.
"""
from litellm.llms.azure_ai.common_utils import (
AZURE_MODEL_ROUTER_SELECTED_MODEL_KEY,
)
from litellm.types.utils import ModelResponse
untouched = ModelResponse()
assert AZURE_MODEL_ROUTER_SELECTED_MODEL_KEY not in (untouched._hidden_params or {})
def test_drop_tool_level_extra_fields_strips_copilot_mcp_server_name():
"""
Regression test: Azure AI returns 400 when tools contain copilot_mcp_server_name.