mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-26 01:12:21 +00:00
* ci: run the unit_selection.sh shard files on every event instead of only fork pull requests Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * ci: rename fork-flag to unit-flag now that it applies on every event * test: move tests/test_litellm root and small trees into tests/unit Pure renames, no content changes. Follow-up commits in this PR fix references, merge the three files that already existed in tests/unit, keep live-provider tests in tests/test_litellm and wire CI. * test: carry tests/test_litellm conftest isolation into tests/unit Callback lists, routing fallbacks, cached HTTP clients, logger state, AWS, proxy-URL and keychain env, and session-end client cleanup now reset for unit tests too. The environment isolation owns its MonkeyPatch so a test's own monkeypatch is undone before the model-cost teardown runs. * test: merge, split and prune the moved root and small-tree tests Merge batches/test_batch_utils.py and the chat_completions and messages dispatch tests into the files that already existed in tests/unit. Keep the live Gemini interactions tests, the async image-fetch format test and the OpenAI embedding scorer test in tests/test_litellm since they need real network or keys. Put test_router.py under tests/unit/test_router so the existing package no longer shadows it. Delete eight tests the audit found superseded by stronger ones kept in this move. * ci: run the moved root and small-tree tests under their legacy flags Add the misc and responses-caching-types flags to unit_selection.sh and CircleCI, extend enterprise-routing and mcp-integration, and point the legacy GHA shards, Makefile, redis-compat workflow, merge smoke manifest and change classifier at the new paths. * test: make the new tests/unit directories packages tests/unit/test_package_layout.py requires every directory to carry an __init__.py, and without one the moved and retained test_litellm_responses_bridge.py modules collide on import. * test: scope the unit socket block to tests/unit in shared sessions The GHA shards collect the legacy test-path and the unit selection in one pytest session. The unit conftest's loopback-only block leaked into legacy modules that reach the network at import. The legacy conftest now lifts the restriction at collect and setup time, and the unit conftest re-applies it when collecting its own modules. * test: move tests/test_litellm/llms into tests/unit/llms Rename-only. Moves the provider tests and the fine-tuning fixtures they load, mirroring the old paths. Follow-up commits merge, split and wire them. * test: merge, split and prune the moved llms tests Merges the Databricks chat transformation tests into the existing unit file, keeps the tests that need real keys or the network in tests/test_litellm, deletes the audited tests a stronger unit test already covers, and points imports at tests.unit.llms. * ci: run the moved llms tests under their legacy flags The Vertex AI and All Other Providers shards keep their legacy test-path for the retained files and add the llm-vertex-ai and llm-other-providers unit selections. CircleCI gets matching unit jobs. * test: make the tests/unit/llms directories packages Adds __init__.py to the moved dirs and drops the legacy ones whose directories no longer hold tests. * test: drop script runners and path hacks the llms split left dangling The __main__ runners in the split openai_like files and the Databricks e2e runner called tests that now live in the other half of the split or were deleted. The retained legacy halves also no longer need sys.path edits. * test: give the shard-script tests their own GITHUB_OUTPUT They only passed where the runner set it. The CircleCI unit job's env allowlist drops it, so the script's redirect failed there. * test: point the router and module-deletion checks at tests/unit router_code_coverage and code_qa_check_tests only searched tests/test_litellm, so the moved router tests no longer counted. The two silent-experiment tests the audit deleted were the only direct callers of those methods; they are replaced with tests that assert the forwarded shadow request and the recursion guard. * test: move tests/test_litellm integrations and secret_managers into tests/unit Rename-only. Mirrors the old paths, including the directory conftests and the prompt and JSON fixtures. Follow-up commits prune and wire them. * test: prune and repoint the moved integrations tests Deletes the 7 audited tests a stronger test in the same tree already covers, imports the TLS sink helpers from their new conftest path, and restores os.environ after each integrations test. Some presets write OTEL_EXPORTER_OTLP_HEADERS straight into os.environ, and without the legacy tree's test ordering that header leaked into the AgentOps tests. * ci: run the moved integrations tests under their legacy flag The integrations GHA shard and a new CircleCI job run the integrations unit selection. secret_managers joins the misc selection. * docs: point integrations and secret_managers references at tests/unit * test: make the moved integrations directories packages * test: keep the Databricks manual e2e runner and fix the SageMaker Nova run path The Databricks e2e file is a manual script whose main() calls the tests that were pruned, so pruning them broke the documented run. It is back to its main version. The SageMaker Nova docstring now points at the file's real location in tests/local_testing. * test: keep the job's UNIT_FLAG out of the shard-script tests --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
314 lines
12 KiB
Python
314 lines
12 KiB
Python
import copy
|
|
import json
|
|
from datetime import datetime
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
import litellm
|
|
from litellm.constants import MAX_S3_OBJECT_DOWNLOAD_FILENAME_BYTES, MAX_S3_OBJECT_KEY_BYTES
|
|
from litellm.integrations.s3 import S3Logger, prompts_only_payload, resolve_s3_log_prompts_only
|
|
|
|
TEST_KMS_KEY_ARN = "arn:aws:kms:us-east-1:111122223333:key/test-key-id"
|
|
TEST_MESSAGES = [{"role": "user", "content": "Reply with exactly the word PINEAPPLE."}]
|
|
TEST_RESPONSE = {"choices": [{"message": {"role": "assistant", "content": "PINEAPPLE"}}]}
|
|
|
|
|
|
def _standard_logging_payload(response_id: str = "chatcmpl-test-id") -> dict:
|
|
return {
|
|
"id": response_id,
|
|
"messages": copy.deepcopy(TEST_MESSAGES),
|
|
"response": copy.deepcopy(TEST_RESPONSE),
|
|
"metadata": {"user_api_key_team_alias": None},
|
|
}
|
|
|
|
|
|
def _log_event_kwargs(response_id: str = "chatcmpl-test-id") -> dict:
|
|
return {
|
|
"litellm_params": {"metadata": {}},
|
|
"standard_logging_object": _standard_logging_payload(response_id),
|
|
}
|
|
|
|
|
|
def _run_log_event(
|
|
callback_params: dict, response_id: str = "chatcmpl-test-id", log_kwargs: dict[str, object] | None = None
|
|
) -> MagicMock:
|
|
original = litellm.s3_callback_params
|
|
litellm.s3_callback_params = callback_params
|
|
try:
|
|
with patch("boto3.client") as mock_boto3_client:
|
|
mock_s3_client = MagicMock()
|
|
mock_boto3_client.return_value = mock_s3_client
|
|
logger = S3Logger()
|
|
logger.log_event(
|
|
kwargs=_log_event_kwargs(response_id) if log_kwargs is None else log_kwargs,
|
|
response_obj={"id": response_id},
|
|
start_time=datetime(2026, 7, 30, 12, 0, 0),
|
|
end_time=datetime(2026, 7, 30, 12, 0, 1),
|
|
print_verbose=lambda *args, **kwargs: None,
|
|
)
|
|
return mock_s3_client
|
|
finally:
|
|
litellm.s3_callback_params = original
|
|
|
|
|
|
def test_put_object_includes_sse_kms_params_when_configured():
|
|
"""
|
|
When s3_server_side_encryption and s3_sse_kms_key_id are set in
|
|
s3_callback_params, put_object must receive ServerSideEncryption and
|
|
SSEKMSKeyId so objects land encrypted with the customer-managed key.
|
|
"""
|
|
mock_s3_client = _run_log_event(
|
|
{
|
|
"s3_bucket_name": "test-bucket",
|
|
"s3_region_name": "us-east-1",
|
|
"s3_server_side_encryption": "aws:kms",
|
|
"s3_sse_kms_key_id": TEST_KMS_KEY_ARN,
|
|
}
|
|
)
|
|
|
|
put_object_kwargs = mock_s3_client.put_object.call_args.kwargs
|
|
assert put_object_kwargs["ServerSideEncryption"] == "aws:kms"
|
|
assert put_object_kwargs["SSEKMSKeyId"] == TEST_KMS_KEY_ARN
|
|
|
|
|
|
def test_put_object_supports_sse_s3_without_key_id():
|
|
"""SSE-S3 (AES256) needs only ServerSideEncryption, no key id."""
|
|
mock_s3_client = _run_log_event(
|
|
{
|
|
"s3_bucket_name": "test-bucket",
|
|
"s3_region_name": "us-east-1",
|
|
"s3_server_side_encryption": "AES256",
|
|
}
|
|
)
|
|
|
|
put_object_kwargs = mock_s3_client.put_object.call_args.kwargs
|
|
assert put_object_kwargs["ServerSideEncryption"] == "AES256"
|
|
assert "SSEKMSKeyId" not in put_object_kwargs
|
|
|
|
|
|
def test_put_object_omits_sse_params_by_default():
|
|
"""Without SSE config, put_object kwargs must stay unchanged."""
|
|
mock_s3_client = _run_log_event(
|
|
{
|
|
"s3_bucket_name": "test-bucket",
|
|
"s3_region_name": "us-east-1",
|
|
}
|
|
)
|
|
|
|
put_object_kwargs = mock_s3_client.put_object.call_args.kwargs
|
|
assert "ServerSideEncryption" not in put_object_kwargs
|
|
assert "SSEKMSKeyId" not in put_object_kwargs
|
|
|
|
|
|
def test_put_object_infers_aws_kms_when_only_key_id_set():
|
|
"""A key id without an algorithm must infer aws:kms instead of sending an invalid request."""
|
|
mock_s3_client = _run_log_event(
|
|
{
|
|
"s3_bucket_name": "test-bucket",
|
|
"s3_region_name": "us-east-1",
|
|
"s3_sse_kms_key_id": TEST_KMS_KEY_ARN,
|
|
}
|
|
)
|
|
|
|
put_object_kwargs = mock_s3_client.put_object.call_args.kwargs
|
|
assert put_object_kwargs["ServerSideEncryption"] == "aws:kms"
|
|
assert put_object_kwargs["SSEKMSKeyId"] == TEST_KMS_KEY_ARN
|
|
|
|
|
|
def test_put_object_drops_key_id_when_algorithm_is_not_kms():
|
|
"""AES256 plus a key id is invalid for S3; the key id must be dropped, not sent."""
|
|
mock_s3_client = _run_log_event(
|
|
{
|
|
"s3_bucket_name": "test-bucket",
|
|
"s3_region_name": "us-east-1",
|
|
"s3_server_side_encryption": "AES256",
|
|
"s3_sse_kms_key_id": TEST_KMS_KEY_ARN,
|
|
}
|
|
)
|
|
|
|
put_object_kwargs = mock_s3_client.put_object.call_args.kwargs
|
|
assert put_object_kwargs["ServerSideEncryption"] == "AES256"
|
|
assert "SSEKMSKeyId" not in put_object_kwargs
|
|
|
|
|
|
def test_non_string_algorithm_is_dropped_and_valid_key_id_is_rescued():
|
|
"""
|
|
A YAML boolean in s3_server_side_encryption must not crash logger init and
|
|
must not discard the valid key id; aws:kms is inferred from the key id.
|
|
"""
|
|
mock_s3_client = _run_log_event(
|
|
{
|
|
"s3_bucket_name": "test-bucket",
|
|
"s3_region_name": "us-east-1",
|
|
"s3_server_side_encryption": True,
|
|
"s3_sse_kms_key_id": TEST_KMS_KEY_ARN,
|
|
}
|
|
)
|
|
|
|
put_object_kwargs = mock_s3_client.put_object.call_args.kwargs
|
|
assert put_object_kwargs["ServerSideEncryption"] == "aws:kms"
|
|
assert put_object_kwargs["SSEKMSKeyId"] == TEST_KMS_KEY_ARN
|
|
|
|
|
|
def test_non_string_key_id_is_dropped_and_valid_algorithm_is_kept():
|
|
"""A mistyped key id (unquoted YAML number) must not disable the valid algorithm."""
|
|
mock_s3_client = _run_log_event(
|
|
{
|
|
"s3_bucket_name": "test-bucket",
|
|
"s3_region_name": "us-east-1",
|
|
"s3_server_side_encryption": "aws:kms",
|
|
"s3_sse_kms_key_id": 12345,
|
|
}
|
|
)
|
|
|
|
put_object_kwargs = mock_s3_client.put_object.call_args.kwargs
|
|
assert put_object_kwargs["ServerSideEncryption"] == "aws:kms"
|
|
assert "SSEKMSKeyId" not in put_object_kwargs
|
|
|
|
|
|
def test_put_object_key_and_filename_are_bounded_for_an_oversized_response_id():
|
|
"""The sync logger bounds both the key and the Content-Disposition filename."""
|
|
mock_s3_client = _run_log_event(
|
|
{"s3_bucket_name": "test-bucket", "s3_region_name": "us-west-2", "s3_path": "logs"},
|
|
response_id="resp_" + "A" * 1100,
|
|
)
|
|
|
|
put_object_kwargs = mock_s3_client.put_object.call_args.kwargs
|
|
assert len(put_object_kwargs["Key"].encode("utf-8")) <= MAX_S3_OBJECT_KEY_BYTES
|
|
assert put_object_kwargs["Key"].startswith("logs/2026-07-30/time-12-00-00-000000_resp_")
|
|
filename = put_object_kwargs["ContentDisposition"].removeprefix('inline; filename="').removesuffix('"')
|
|
assert len(filename.encode("utf-8")) <= MAX_S3_OBJECT_DOWNLOAD_FILENAME_BYTES
|
|
|
|
|
|
def test_put_object_keeps_the_configured_path_intact_when_only_the_id_has_to_shrink():
|
|
"""A long configured s3_path survives whole when the id can be shortened instead."""
|
|
long_path = "litellm-prod-logs/" + "t" * 921
|
|
mock_s3_client = _run_log_event(
|
|
{"s3_bucket_name": "test-bucket", "s3_region_name": "us-west-2", "s3_path": long_path},
|
|
response_id="resp_" + "B" * 100,
|
|
)
|
|
|
|
key = mock_s3_client.put_object.call_args.kwargs["Key"]
|
|
assert key.startswith(long_path + "/2026-07-30/")
|
|
assert len(key.encode("utf-8")) == MAX_S3_OBJECT_KEY_BYTES
|
|
|
|
|
|
def _uploaded_body(mock_s3_client: MagicMock) -> dict[str, object]:
|
|
return json.loads(mock_s3_client.put_object.call_args.kwargs["Body"])
|
|
|
|
|
|
def test_log_event_prompts_only_drops_response_and_keeps_messages(monkeypatch: pytest.MonkeyPatch):
|
|
monkeypatch.delenv("S3_LOG_PROMPTS_ONLY", raising=False)
|
|
log_kwargs = _log_event_kwargs()
|
|
original_payload = copy.deepcopy(log_kwargs["standard_logging_object"])
|
|
|
|
mock_s3_client = _run_log_event(
|
|
{"s3_bucket_name": "test-bucket", "s3_region_name": "us-east-1", "s3_log_prompts_only": True},
|
|
log_kwargs=log_kwargs,
|
|
)
|
|
|
|
body = _uploaded_body(mock_s3_client)
|
|
assert body["messages"] == TEST_MESSAGES
|
|
assert body["response"] is None
|
|
assert body["id"] == "chatcmpl-test-id"
|
|
assert log_kwargs["standard_logging_object"] == original_payload
|
|
|
|
|
|
def test_log_event_default_keeps_response(monkeypatch: pytest.MonkeyPatch):
|
|
monkeypatch.delenv("S3_LOG_PROMPTS_ONLY", raising=False)
|
|
|
|
mock_s3_client = _run_log_event({"s3_bucket_name": "test-bucket", "s3_region_name": "us-east-1"})
|
|
|
|
body = _uploaded_body(mock_s3_client)
|
|
assert body["response"] == TEST_RESPONSE
|
|
assert body["messages"] == TEST_MESSAGES
|
|
|
|
|
|
def test_log_event_reads_prompts_only_env_var_at_log_time(monkeypatch: pytest.MonkeyPatch):
|
|
monkeypatch.delenv("S3_LOG_PROMPTS_ONLY", raising=False)
|
|
original = litellm.s3_callback_params
|
|
litellm.s3_callback_params = {"s3_bucket_name": "test-bucket", "s3_region_name": "us-east-1"}
|
|
try:
|
|
with patch("boto3.client") as mock_boto3_client:
|
|
mock_s3_client = MagicMock()
|
|
mock_boto3_client.return_value = mock_s3_client
|
|
logger = S3Logger()
|
|
monkeypatch.setenv("S3_LOG_PROMPTS_ONLY", "true")
|
|
logger.log_event(
|
|
kwargs=_log_event_kwargs(),
|
|
response_obj={"id": "chatcmpl-test-id"},
|
|
start_time=datetime(2026, 7, 30, 12, 0, 0),
|
|
end_time=datetime(2026, 7, 30, 12, 0, 1),
|
|
print_verbose=lambda *args, **kwargs: None,
|
|
)
|
|
finally:
|
|
litellm.s3_callback_params = original
|
|
|
|
body = _uploaded_body(mock_s3_client)
|
|
assert body["response"] is None
|
|
assert body["messages"] == TEST_MESSAGES
|
|
|
|
|
|
def test_log_event_explicit_false_param_beats_env_var(monkeypatch: pytest.MonkeyPatch):
|
|
monkeypatch.setenv("S3_LOG_PROMPTS_ONLY", "true")
|
|
|
|
mock_s3_client = _run_log_event(
|
|
{"s3_bucket_name": "test-bucket", "s3_region_name": "us-east-1", "s3_log_prompts_only": False}
|
|
)
|
|
|
|
assert _uploaded_body(mock_s3_client)["response"] == TEST_RESPONSE
|
|
|
|
|
|
def test_s3_logger_init_does_not_mutate_global_callback_params(monkeypatch: pytest.MonkeyPatch):
|
|
monkeypatch.setenv("MY_S3_BUCKET", "resolved-bucket")
|
|
callback_params = {"s3_bucket_name": "os.environ/MY_S3_BUCKET", "s3_region_name": "us-east-1"}
|
|
snapshot = copy.deepcopy(callback_params)
|
|
original = litellm.s3_callback_params
|
|
litellm.s3_callback_params = callback_params
|
|
try:
|
|
with patch("boto3.client"):
|
|
logger = S3Logger()
|
|
finally:
|
|
litellm.s3_callback_params = original
|
|
|
|
assert logger.bucket_name == "resolved-bucket"
|
|
assert callback_params == snapshot
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"configured,env_value,expected",
|
|
[
|
|
(True, None, True),
|
|
(False, "true", False),
|
|
("true", None, True),
|
|
("False", "true", False),
|
|
("1", None, True),
|
|
("0", None, False),
|
|
(" yes ", None, True),
|
|
(None, None, False),
|
|
(None, "true", True),
|
|
(None, "false", False),
|
|
(None, "", False),
|
|
("", "true", False),
|
|
],
|
|
)
|
|
def test_resolve_s3_log_prompts_only(configured: object, env_value: str | None, expected: bool):
|
|
environ = {} if env_value is None else {"S3_LOG_PROMPTS_ONLY": env_value}
|
|
assert resolve_s3_log_prompts_only(configured, environ) is expected
|
|
|
|
|
|
def test_resolve_s3_log_prompts_only_unparseable_value_fails_toward_prompts_only():
|
|
assert resolve_s3_log_prompts_only("enabled", {}) is True
|
|
|
|
|
|
def test_prompts_only_payload_returns_copy_with_response_cleared():
|
|
payload = _standard_logging_payload()
|
|
snapshot = copy.deepcopy(payload)
|
|
|
|
stripped = prompts_only_payload(payload)
|
|
|
|
assert stripped["response"] is None
|
|
assert stripped["messages"] == TEST_MESSAGES
|
|
assert stripped is not payload
|
|
assert payload == snapshot
|