mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-18 09:45:23 +00:00
fix: redact positional credentials and malformed request headers
This commit is contained in:
parent
f248535545
commit
278dc554cc
5 changed files with 457 additions and 9 deletions
|
|
@ -1,7 +1,7 @@
|
|||
from collections.abc import ItemsView, Mapping, Sequence
|
||||
from collections.abc import Set as AbstractSet
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any, Final
|
||||
from typing import Any, Final, cast
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
|
@ -255,6 +255,50 @@ def mask_sensitive_keys(data: Mapping[str, object], sensitive_fields: set[str])
|
|||
return masked
|
||||
|
||||
|
||||
_REQUEST_LOG_HEADER_NAMES: Final = frozenset(
|
||||
(
|
||||
"accept",
|
||||
"accept-encoding",
|
||||
"authorization",
|
||||
"proxy-authorization",
|
||||
"cookie",
|
||||
"set-cookie",
|
||||
"api-key",
|
||||
"x-api-key",
|
||||
"x-goog-api-key",
|
||||
"content-type",
|
||||
"content-length",
|
||||
"user-agent",
|
||||
"idempotency-key",
|
||||
"x-request-id",
|
||||
"x-correlation-id",
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def _redact_request_headers(value: object) -> object:
|
||||
if value is None:
|
||||
return None
|
||||
if not isinstance(value, Mapping):
|
||||
return REDACTED
|
||||
headers: Final = cast(Mapping[object, object], value) # cast-ok: Mapping checked above, keys remain untrusted
|
||||
if any(
|
||||
not isinstance(name, str) or name.lower() not in _REQUEST_LOG_HEADER_NAMES or not isinstance(item, str)
|
||||
for name, item in headers.items()
|
||||
):
|
||||
return REDACTED
|
||||
return headers
|
||||
|
||||
|
||||
def redact_credentials_in_request(data: Mapping[str, object]) -> Mapping[str, object]:
|
||||
return redact_credentials_in_payload(
|
||||
{
|
||||
key: _redact_request_headers(value) if key in ("headers", "extra_headers") else value
|
||||
for key, value in data.items()
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def redact_credentials_in_payload(data: Mapping[str, object]) -> Mapping[str, object]:
|
||||
"""Return a copy of ``data`` where every value under a credential-named key is
|
||||
replaced by the shared ``REDACTED`` marker, nested mappings are recursed into,
|
||||
|
|
|
|||
|
|
@ -94,7 +94,11 @@ from litellm.litellm_core_utils.fallback_generalizations import (
|
|||
from litellm.litellm_core_utils.get_model_cost_map import AliasedCostEntry
|
||||
from litellm.litellm_core_utils.model_param_helper import peek_reasoning_summary_aliases
|
||||
from litellm.litellm_core_utils.offline_mode import is_offline_mode
|
||||
from litellm.litellm_core_utils.sensitive_data_masker import redact_credentials_in_payload
|
||||
from litellm.litellm_core_utils.secret_redaction import REDACTED
|
||||
from litellm.litellm_core_utils.sensitive_data_masker import (
|
||||
redact_credentials_in_payload,
|
||||
redact_credentials_in_request,
|
||||
)
|
||||
from litellm.litellm_core_utils.tokenizer import strip_special_tokens
|
||||
from litellm.litellm_core_utils.tool_fields import MalformedField, resolve_deployment_tool_fields
|
||||
from litellm.rust_bridge import tokenizer as tokenizer_dispatch
|
||||
|
|
@ -8537,7 +8541,38 @@ def get_valid_models(
|
|||
return [] # NON-Blocking
|
||||
|
||||
|
||||
def print_args_passed_to_litellm(original_function, args, kwargs):
|
||||
def _request_logging_parameters(original_function: Callable[..., object]) -> tuple[str, ...]:
|
||||
parameters: Final = inspect.signature(original_function).parameters.values()
|
||||
positional: Final = tuple(
|
||||
parameter.name
|
||||
for parameter in parameters
|
||||
if parameter.kind in (inspect.Parameter.POSITIONAL_ONLY, inspect.Parameter.POSITIONAL_OR_KEYWORD)
|
||||
)
|
||||
if positional or not inspect.iscoroutinefunction(original_function):
|
||||
return positional
|
||||
if original_function.__name__.startswith("a"):
|
||||
synchronous: Final = getattr(inspect.getmodule(original_function), original_function.__name__[1:], None)
|
||||
if callable(synchronous) and not inspect.iscoroutinefunction(synchronous):
|
||||
return _request_logging_parameters(synchronous)
|
||||
return ()
|
||||
|
||||
|
||||
def _redact_positional_request_args(
|
||||
original_function: Callable[..., object], args: tuple[object, ...]
|
||||
) -> tuple[object, ...]:
|
||||
if not args:
|
||||
return ()
|
||||
try:
|
||||
names: Final = _request_logging_parameters(original_function)
|
||||
except (TypeError, ValueError):
|
||||
return (REDACTED,) * len(args)
|
||||
redacted: Final = redact_credentials_in_request(dict(zip(names, args)))
|
||||
return tuple(redacted[name] for name in names[: len(args)]) + (REDACTED,) * max(0, len(args) - len(names))
|
||||
|
||||
|
||||
def print_args_passed_to_litellm(
|
||||
original_function: Callable[..., object], args: tuple[object, ...], kwargs: Mapping[str, object]
|
||||
) -> None:
|
||||
if not is_debugging_on():
|
||||
return
|
||||
try:
|
||||
|
|
@ -8557,8 +8592,8 @@ def print_args_passed_to_litellm(original_function, args, kwargs):
|
|||
):
|
||||
return
|
||||
|
||||
args_str: Final = ", ".join(map(repr, args))
|
||||
redacted_kwargs: Final = redact_credentials_in_payload(kwargs)
|
||||
args_str: Final = ", ".join(map(repr, _redact_positional_request_args(original_function, args)))
|
||||
redacted_kwargs: Final = redact_credentials_in_request(kwargs)
|
||||
kwargs_str: Final = ", ".join(f"{key}={value!r}" for key, value in redacted_kwargs.items())
|
||||
print_verbose(
|
||||
"\n",
|
||||
|
|
|
|||
|
|
@ -3,13 +3,96 @@ Unit tests for SensitiveDataMasker - List Preservation
|
|||
"""
|
||||
|
||||
from functools import reduce
|
||||
from types import MappingProxyType
|
||||
from typing import Final
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
from litellm.litellm_core_utils.sensitive_data_masker import SensitiveDataMasker
|
||||
|
||||
|
||||
@pytest.mark.parametrize("field", ["headers", "extra_headers"])
|
||||
@pytest.mark.parametrize(
|
||||
"malformed",
|
||||
[
|
||||
"synthetic-header-secret",
|
||||
b"synthetic-header-secret",
|
||||
[("Authorization", "synthetic-header-secret")],
|
||||
{b"synthetic-header-secret": "unused"},
|
||||
{123: "synthetic-header-secret"},
|
||||
{"Bearer synthetic-header-secret": "unused"},
|
||||
{"synthetic-header-secret\r\n": "unused"},
|
||||
{"": "synthetic-header-secret"},
|
||||
{"\u2603": "synthetic-header-secret"},
|
||||
{"SYNTHETIC_KEY": "Bearer synthetic-header-secret"},
|
||||
{"q8m4p9x2": "synthetic-header-secret"},
|
||||
{"X-Request-Id": b"synthetic-header-secret"},
|
||||
{"X-Request-Id": {"value": "synthetic-header-secret"}},
|
||||
{"X-Request-Id": ["synthetic-header-secret"]},
|
||||
],
|
||||
)
|
||||
def test_request_redaction_hides_malformed_headers_only(field: str, malformed: object) -> None:
|
||||
from litellm.litellm_core_utils.sensitive_data_masker import redact_credentials_in_request
|
||||
|
||||
payload: Final = {field: malformed, "model": "synthetic-model", "temperature": 0.25}
|
||||
assert redact_credentials_in_request(payload) == {
|
||||
field: "REDACTED",
|
||||
"model": "synthetic-model",
|
||||
"temperature": 0.25,
|
||||
}
|
||||
assert payload[field] is malformed
|
||||
|
||||
|
||||
@pytest.mark.parametrize("factory", [dict, MappingProxyType, httpx.Headers])
|
||||
@pytest.mark.parametrize("field", ["headers", "extra_headers"])
|
||||
def test_request_redaction_preserves_normal_headers_and_tool_schema(factory, field: str) -> None:
|
||||
from litellm.litellm_core_utils.sensitive_data_masker import redact_credentials_in_request
|
||||
|
||||
headers: Final = factory(
|
||||
{
|
||||
"authorization": "synthetic-header-secret",
|
||||
"cookie": "synthetic-cookie",
|
||||
"x-request-id": "safe-correlation",
|
||||
"content-type": "application/json",
|
||||
}
|
||||
)
|
||||
schema: Final = {
|
||||
"type": "object",
|
||||
"properties": {"headers": {"type": "string"}, "extra_headers": {"type": "array"}},
|
||||
}
|
||||
result: Final = redact_credentials_in_request({field: headers, "tools": [{"parameters": schema}]})
|
||||
assert result == {
|
||||
field: {
|
||||
"authorization": "REDACTED",
|
||||
"cookie": "REDACTED",
|
||||
"x-request-id": "safe-correlation",
|
||||
"content-type": "application/json",
|
||||
},
|
||||
"tools": [{"parameters": schema}],
|
||||
}
|
||||
assert headers["authorization"] == "synthetic-header-secret"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("headers", [None, {}])
|
||||
def test_request_redaction_preserves_unset_headers(headers) -> None:
|
||||
from litellm.litellm_core_utils.sensitive_data_masker import redact_credentials_in_request
|
||||
|
||||
assert redact_credentials_in_request({"headers": headers}) == {"headers": headers}
|
||||
|
||||
|
||||
def test_request_redaction_never_renders_non_string_header_values() -> None:
|
||||
from litellm.litellm_core_utils.sensitive_data_masker import redact_credentials_in_request
|
||||
|
||||
class SensitiveHeaderValue:
|
||||
def __repr__(self) -> str:
|
||||
pytest.fail("A non-string header value must not be rendered")
|
||||
|
||||
assert redact_credentials_in_request({"headers": {"X-Request-Id": SensitiveHeaderValue()}}) == {
|
||||
"headers": "REDACTED"
|
||||
}
|
||||
|
||||
|
||||
def test_lists_are_preserved_not_converted_to_strings():
|
||||
"""
|
||||
Regression test: Ensure lists are preserved as JSON arrays, not converted to strings.
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import json
|
|||
import logging
|
||||
import traceback
|
||||
import wave
|
||||
from typing import Final
|
||||
from uuid import UUID
|
||||
|
||||
import httpx
|
||||
|
|
@ -11,13 +12,26 @@ import pytest
|
|||
import litellm
|
||||
from litellm.integrations.custom_logger import CustomLogger
|
||||
from litellm.llms.custom_httpx.http_handler import HTTPHandler
|
||||
from litellm.llms.flowspeech.text_to_speech.transformation import FlowSpeechTextToSpeechConfig
|
||||
from litellm.llms.flowspeech.text_to_speech.transformation import FlowSpeechException, FlowSpeechTextToSpeechConfig
|
||||
|
||||
REQUEST_ID = "b698824b-de05-486d-8833-05d5c4522a39"
|
||||
JOB_ID = "11111111-2222-4333-8444-555555555555"
|
||||
SECRET_SENTINEL = "credential-value-must-not-appear-672911"
|
||||
|
||||
|
||||
def assert_clean_exception_chain(error: BaseException | None, seen: frozenset[int] = frozenset()) -> None:
|
||||
if error is None or id(error) in seen:
|
||||
return
|
||||
assert SECRET_SENTINEL not in repr(error.args)
|
||||
assert SECRET_SENTINEL not in repr(vars(error))
|
||||
assert SECRET_SENTINEL not in "".join(traceback.format_exception(error))
|
||||
request: Final = getattr(error, "request", None)
|
||||
if isinstance(request, httpx.Request):
|
||||
assert SECRET_SENTINEL not in str(request.url) + repr(request.headers) + repr(request.content)
|
||||
assert_clean_exception_chain(error.__cause__, seen | {id(error)})
|
||||
assert_clean_exception_chain(error.__context__, seen | {id(error)})
|
||||
|
||||
|
||||
class FailureRecorder(CustomLogger):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
|
@ -237,6 +251,7 @@ def test_invalid_headers_raise_no_input_bearing_exception_chain(headers):
|
|||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("asynchronous", [False, True])
|
||||
@pytest.mark.parametrize("positional", [False, True])
|
||||
@pytest.mark.parametrize(
|
||||
"invalid,message",
|
||||
[
|
||||
|
|
@ -246,9 +261,25 @@ def test_invalid_headers_raise_no_input_bearing_exception_chain(headers):
|
|||
({"headers": {"Authorization": {"value": SECRET_SENTINEL}}}, "FlowSpeech headers must be a mapping"),
|
||||
({"extra_headers": {"Authorization": SECRET_SENTINEL.encode()}}, "FlowSpeech headers must be a mapping"),
|
||||
({"extra_headers": {"Authorization": {"value": SECRET_SENTINEL}}}, "FlowSpeech headers must be a mapping"),
|
||||
({"headers": SECRET_SENTINEL}, "FlowSpeech headers must be a mapping"),
|
||||
({"extra_headers": SECRET_SENTINEL}, "FlowSpeech headers must be a mapping"),
|
||||
({"headers": {SECRET_SENTINEL.encode(): "unused"}}, "FlowSpeech headers must be a mapping"),
|
||||
({"extra_headers": {SECRET_SENTINEL.encode(): "unused"}}, "FlowSpeech headers must be a mapping"),
|
||||
({"headers": {"X-Correlation-Id": SECRET_SENTINEL.encode()}}, "FlowSpeech headers must be a mapping"),
|
||||
({"headers": {"X-Correlation-Id": {"value": SECRET_SENTINEL}}}, "FlowSpeech headers must be a mapping"),
|
||||
({"extra_headers": {"X-Correlation-Id": SECRET_SENTINEL.encode()}}, "FlowSpeech headers must be a mapping"),
|
||||
({"extra_headers": {"X-Correlation-Id": {"value": SECRET_SENTINEL}}}, "FlowSpeech headers must be a mapping"),
|
||||
],
|
||||
)
|
||||
async def test_invalid_credentials_never_reach_errors_or_logs(asynchronous, invalid, message, monkeypatch, caplog, capsys):
|
||||
async def test_invalid_credentials_never_reach_errors_or_logs(
|
||||
asynchronous,
|
||||
positional,
|
||||
invalid,
|
||||
message,
|
||||
monkeypatch,
|
||||
caplog,
|
||||
capsys,
|
||||
):
|
||||
caplog.set_level(logging.DEBUG, logger="LiteLLM")
|
||||
recorder = FailureRecorder()
|
||||
monkeypatch.setattr(litellm, "failure_callback", [recorder])
|
||||
|
|
@ -260,14 +291,20 @@ async def test_invalid_credentials_never_reach_errors_or_logs(asynchronous, inva
|
|||
return httpx.Response(200, json=success())
|
||||
|
||||
kwargs = {"model": "flowspeech/flowspeech-tts", "input": "Hello", "api_key": "test-key", **invalid}
|
||||
args: Final = (kwargs["model"], kwargs["input"], None, kwargs["api_key"]) if positional else ()
|
||||
call_kwargs: Final = (
|
||||
{key: value for key, value in kwargs.items() if key not in ("model", "input", "api_key")}
|
||||
if positional
|
||||
else kwargs
|
||||
)
|
||||
if asynchronous:
|
||||
async with httpx.AsyncClient(transport=httpx.MockTransport(respond)) as client:
|
||||
with pytest.raises(litellm.APIConnectionError, match=message) as error:
|
||||
await litellm.aspeech(**kwargs, client=client)
|
||||
await litellm.aspeech(*args, **call_kwargs, client=client)
|
||||
else:
|
||||
with httpx.Client(transport=httpx.MockTransport(respond)) as client:
|
||||
with pytest.raises(ValueError, match=message) as error:
|
||||
litellm.speech(**kwargs, client=client)
|
||||
litellm.speech(*args, **call_kwargs, client=client)
|
||||
captured = capsys.readouterr()
|
||||
assert requests == []
|
||||
assert recorder.errors
|
||||
|
|
@ -275,6 +312,136 @@ async def test_invalid_credentials_never_reach_errors_or_logs(asynchronous, inva
|
|||
assert SECRET_SENTINEL not in "".join(traceback.format_exception(error.value))
|
||||
assert SECRET_SENTINEL not in repr(recorder.errors)
|
||||
assert SECRET_SENTINEL not in caplog.text + captured.out + captured.err
|
||||
assert_clean_exception_chain(error.value)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("asynchronous", [False, True])
|
||||
@pytest.mark.parametrize("positional", [False, True])
|
||||
@pytest.mark.parametrize("successful", [False, True])
|
||||
@pytest.mark.parametrize("verbose", [False, True])
|
||||
@pytest.mark.parametrize("header_field", ["headers", "extra_headers"])
|
||||
@pytest.mark.parametrize("header_name", ["Authorization", SECRET_SENTINEL])
|
||||
async def test_request_log_redaction_preserves_speech_transport(
|
||||
asynchronous: bool,
|
||||
positional: bool,
|
||||
successful: bool,
|
||||
verbose: bool,
|
||||
header_field: str,
|
||||
header_name: str,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
capsys: pytest.CaptureFixture[str],
|
||||
) -> None:
|
||||
monkeypatch.setattr(litellm, "set_verbose", verbose)
|
||||
monkeypatch.setattr("litellm._logging.set_verbose", verbose)
|
||||
caplog.set_level(logging.WARNING if verbose else logging.DEBUG, logger="LiteLLM")
|
||||
headers: Final = {header_name: SECRET_SENTINEL, "X-Correlation-Id": "safe-correlation"}
|
||||
args: Final = ("flowspeech/flowspeech-tts", "Hello", "Kore", SECRET_SENTINEL) if positional else ()
|
||||
kwargs: Final = (
|
||||
{}
|
||||
if positional
|
||||
else {
|
||||
"model": "flowspeech/flowspeech-tts",
|
||||
"input": "Hello",
|
||||
"voice": "Kore",
|
||||
"api_key": SECRET_SENTINEL,
|
||||
}
|
||||
)
|
||||
calls: Final = []
|
||||
|
||||
def respond(request: httpx.Request) -> httpx.Response:
|
||||
calls.append(request.method)
|
||||
if request.method == "POST":
|
||||
assert request.headers["authorization"] == f"Bearer {SECRET_SENTINEL}"
|
||||
assert request.headers["x-correlation-id"] == "safe-correlation"
|
||||
assert request.headers["idempotency-key"] == REQUEST_ID
|
||||
assert json.loads(request.content) == {"input": {"mode": "single", "text": "Hello", "voiceName": "Kore"}}
|
||||
if not successful:
|
||||
return httpx.Response(
|
||||
400, json={"error": {"code": "synthetic", "message": "synthetic", "retryable": False}}
|
||||
)
|
||||
return httpx.Response(200, json=success())
|
||||
assert "authorization" not in request.headers
|
||||
assert "cookie" not in request.headers
|
||||
assert "x-correlation-id" not in request.headers
|
||||
return httpx.Response(200, content=wav_bytes())
|
||||
|
||||
async def call():
|
||||
if asynchronous:
|
||||
async with httpx.AsyncClient(transport=httpx.MockTransport(respond)) as client:
|
||||
return await litellm.aspeech(
|
||||
*args,
|
||||
**kwargs,
|
||||
**{header_field: headers},
|
||||
request_id=REQUEST_ID,
|
||||
max_retries=0,
|
||||
client=client,
|
||||
)
|
||||
with httpx.Client(transport=httpx.MockTransport(respond)) as client:
|
||||
return litellm.speech(
|
||||
*args,
|
||||
**kwargs,
|
||||
**{header_field: headers},
|
||||
request_id=REQUEST_ID,
|
||||
max_retries=0,
|
||||
client=client,
|
||||
)
|
||||
|
||||
if successful:
|
||||
result: Final = await call()
|
||||
assert result.content == wav_bytes()
|
||||
else:
|
||||
with pytest.raises(litellm.BadRequestError if asynchronous else FlowSpeechException) as error:
|
||||
await call()
|
||||
assert_clean_exception_chain(error.value)
|
||||
assert error.value.status_code == 400
|
||||
captured: Final = capsys.readouterr()
|
||||
output: Final = caplog.text + captured.out + captured.err
|
||||
assert SECRET_SENTINEL not in output
|
||||
assert "REDACTED" in output
|
||||
assert ("safe-correlation" in output) is (header_name == "Authorization")
|
||||
assert "Hello" in output and "Kore" in output
|
||||
assert calls == (["POST", "GET"] if successful else ["POST"])
|
||||
assert headers == {header_name: SECRET_SENTINEL, "X-Correlation-Id": "safe-correlation"}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("asynchronous", [False, True])
|
||||
@pytest.mark.parametrize("debug", [False, True])
|
||||
async def test_positional_key_debug_controls(
|
||||
asynchronous: bool,
|
||||
debug: bool,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
capsys: pytest.CaptureFixture[str],
|
||||
) -> None:
|
||||
monkeypatch.setattr(litellm, "set_verbose", False)
|
||||
monkeypatch.setattr("litellm._logging.set_verbose", False)
|
||||
caplog.set_level(logging.DEBUG if debug else logging.ERROR, logger="LiteLLM")
|
||||
calls: Final = []
|
||||
|
||||
def respond(request: httpx.Request) -> httpx.Response:
|
||||
calls.append(request.method)
|
||||
assert request.headers["authorization"] == f"Bearer {SECRET_SENTINEL}"
|
||||
return httpx.Response(400, json={"error": {"code": "synthetic", "message": "synthetic", "retryable": False}})
|
||||
|
||||
with pytest.raises(litellm.BadRequestError if asynchronous else FlowSpeechException) as error:
|
||||
if asynchronous:
|
||||
async with httpx.AsyncClient(transport=httpx.MockTransport(respond)) as client:
|
||||
await litellm.aspeech(
|
||||
"flowspeech/flowspeech-tts", "Hello", "Kore", SECRET_SENTINEL, client=client, max_retries=0
|
||||
)
|
||||
else:
|
||||
with httpx.Client(transport=httpx.MockTransport(respond)) as client:
|
||||
litellm.speech(
|
||||
"flowspeech/flowspeech-tts", "Hello", "Kore", SECRET_SENTINEL, client=client, max_retries=0
|
||||
)
|
||||
captured: Final = capsys.readouterr()
|
||||
assert SECRET_SENTINEL not in caplog.text + captured.out + captured.err
|
||||
assert ("Request to litellm:" in caplog.text) is debug
|
||||
assert calls == ["POST"]
|
||||
assert_clean_exception_chain(error.value)
|
||||
|
||||
|
||||
def test_registers_provider():
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import base64
|
|||
import copy
|
||||
import contextlib
|
||||
import contextvars
|
||||
import inspect
|
||||
import io
|
||||
import json
|
||||
import logging
|
||||
|
|
@ -6946,6 +6947,124 @@ class TestVerboseRequestLineRedaction:
|
|||
|
||||
FAKE_API_KEY: Final = "sk-fake-lit6823-0000000000000000"
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"function_name", ["speech", "aspeech", "embedding", "aembedding", "completion", "acompletion"]
|
||||
)
|
||||
@pytest.mark.parametrize("verbose", [False, True])
|
||||
def test_positional_credentials_follow_public_signatures(
|
||||
self,
|
||||
function_name: str,
|
||||
verbose: bool,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
capsys: pytest.CaptureFixture[str],
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
from litellm.utils import print_args_passed_to_litellm
|
||||
|
||||
function: Final = getattr(litellm, function_name)
|
||||
signature_name: Final = function_name[1:] if function_name in ("aspeech", "aembedding") else function_name
|
||||
signature_function: Final = getattr(litellm, signature_name)
|
||||
sentinel: Final = "SYNTHETIC-POSITIONAL-KEY-ONLY"
|
||||
input_value: Final = "synthetic-input" if signature_name in ("speech", "embedding") else []
|
||||
bound: Final = inspect.signature(signature_function).bind_partial(
|
||||
"synthetic-model", input_value, api_key=sentinel
|
||||
)
|
||||
bound.apply_defaults()
|
||||
args: Final = bound.args
|
||||
monkeypatch.setattr(litellm, "set_verbose", verbose)
|
||||
monkeypatch.setattr("litellm._logging.set_verbose", verbose)
|
||||
with caplog.at_level(logging.DEBUG if not verbose else logging.WARNING, logger=verbose_logger.name):
|
||||
print_args_passed_to_litellm(function, args, {"max_retries": 0})
|
||||
captured: Final = capsys.readouterr()
|
||||
output: Final = caplog.text + captured.out + captured.err
|
||||
assert f"litellm.{function_name}(" in output
|
||||
assert "synthetic-model" in output
|
||||
assert "max_retries=0" in output
|
||||
assert "REDACTED" in output
|
||||
assert sentinel not in output
|
||||
assert sentinel in args
|
||||
|
||||
@pytest.mark.parametrize("field", ["headers", "extra_headers"])
|
||||
@pytest.mark.parametrize("malformed", ["synthetic-header-value", {b"synthetic-header-value": "unused"}])
|
||||
def test_malformed_headers_do_not_reach_request_line(
|
||||
self,
|
||||
field: str,
|
||||
malformed: object,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
from litellm.utils import print_args_passed_to_litellm
|
||||
|
||||
with caplog.at_level(logging.DEBUG, logger=verbose_logger.name):
|
||||
print_args_passed_to_litellm(litellm.completion, (), {"model": "synthetic-model", field: malformed})
|
||||
assert "synthetic-header-value" not in caplog.text
|
||||
assert f"{field}='REDACTED'" in caplog.text
|
||||
assert "model='synthetic-model'" in caplog.text
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("asynchronous", [False, True])
|
||||
@pytest.mark.parametrize("positional", [False, True])
|
||||
async def test_non_speech_mock_completion_preserves_result_and_context(
|
||||
self,
|
||||
asynchronous: bool,
|
||||
positional: bool,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
function: Final = litellm.acompletion if asynchronous else litellm.completion
|
||||
params: Final = {
|
||||
"model": "openai/synthetic-model",
|
||||
"messages": [{"role": "user", "content": "synthetic input"}],
|
||||
"api_key": self.FAKE_API_KEY,
|
||||
"temperature": 0.25,
|
||||
"extra_headers": {"Authorization": self.FAKE_API_KEY, "X-Request-Id": "safe-correlation"},
|
||||
}
|
||||
bound: Final = inspect.signature(function).bind_partial(**params)
|
||||
bound.apply_defaults()
|
||||
args: Final = bound.args if positional else ()
|
||||
kwargs: Final = bound.kwargs if positional else params
|
||||
with caplog.at_level(logging.DEBUG, logger=verbose_logger.name):
|
||||
response: Final = (
|
||||
await litellm.acompletion(*args, **kwargs, mock_response="synthetic output")
|
||||
if asynchronous
|
||||
else litellm.completion(*args, **kwargs, mock_response="synthetic output")
|
||||
)
|
||||
assert response.choices[0].message.content == "synthetic output"
|
||||
assert self.FAKE_API_KEY not in caplog.text
|
||||
assert "safe-correlation" in caplog.text
|
||||
assert "synthetic input" in caplog.text
|
||||
assert "0.25" in caplog.text
|
||||
assert params["api_key"] == self.FAKE_API_KEY
|
||||
assert params["extra_headers"]["Authorization"] == self.FAKE_API_KEY
|
||||
|
||||
def test_unidentified_varargs_are_hidden_without_losing_keyword_context(
|
||||
self,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
from litellm.utils import print_args_passed_to_litellm
|
||||
|
||||
def opaque(*args: object, **kwargs: object) -> None:
|
||||
pass
|
||||
|
||||
with caplog.at_level(logging.DEBUG, logger=verbose_logger.name):
|
||||
print_args_passed_to_litellm(opaque, (self.FAKE_API_KEY,), {"model": "synthetic-model"})
|
||||
assert self.FAKE_API_KEY not in caplog.text
|
||||
assert "litellm.opaque('REDACTED', model='synthetic-model')" in caplog.text
|
||||
|
||||
def test_positional_only_and_surplus_arguments_are_sanitized(
|
||||
self,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
from litellm.utils import print_args_passed_to_litellm
|
||||
|
||||
def endpoint(model: str, api_key: str, /, *, timeout: int = 10) -> None:
|
||||
pass
|
||||
|
||||
with caplog.at_level(logging.DEBUG, logger=verbose_logger.name):
|
||||
print_args_passed_to_litellm(
|
||||
endpoint, ("synthetic-model", self.FAKE_API_KEY, self.FAKE_API_KEY), {"timeout": 2}
|
||||
)
|
||||
assert self.FAKE_API_KEY not in caplog.text
|
||||
assert "litellm.endpoint('synthetic-model', 'REDACTED', 'REDACTED', timeout=2)" in caplog.text
|
||||
|
||||
def _verbose_request_line(self, capsys, monkeypatch, **kwargs) -> str:
|
||||
monkeypatch.setattr(litellm, "set_verbose", True)
|
||||
monkeypatch.setattr("litellm._logging.set_verbose", True)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue