mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-28 01:32:17 +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>
207 lines
7.1 KiB
Python
207 lines
7.1 KiB
Python
"""
|
|
Unit tests for spend_logs_metadata inclusion in Prometheus custom labels.
|
|
|
|
Verifies that metadata from x-litellm-spend-logs-metadata header is available
|
|
in Prometheus custom labels via combined_metadata.
|
|
"""
|
|
|
|
from litellm.integrations.prometheus import (
|
|
_get_combined_custom_metadata_from_standard_logging_payload,
|
|
get_custom_labels_from_metadata,
|
|
)
|
|
|
|
|
|
def test_get_custom_labels_includes_spend_logs_metadata(monkeypatch):
|
|
"""
|
|
Test that get_custom_labels_from_metadata extracts fields from
|
|
spend_logs_metadata when it is merged into combined_metadata.
|
|
"""
|
|
monkeypatch.setattr(
|
|
"litellm.custom_prometheus_metadata_labels",
|
|
["metadata.department", "metadata.env"],
|
|
)
|
|
|
|
# Simulate combined_metadata after merging all three sources
|
|
combined_metadata = {
|
|
"request_key": "from_requester", # from requester_metadata
|
|
"auth_key": "from_auth", # from user_api_key_auth_metadata
|
|
"department": "engineering", # from spend_logs_metadata
|
|
"env": "production", # from spend_logs_metadata
|
|
}
|
|
|
|
result = get_custom_labels_from_metadata(combined_metadata)
|
|
assert result == {
|
|
"metadata_department": "engineering",
|
|
"metadata_env": "production",
|
|
}
|
|
|
|
|
|
def test_spend_logs_metadata_overrides_earlier_sources(monkeypatch):
|
|
"""
|
|
Test that spend_logs_metadata values take precedence over
|
|
requester_metadata and user_api_key_auth_metadata when keys overlap,
|
|
since it is spread last in combined_metadata.
|
|
"""
|
|
monkeypatch.setattr(
|
|
"litellm.custom_prometheus_metadata_labels",
|
|
["metadata.team"],
|
|
)
|
|
|
|
# Simulate the dict spread order: requester -> auth -> spend_logs
|
|
requester_metadata = {"team": "old_team"}
|
|
user_api_key_auth_metadata = {"team": "auth_team"}
|
|
spend_logs_metadata = {"team": "spend_team"}
|
|
|
|
combined_metadata = {
|
|
**requester_metadata,
|
|
**user_api_key_auth_metadata,
|
|
**spend_logs_metadata,
|
|
}
|
|
|
|
result = get_custom_labels_from_metadata(combined_metadata)
|
|
assert result == {"metadata_team": "spend_team"}
|
|
|
|
|
|
def test_combined_metadata_with_all_three_sources(monkeypatch):
|
|
"""
|
|
Test that combined_metadata correctly merges requester_metadata,
|
|
user_api_key_auth_metadata, and spend_logs_metadata.
|
|
"""
|
|
monkeypatch.setattr(
|
|
"litellm.custom_prometheus_metadata_labels",
|
|
["metadata.from_requester", "metadata.from_auth", "metadata.from_spend"],
|
|
)
|
|
|
|
# Reproduce the exact spread pattern from prometheus.py
|
|
_requester_metadata = {"from_requester": "val1"}
|
|
user_api_key_auth_metadata = {"from_auth": "val2"}
|
|
spend_logs_metadata = {"from_spend": "val3"}
|
|
|
|
combined_metadata = {
|
|
**(_requester_metadata if _requester_metadata else {}),
|
|
**(user_api_key_auth_metadata if user_api_key_auth_metadata else {}),
|
|
**(spend_logs_metadata if spend_logs_metadata else {}),
|
|
}
|
|
|
|
result = get_custom_labels_from_metadata(combined_metadata)
|
|
assert result == {
|
|
"metadata_from_requester": "val1",
|
|
"metadata_from_auth": "val2",
|
|
"metadata_from_spend": "val3",
|
|
}
|
|
|
|
|
|
def test_combined_metadata_with_none_spend_logs(monkeypatch):
|
|
"""
|
|
Test that combined_metadata works when spend_logs_metadata is None.
|
|
"""
|
|
monkeypatch.setattr(
|
|
"litellm.custom_prometheus_metadata_labels",
|
|
["metadata.foo"],
|
|
)
|
|
|
|
_requester_metadata = {"foo": "bar"}
|
|
user_api_key_auth_metadata = None
|
|
spend_logs_metadata = None
|
|
|
|
combined_metadata = {
|
|
**(_requester_metadata if _requester_metadata else {}),
|
|
**(user_api_key_auth_metadata if user_api_key_auth_metadata else {}),
|
|
**(spend_logs_metadata if spend_logs_metadata else {}),
|
|
}
|
|
|
|
result = get_custom_labels_from_metadata(combined_metadata)
|
|
assert result == {"metadata_foo": "bar"}
|
|
|
|
|
|
def test_combined_metadata_includes_top_level_fields():
|
|
"""
|
|
Regression test for LIT-3741: user_api_key_project_alias (and other
|
|
top-level metadata fields) must be included in the combined metadata
|
|
so they can be referenced via custom_prometheus_metadata_labels.
|
|
"""
|
|
standard_logging_payload = {
|
|
"metadata": {
|
|
"user_api_key_hash": "sk-abc123",
|
|
"user_api_key_alias": "hotel-key",
|
|
"user_api_key_team_id": "team-1",
|
|
"user_api_key_team_alias": "hotel-team",
|
|
"user_api_key_project_id": "proj-1",
|
|
"user_api_key_project_alias": "hotel-recommendations",
|
|
"user_api_key_user_id": "user-1",
|
|
"user_api_key_user_email": "user@example.com",
|
|
"user_api_key_end_user_id": None,
|
|
"user_api_key_org_id": None,
|
|
"user_api_key_org_alias": None,
|
|
"user_api_key_request_route": "/v1/chat/completions",
|
|
"requester_metadata": {"custom_field": "custom_value"},
|
|
"user_api_key_auth_metadata": {"auth_field": "auth_value"},
|
|
"spend_logs_metadata": None,
|
|
}
|
|
}
|
|
|
|
combined = _get_combined_custom_metadata_from_standard_logging_payload(
|
|
standard_logging_payload
|
|
)
|
|
|
|
assert combined["user_api_key_project_alias"] == "hotel-recommendations"
|
|
assert combined["user_api_key_project_id"] == "proj-1"
|
|
assert combined["user_api_key_team_alias"] == "hotel-team"
|
|
assert combined["user_api_key_request_route"] == "/v1/chat/completions"
|
|
assert combined["custom_field"] == "custom_value"
|
|
assert combined["auth_field"] == "auth_value"
|
|
|
|
|
|
def test_project_alias_accessible_via_custom_prometheus_labels(monkeypatch):
|
|
"""
|
|
Regression test for LIT-3741: configuring
|
|
custom_prometheus_metadata_labels with "metadata.user_api_key_project_alias"
|
|
should produce a label with the project's alias value.
|
|
"""
|
|
monkeypatch.setattr(
|
|
"litellm.custom_prometheus_metadata_labels",
|
|
["metadata.user_api_key_project_alias"],
|
|
)
|
|
|
|
standard_logging_payload = {
|
|
"metadata": {
|
|
"user_api_key_project_alias": "hotel-recommendations",
|
|
"requester_metadata": None,
|
|
"user_api_key_auth_metadata": None,
|
|
"spend_logs_metadata": None,
|
|
}
|
|
}
|
|
|
|
combined = _get_combined_custom_metadata_from_standard_logging_payload(
|
|
standard_logging_payload
|
|
)
|
|
result = get_custom_labels_from_metadata(combined)
|
|
|
|
assert result == {"metadata_user_api_key_project_alias": "hotel-recommendations"}
|
|
|
|
|
|
def test_project_alias_accessible_without_prefix(monkeypatch):
|
|
"""
|
|
user_api_key_project_alias should also be accessible without
|
|
the "metadata." prefix in custom_prometheus_metadata_labels config.
|
|
"""
|
|
monkeypatch.setattr(
|
|
"litellm.custom_prometheus_metadata_labels",
|
|
["user_api_key_project_alias"],
|
|
)
|
|
|
|
standard_logging_payload = {
|
|
"metadata": {
|
|
"user_api_key_project_alias": "hotel-recommendations",
|
|
"requester_metadata": None,
|
|
"user_api_key_auth_metadata": None,
|
|
"spend_logs_metadata": None,
|
|
}
|
|
}
|
|
|
|
combined = _get_combined_custom_metadata_from_standard_logging_payload(
|
|
standard_logging_payload
|
|
)
|
|
result = get_custom_labels_from_metadata(combined)
|
|
|
|
assert result == {"user_api_key_project_alias": "hotel-recommendations"}
|