mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-11 03:38: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>
93 lines
3.1 KiB
Python
93 lines
3.1 KiB
Python
#### What this tests ####
|
|
# This tests setting rules before / after making llm api calls
|
|
import re
|
|
|
|
import pytest
|
|
|
|
import litellm
|
|
from litellm import acompletion, completion
|
|
|
|
def my_post_call_rule(input: str):
|
|
input = input.lower()
|
|
print(f"input: {input}")
|
|
print(f"INSIDE MY POST CALL RULE, len(input) - {len(input)}")
|
|
if len(input) < 200:
|
|
return {
|
|
"decision": False,
|
|
"message": "This violates LiteLLM Proxy Rules. Response too short",
|
|
}
|
|
return {"decision": True}
|
|
|
|
|
|
def my_post_call_rule_2(input: str):
|
|
input = input.lower()
|
|
print(f"input: {input}")
|
|
print(f"INSIDE MY POST CALL RULE, len(input) - {len(input)}")
|
|
if len(input) < 200 and len(input) > 0:
|
|
return {
|
|
"decision": False,
|
|
"message": "This violates LiteLLM Proxy Rules. Response too short",
|
|
}
|
|
return {"decision": True}
|
|
|
|
|
|
# Test 2: Post-call rule
|
|
# commenting out of ci/cd since llm's have variable output which was causing our pipeline to fail erratically.
|
|
def test_post_call_rule():
|
|
litellm.pre_call_rules = []
|
|
litellm.post_call_rules = [my_post_call_rule]
|
|
|
|
### completion
|
|
with pytest.raises(Exception, match=re.escape("This violates LiteLLM Proxy Rules. Response too short")) as exc_info:
|
|
completion(
|
|
model="gpt-3.5-turbo",
|
|
messages=[{"role": "user", "content": "say sorry"}],
|
|
max_tokens=2,
|
|
)
|
|
assert exc_info.value.message == "This violates LiteLLM Proxy Rules. Response too short"
|
|
# print(f"MAKING ACOMPLETION CALL")
|
|
# litellm.set_verbose = True
|
|
### async completion
|
|
# async def test_async_response():
|
|
# messages=[{"role": "user", "content": "say sorry"}]
|
|
# try:
|
|
# response = await acompletion(model="gpt-3.5-turbo", messages=messages)
|
|
# pytest.fail(f"acompletion call should have been failed.")
|
|
# except Exception as e:
|
|
# pass
|
|
# asyncio.run(test_async_response())
|
|
litellm.pre_call_rules = []
|
|
litellm.post_call_rules = []
|
|
|
|
|
|
# test_post_call_rule()
|
|
|
|
|
|
def test_post_call_rule_streaming():
|
|
litellm.pre_call_rules = []
|
|
litellm.post_call_rules = [my_post_call_rule_2]
|
|
### completion
|
|
response = completion(
|
|
model="gpt-3.5-turbo",
|
|
messages=[{"role": "user", "content": "say sorry"}],
|
|
max_tokens=2,
|
|
stream=True,
|
|
)
|
|
|
|
with pytest.raises(Exception, match=re.escape("This violates LiteLLM Proxy Rules. Response too short")) as exc_info:
|
|
list(response)
|
|
assert "This violates LiteLLM Proxy Rules. Response too short" in exc_info.value.message
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_post_call_processing_error_async_response():
|
|
try:
|
|
response = await acompletion(
|
|
model="command-nightly", # Just used as an example
|
|
messages=[{"content": "Hello, how are you?", "role": "user"}],
|
|
api_base="https://openai-proxy.berriai.repl.co", # Just used as an example
|
|
custom_llm_provider="openai",
|
|
)
|
|
pytest.fail("This call should have failed")
|
|
except Exception as e:
|
|
pass
|