mirror of
https://github.com/BerriAI/litellm.git
synced 2026-08-28 05:25:59 +00:00
* test: drop the cwd-relative sys.path.insert calls from the test suite
TQ003 stands at 1,077 across 1,058 files, and 1,015 of them are the same shape:
sys.path.insert(0, os.path.abspath("../..")) and its deeper siblings. The
argument resolves against the working directory rather than the file, so from
the repo root, where every job runs pytest, it inserts the directory two levels
above the checkout. It has never pointed at litellm. The package is installed
into the environment anyway, which is what actually makes the import work, and
what the rule's message has said all along.
Removing them leaves 1,634 imports of sys and os with no remaining reference,
and those go too, except where another test module imports the name back out of
the file. The rest of TQ003 is 62 call sites that resolve against __file__ or a
variable, which are a different question and are left alone.
Collection is identical either way: 45,871 tests and the same 51 pre-existing
collection errors before and after, and ruff reports no new undefined name.
* test: drop the duplicate imports the sys.path sweep exposed to F811
* test(pre-call-utils): restore the os import the new bedrock tests need
209 lines
5.8 KiB
Python
209 lines
5.8 KiB
Python
# What is this?
|
|
## Unit test that rejected requests are also logged as failures
|
|
|
|
# What is this?
|
|
## This tests the llm guard integration
|
|
|
|
import asyncio
|
|
import random
|
|
|
|
# What is this?
|
|
## Unit test for presidio pii masking
|
|
import time
|
|
import traceback
|
|
from datetime import datetime
|
|
|
|
from dotenv import load_dotenv
|
|
|
|
load_dotenv()
|
|
|
|
from typing import Literal
|
|
|
|
import pytest
|
|
from fastapi import Request, Response
|
|
from starlette.datastructures import URL
|
|
|
|
import litellm
|
|
from litellm import Router, mock_completion
|
|
from litellm.caching.caching import DualCache
|
|
from litellm.integrations.custom_logger import CustomLogger
|
|
from litellm.proxy._types import UserAPIKeyAuth
|
|
from litellm_enterprise.enterprise_callbacks.secret_detection import (
|
|
_ENTERPRISE_SecretDetection,
|
|
)
|
|
from litellm.proxy.proxy_server import (
|
|
Depends,
|
|
HTTPException,
|
|
chat_completion,
|
|
completion,
|
|
embeddings,
|
|
)
|
|
from litellm.proxy.utils import ProxyLogging, hash_token
|
|
|
|
|
|
class testLogger(CustomLogger):
|
|
|
|
def __init__(self):
|
|
self.reaches_sync_failure_event = False
|
|
self.reaches_async_failure_event = False
|
|
|
|
async def async_pre_call_hook(
|
|
self,
|
|
user_api_key_dict: UserAPIKeyAuth,
|
|
cache: DualCache,
|
|
data: dict,
|
|
call_type: Literal[
|
|
"completion",
|
|
"text_completion",
|
|
"embeddings",
|
|
"image_generation",
|
|
"moderation",
|
|
"audio_transcription",
|
|
"pass_through_endpoint",
|
|
"rerank",
|
|
],
|
|
):
|
|
raise HTTPException(
|
|
status_code=429, detail={"error": "Max parallel request limit reached"}
|
|
)
|
|
|
|
async def async_log_failure_event(self, kwargs, response_obj, start_time, end_time):
|
|
self.reaches_async_failure_event = True
|
|
|
|
def log_failure_event(self, kwargs, response_obj, start_time, end_time):
|
|
self.reaches_sync_failure_event = True
|
|
|
|
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "fake-model",
|
|
"litellm_params": {
|
|
"model": "openai/fake",
|
|
"api_base": "https://exampleopenaiendpoint-production.up.railway.app/",
|
|
"api_key": "sk-12345",
|
|
},
|
|
}
|
|
]
|
|
)
|
|
|
|
|
|
def _register_proxy_test_logger(callback_logger: testLogger) -> None:
|
|
"""
|
|
Register the test logger on global callback lists.
|
|
|
|
``function_setup`` dedupes by object identity; each parametrized case
|
|
constructs a new ``testLogger`` and must replace the global lists, not
|
|
only ``litellm.callbacks``.
|
|
"""
|
|
litellm.callbacks = [callback_logger]
|
|
litellm.success_callback = [callback_logger]
|
|
litellm.failure_callback = [callback_logger]
|
|
litellm._async_success_callback = [callback_logger]
|
|
litellm._async_failure_callback = [callback_logger]
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"route, body",
|
|
[
|
|
(
|
|
"/v1/chat/completions",
|
|
{
|
|
"model": "fake-model",
|
|
"messages": [
|
|
{
|
|
"role": "user",
|
|
"content": "Hello here is my OPENAI_API_KEY = sk-12345",
|
|
}
|
|
],
|
|
},
|
|
),
|
|
("/v1/completions", {"model": "fake-model", "prompt": "ping"}),
|
|
(
|
|
"/v1/embeddings",
|
|
{
|
|
"input": "The food was delicious and the waiter...",
|
|
"model": "fake-model",
|
|
"encoding_format": "float",
|
|
},
|
|
),
|
|
],
|
|
)
|
|
@pytest.mark.asyncio
|
|
async def test_chat_completion_request_with_redaction(route, body):
|
|
"""
|
|
IMPORTANT Enterprise Test - Do not delete it:
|
|
Makes a /chat/completions request on LiteLLM Proxy
|
|
|
|
Ensures that the secret is redacted EVEN on the callback
|
|
"""
|
|
from litellm.proxy import proxy_server
|
|
|
|
setattr(proxy_server, "llm_router", router)
|
|
_test_logger = testLogger()
|
|
_register_proxy_test_logger(_test_logger)
|
|
litellm.set_verbose = True
|
|
|
|
# Prepare the query string
|
|
query_params = "param1=value1¶m2=value2"
|
|
|
|
# Create the Request object with query parameters
|
|
request = Request(
|
|
scope={
|
|
"type": "http",
|
|
"method": "POST",
|
|
"headers": [(b"content-type", b"application/json")],
|
|
"query_string": query_params.encode(),
|
|
}
|
|
)
|
|
|
|
request._url = URL(url=route)
|
|
|
|
async def return_body():
|
|
import json
|
|
|
|
return json.dumps(body).encode()
|
|
|
|
request.body = return_body
|
|
|
|
try:
|
|
if route == "/v1/chat/completions":
|
|
response = await chat_completion(
|
|
request=request,
|
|
user_api_key_dict=UserAPIKeyAuth(
|
|
api_key="sk-12345",
|
|
token="hashed_sk-12345",
|
|
rpm_limit=0,
|
|
request_route=route,
|
|
),
|
|
fastapi_response=Response(),
|
|
)
|
|
elif route == "/v1/completions":
|
|
response = await completion(
|
|
request=request,
|
|
user_api_key_dict=UserAPIKeyAuth(
|
|
api_key="sk-12345",
|
|
token="hashed_sk-12345",
|
|
rpm_limit=0,
|
|
request_route=route,
|
|
),
|
|
fastapi_response=Response(),
|
|
)
|
|
elif route == "/v1/embeddings":
|
|
response = await embeddings(
|
|
request=request,
|
|
user_api_key_dict=UserAPIKeyAuth(
|
|
api_key="sk-12345",
|
|
token="hashed_sk-12345",
|
|
rpm_limit=0,
|
|
request_route=route,
|
|
),
|
|
fastapi_response=Response(),
|
|
)
|
|
except Exception:
|
|
pass
|
|
await asyncio.sleep(3)
|
|
|
|
assert _test_logger.reaches_async_failure_event is True
|
|
|
|
assert _test_logger.reaches_sync_failure_event is True
|