mirror of
https://github.com/BerriAI/litellm.git
synced 2026-12-02 01:28:38 +00:00
* test: move the unit half of 126 mixed legacy files into tests/unit * test: restore litellm globals that moved tests set * test: finalize migration test cleanup * test: restore original bodies of moved legacy tests The move into tests/unit had rewritten 612 test bodies, and some of the rewrites dropped assertions. Each moved test now carries its original body from the legacy file, with only the imports, helpers, fake provider credentials and monkeypatched env it needs to run under tests/unit test_timeout_streaming goes back to tests/local_testing because it needs the fake OpenAI endpoint server. The image payload fixture moves with its only user, and two tests that leaked global state (a registered model cost entry and queued logging tasks) are now isolated * test: drop module imports shadowed by restored local imports * test: assert on LiteLLM output in no-assertion moved tests and isolate leaks Twenty no-assertion candidates get one assertion on the value LiteLLM returns, with the original lines unchanged. Four tests go back to their legacy files because they only check types or imports, write into the working directory, or cannot assert without a body change Two moved tests leaked globals into later tests in the same worker, so monkeypatch fixtures now restore the retry-after header parser and the end user cost tracking flags * test: drain queued logging tasks before the Phoenix span test The moved Phoenix test counted spans from logging tasks that earlier tests had queued, so the drain fixture moves to tests/unit/conftest.py and both it and the Datadog batch test use it. test_factory_function goes back to its legacy file because its returned wrapper calls the real Assistants API and cannot be asserted on without a body change --------- Co-authored-by: yuneng <yuneng@berri.ai>
491 lines
15 KiB
Python
491 lines
15 KiB
Python
import sys, os, asyncio, time, random
|
|
from datetime import datetime
|
|
import traceback
|
|
from dotenv import load_dotenv
|
|
|
|
load_dotenv()
|
|
import copy
|
|
|
|
import pytest
|
|
from litellm import Router
|
|
from litellm.router_strategy.budget_limiter import RouterBudgetLimiting
|
|
from litellm.types.router import (
|
|
RoutingStrategy,
|
|
)
|
|
from litellm.types.utils import GenericBudgetConfigType, BudgetConfig
|
|
from litellm.caching.caching import DualCache, RedisCache
|
|
import logging
|
|
from litellm._logging import verbose_router_logger
|
|
import litellm
|
|
from datetime import timezone, timedelta
|
|
|
|
verbose_router_logger.setLevel(logging.DEBUG)
|
|
|
|
|
|
def cleanup_redis():
|
|
"""Cleanup Redis cache before each test"""
|
|
try:
|
|
import redis
|
|
|
|
print("cleaning up redis..")
|
|
|
|
redis_client = redis.Redis(
|
|
host=os.getenv("REDIS_HOST"),
|
|
port=int(os.getenv("REDIS_PORT")),
|
|
password=os.getenv("REDIS_PASSWORD"),
|
|
)
|
|
print("scan iter result", redis_client.scan_iter("provider_spend:*"))
|
|
# Delete all provider spend keys
|
|
for key in redis_client.scan_iter("provider_spend:*"):
|
|
print("deleting key", key)
|
|
redis_client.delete(key)
|
|
for key in redis_client.scan_iter("deployment_spend:*"):
|
|
print("deleting key", key)
|
|
redis_client.delete(key)
|
|
for key in redis_client.scan_iter("tag_spend:*"):
|
|
print("deleting key", key)
|
|
redis_client.delete(key)
|
|
except Exception as e:
|
|
print(f"Error cleaning up Redis: {str(e)}")
|
|
|
|
|
|
@pytest.mark.flaky(retries=6, delay=2)
|
|
@pytest.mark.asyncio
|
|
async def test_provider_budgets_e2e_test():
|
|
"""
|
|
Expected behavior:
|
|
- First request forced to OpenAI
|
|
- Hit OpenAI budget limit
|
|
- Next 3 requests all go to Azure
|
|
|
|
"""
|
|
cleanup_redis()
|
|
# Modify for test
|
|
provider_budget_config: GenericBudgetConfigType = {
|
|
"openai": BudgetConfig(time_period="1d", budget_limit=0.000000000001),
|
|
"azure": BudgetConfig(time_period="1d", budget_limit=100),
|
|
}
|
|
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo", # openai model name
|
|
"litellm_params": { # params for litellm completion/embedding call
|
|
"model": "azure/gpt-4.1-mini",
|
|
"api_key": os.getenv("AZURE_AI_API_KEY"),
|
|
"api_version": os.getenv("AZURE_API_VERSION"),
|
|
"api_base": os.getenv("AZURE_AI_API_BASE"),
|
|
},
|
|
"model_info": {"id": "azure-model-id"},
|
|
},
|
|
{
|
|
"model_name": "gpt-3.5-turbo", # openai model name
|
|
"litellm_params": {
|
|
"model": "openai/gpt-4o-mini",
|
|
},
|
|
"model_info": {"id": "openai-model-id"},
|
|
},
|
|
],
|
|
provider_budget_config=provider_budget_config,
|
|
redis_host=os.getenv("REDIS_HOST"),
|
|
redis_port=int(os.getenv("REDIS_PORT")),
|
|
redis_password=os.getenv("REDIS_PASSWORD"),
|
|
)
|
|
|
|
response = await router.acompletion(
|
|
messages=[{"role": "user", "content": "Hello, how are you?"}],
|
|
model="openai/gpt-4o-mini",
|
|
)
|
|
print(response)
|
|
|
|
await asyncio.sleep(2.5)
|
|
|
|
for _ in range(3):
|
|
response = await router.acompletion(
|
|
messages=[{"role": "user", "content": "Hello, how are you?"}],
|
|
model="gpt-3.5-turbo",
|
|
)
|
|
print(response)
|
|
|
|
print("response.hidden_params", response._hidden_params)
|
|
|
|
await asyncio.sleep(1)
|
|
|
|
assert response._hidden_params.get("custom_llm_provider") == "azure"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.flaky(retries=3, delay=2)
|
|
async def test_provider_budgets_e2e_test_expect_to_fail():
|
|
"""
|
|
Expected behavior:
|
|
- first request passes, all subsequent requests fail
|
|
|
|
"""
|
|
cleanup_redis()
|
|
|
|
# Note: We intentionally use a dictionary with string keys for budget_limit and time_period
|
|
# we want to test that the router can handle type conversion, since the proxy config yaml passes these values as a dictionary
|
|
provider_budget_config = {
|
|
"anthropic": {
|
|
"budget_limit": 0.000000000001,
|
|
"time_period": "1d",
|
|
}
|
|
}
|
|
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "anthropic/*", # openai model name
|
|
"litellm_params": {
|
|
"model": "anthropic/*",
|
|
},
|
|
},
|
|
],
|
|
redis_host=os.getenv("REDIS_HOST"),
|
|
redis_port=int(os.getenv("REDIS_PORT")),
|
|
redis_password=os.getenv("REDIS_PASSWORD"),
|
|
provider_budget_config=provider_budget_config,
|
|
)
|
|
|
|
response = await router.acompletion(
|
|
messages=[{"role": "user", "content": "Hello, how are you?"}],
|
|
model="anthropic/claude-sonnet-4-5-20250929",
|
|
)
|
|
print(response)
|
|
|
|
await asyncio.sleep(2.5)
|
|
|
|
for _ in range(3):
|
|
with pytest.raises(Exception, match="Exceeded budget for provider") as exc_info:
|
|
await router.acompletion(
|
|
messages=[{"role": "user", "content": "Hello, how are you?"}],
|
|
model="anthropic/claude-sonnet-4-5-20250929",
|
|
)
|
|
|
|
await asyncio.sleep(0.5)
|
|
# Verify the error is related to budget exceeded
|
|
|
|
assert "Exceeded budget for provider" in str(exc_info.value)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_llm_provider_for_deployment():
|
|
"""
|
|
Test the _get_llm_provider_for_deployment helper method
|
|
|
|
"""
|
|
cleanup_redis()
|
|
provider_budget = RouterBudgetLimiting(
|
|
dual_cache=DualCache(), provider_budget_config={}
|
|
)
|
|
|
|
# Test OpenAI deployment
|
|
openai_deployment = {"litellm_params": {"model": "openai/gpt-4"}}
|
|
assert (
|
|
provider_budget._get_llm_provider_for_deployment(openai_deployment) == "openai"
|
|
)
|
|
|
|
# Test Azure deployment
|
|
azure_deployment = {
|
|
"litellm_params": {
|
|
"model": "azure/gpt-4",
|
|
"api_key": "test",
|
|
"api_base": "test",
|
|
}
|
|
}
|
|
assert provider_budget._get_llm_provider_for_deployment(azure_deployment) == "azure"
|
|
|
|
# should not raise error for unknown deployment
|
|
unknown_deployment = {}
|
|
assert provider_budget._get_llm_provider_for_deployment(unknown_deployment) is None
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_handle_new_budget_window():
|
|
"""
|
|
Test _handle_new_budget_window helper method
|
|
|
|
Current
|
|
"""
|
|
cleanup_redis()
|
|
provider_budget = RouterBudgetLimiting(
|
|
dual_cache=DualCache(), provider_budget_config={}
|
|
)
|
|
|
|
spend_key = "provider_spend:openai:7d"
|
|
start_time_key = "provider_budget_start_time:openai"
|
|
current_time = 1000.0
|
|
response_cost = 0.5
|
|
ttl_seconds = 86400 # 1 day
|
|
|
|
# Test handling new budget window
|
|
new_start_time = await provider_budget._handle_new_budget_window(
|
|
spend_key=spend_key,
|
|
start_time_key=start_time_key,
|
|
current_time=current_time,
|
|
response_cost=response_cost,
|
|
ttl_seconds=ttl_seconds,
|
|
)
|
|
|
|
assert new_start_time == current_time
|
|
|
|
# Verify the spend was set correctly
|
|
spend = await provider_budget.dual_cache.async_get_cache(spend_key)
|
|
print("spend in cache for key", spend_key, "is", spend)
|
|
assert float(spend) == response_cost
|
|
|
|
# Verify start time was set correctly
|
|
start_time = await provider_budget.dual_cache.async_get_cache(start_time_key)
|
|
print("start time in cache for key", start_time_key, "is", start_time)
|
|
assert float(start_time) == current_time
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_or_set_budget_start_time():
|
|
"""
|
|
Test _get_or_set_budget_start_time helper method
|
|
|
|
scenario 1: no existing start time in cache, should return current time
|
|
scenario 2: existing start time in cache, should return existing start time
|
|
"""
|
|
cleanup_redis()
|
|
provider_budget = RouterBudgetLimiting(
|
|
dual_cache=DualCache(), provider_budget_config={}
|
|
)
|
|
|
|
start_time_key = "test_start_time"
|
|
current_time = 1000.0
|
|
ttl_seconds = 86400 # 1 day
|
|
|
|
# When there is no existing start time, we should set it to the current time
|
|
start_time = await provider_budget._get_or_set_budget_start_time(
|
|
start_time_key=start_time_key,
|
|
current_time=current_time,
|
|
ttl_seconds=ttl_seconds,
|
|
)
|
|
print("budget start time when no existing start time is in cache", start_time)
|
|
assert start_time == current_time
|
|
|
|
# When there is an existing start time, we should return it even if the current time is later
|
|
new_current_time = 2000.0
|
|
existing_start_time = await provider_budget._get_or_set_budget_start_time(
|
|
start_time_key=start_time_key,
|
|
current_time=new_current_time,
|
|
ttl_seconds=ttl_seconds,
|
|
)
|
|
print(
|
|
"budget start time when existing start time is in cache, but current time is later",
|
|
existing_start_time,
|
|
)
|
|
assert existing_start_time == current_time # Should return the original start time
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_increment_spend_in_current_window():
|
|
"""
|
|
Test _increment_spend_in_current_window helper method
|
|
|
|
Expected behavior:
|
|
- Increment the spend in memory cache
|
|
- Queue the increment operation to Redis
|
|
"""
|
|
cleanup_redis()
|
|
provider_budget = RouterBudgetLimiting(
|
|
dual_cache=DualCache(), provider_budget_config={}
|
|
)
|
|
|
|
spend_key = "provider_spend:openai:1d"
|
|
response_cost = 0.5
|
|
ttl = 86400 # 1 day
|
|
|
|
# Set initial spend
|
|
await provider_budget.dual_cache.async_set_cache(key=spend_key, value=1.0, ttl=ttl)
|
|
|
|
# Test incrementing spend
|
|
await provider_budget._increment_spend_in_current_window(
|
|
spend_key=spend_key,
|
|
response_cost=response_cost,
|
|
ttl=ttl,
|
|
)
|
|
|
|
# Verify the spend was incremented correctly in memory
|
|
spend = await provider_budget.dual_cache.async_get_cache(spend_key)
|
|
assert float(spend) == 1.5
|
|
|
|
# Verify the increment operation was queued for Redis
|
|
print(
|
|
"redis_increment_operation_queue",
|
|
provider_budget.redis_increment_operation_queue,
|
|
)
|
|
assert len(provider_budget.redis_increment_operation_queue) == 1
|
|
queued_op = provider_budget.redis_increment_operation_queue[0]
|
|
assert queued_op["key"] == spend_key
|
|
assert queued_op["increment_value"] == response_cost
|
|
assert queued_op["ttl"] == ttl
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_deployment_budget_limits_e2e_test():
|
|
"""
|
|
Expected behavior:
|
|
- First request forced to openai/gpt-4o
|
|
- Hit budget limit for openai/gpt-4o
|
|
- Next 3 requests all go to openai/gpt-4o-mini
|
|
|
|
"""
|
|
litellm.set_verbose = True
|
|
cleanup_redis()
|
|
# Modify for test
|
|
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-4o", # openai model name
|
|
"litellm_params": { # params for litellm completion/embedding call
|
|
"model": "openai/gpt-4o",
|
|
"api_key": os.getenv("OPENAI_API_KEY"),
|
|
"max_budget": 0.000000000001,
|
|
"budget_duration": "1d",
|
|
},
|
|
"model_info": {"id": "openai-gpt-4o"},
|
|
},
|
|
{
|
|
"model_name": "gpt-4o", # openai model name
|
|
"litellm_params": {
|
|
"model": "openai/gpt-4o-mini",
|
|
"api_key": os.getenv("OPENAI_API_KEY"),
|
|
"max_budget": 10,
|
|
"budget_duration": "20d",
|
|
},
|
|
"model_info": {"id": "openai-gpt-4o-mini"},
|
|
},
|
|
],
|
|
)
|
|
|
|
response = await router.acompletion(
|
|
messages=[{"role": "user", "content": "Hello, how are you?"}],
|
|
model="openai-gpt-4o",
|
|
)
|
|
print(response)
|
|
|
|
await asyncio.sleep(2.5)
|
|
|
|
for _ in range(3):
|
|
response = await router.acompletion(
|
|
messages=[{"role": "user", "content": "Hello, how are you?"}],
|
|
model="gpt-4o",
|
|
)
|
|
print(response)
|
|
await asyncio.sleep(1)
|
|
|
|
print("response.hidden_params", response._hidden_params)
|
|
assert response._hidden_params.get("model_id") == "openai-gpt-4o-mini"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_deployment_budgets_e2e_test_expect_to_fail():
|
|
"""
|
|
Expected behavior:
|
|
- first request passes, all subsequent requests fail
|
|
|
|
"""
|
|
cleanup_redis()
|
|
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "openai/gpt-4o-mini", # openai model name
|
|
"litellm_params": {
|
|
"model": "openai/gpt-4o-mini",
|
|
"max_budget": 0.000000000001,
|
|
"budget_duration": "1d",
|
|
},
|
|
},
|
|
],
|
|
redis_host=os.getenv("REDIS_HOST"),
|
|
redis_port=int(os.getenv("REDIS_PORT")),
|
|
redis_password=os.getenv("REDIS_PASSWORD"),
|
|
)
|
|
|
|
response = await router.acompletion(
|
|
messages=[{"role": "user", "content": "Hello, how are you?"}],
|
|
model="openai/gpt-4o-mini",
|
|
)
|
|
print(response)
|
|
|
|
await asyncio.sleep(2.5)
|
|
|
|
for _ in range(3):
|
|
with pytest.raises(Exception, match="Exceeded budget for deployment") as exc_info:
|
|
await router.acompletion(
|
|
messages=[{"role": "user", "content": "Hello, how are you?"}],
|
|
model="openai/gpt-4o-mini",
|
|
)
|
|
|
|
await asyncio.sleep(0.5)
|
|
# Verify the error is related to budget exceeded
|
|
|
|
assert "Exceeded budget for deployment" in str(exc_info.value)
|
|
|
|
|
|
@pytest.mark.flaky(retries=6, delay=2)
|
|
@pytest.mark.asyncio
|
|
async def test_tag_budgets_e2e_test_expect_to_fail():
|
|
"""
|
|
Expected behavior:
|
|
- first request passes, all subsequent requests fail
|
|
|
|
"""
|
|
cleanup_redis()
|
|
TAG_NAME = "product:chat-bot"
|
|
TAG_NAME_2 = "product:chat-bot-2"
|
|
litellm.tag_budget_config = {
|
|
TAG_NAME: BudgetConfig(max_budget=0.000000000001, budget_duration="1d"),
|
|
TAG_NAME_2: BudgetConfig(max_budget=100, budget_duration="1d"),
|
|
}
|
|
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "openai/gpt-4o-mini", # openai model name
|
|
"litellm_params": {
|
|
"model": "openai/gpt-4o-mini",
|
|
},
|
|
},
|
|
],
|
|
redis_host=os.getenv("REDIS_HOST"),
|
|
redis_port=int(os.getenv("REDIS_PORT")),
|
|
redis_password=os.getenv("REDIS_PASSWORD"),
|
|
)
|
|
|
|
response = await router.acompletion(
|
|
messages=[{"role": "user", "content": "Hello, how are you?"}],
|
|
model="openai/gpt-4o-mini",
|
|
metadata={"tags": [TAG_NAME]},
|
|
)
|
|
print(response)
|
|
|
|
await asyncio.sleep(2.5)
|
|
|
|
for _ in range(3):
|
|
with pytest.raises(Exception, match=f"Exceeded budget for tag='{TAG_NAME}'") as exc_info:
|
|
await router.acompletion(
|
|
messages=[{"role": "user", "content": "Hello, how are you?"}],
|
|
model="openai/gpt-4o-mini",
|
|
metadata={"tags": [TAG_NAME]},
|
|
)
|
|
|
|
await asyncio.sleep(0.5)
|
|
# Verify the error is related to budget exceeded
|
|
|
|
assert f"Exceeded budget for tag='{TAG_NAME}'" in str(exc_info.value)
|
|
|
|
# test with tag-2 expect to pass
|
|
for _ in range(2):
|
|
response = await router.acompletion(
|
|
messages=[{"role": "user", "content": "Hello, how are you?"}],
|
|
model="openai/gpt-4o-mini",
|
|
metadata={"tags": [TAG_NAME_2]},
|
|
)
|
|
print(response)
|