litellm/tests/test_litellm/rerank_api/test_main.py
yuneng-jiang 6a0d03914c
test: drop the cwd-relative sys.path.insert calls from the test suite (#37802)
* 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
2026-08-22 09:25:58 -07:00

64 lines
2.1 KiB
Python

import logging
from unittest.mock import MagicMock, patch
import litellm
MARKER_QUERY = "MARKER_QUERY_do_not_log_at_info"
MARKER_DOC = "MARKER_DOC_sensitive_customer_text"
def _mock_cohere_response() -> MagicMock:
mock_response = MagicMock()
def return_val():
return {
"id": "cmpl-mockid",
"results": [{"index": 0, "relevance_score": 0.95}],
"meta": {
"api_version": {"version": "1.0"},
"billed_units": {"search_units": 1},
},
}
mock_response.json = return_val
mock_response.headers = {"key": "value"}
mock_response.status_code = 200
return mock_response
def test_rerank_does_not_log_request_content_at_info(caplog):
"""Regression for #32525: rerank must not emit query/documents to logs at INFO.
The mapped ``optional_rerank_params`` (which always contains ``query`` and
``documents``) bypasses ``turn_off_message_logging`` / ``redact_messages``,
so logging it at INFO leaks raw request content into stdout and any log sink.
"""
litellm.cohere_key = "test_api_key"
caplog.set_level(logging.DEBUG, logger="LiteLLM")
with patch(
"litellm.llms.custom_httpx.http_handler.HTTPHandler.post",
return_value=_mock_cohere_response(),
):
litellm.rerank(
model="cohere/rerank-english-v3.0",
query=MARKER_QUERY,
documents=[MARKER_DOC, "unrelated"],
top_n=2,
)
litellm_records = [r for r in caplog.records if r.name == "LiteLLM"]
info_or_above = [
r.getMessage()
for r in litellm_records
if r.levelno >= logging.INFO and (MARKER_QUERY in r.getMessage() or MARKER_DOC in r.getMessage())
]
assert not info_or_above, f"rerank leaked request content at INFO+: {info_or_above}"
optional_params_logs = [r for r in litellm_records if "optional_rerank_params" in r.getMessage()]
assert optional_params_logs, "expected the optional_rerank_params line to be logged"
assert all(
r.levelno == logging.DEBUG for r in optional_params_logs
), "optional_rerank_params must be logged at DEBUG, not INFO"