fix: avoid recursion in request log signature detection

This commit is contained in:
waeckerlinfederowicz66-sketch 2026-10-27 13:07:32 +08:00
parent c5e2a08a5e
commit d91c66bcd7
3 changed files with 59 additions and 5 deletions

View file

@ -8541,19 +8541,23 @@ def get_valid_models(
return [] # NON-Blocking
def _request_logging_parameters(original_function: Callable[..., object]) -> tuple[str, ...]:
def _positional_request_parameter_names(original_function: Callable[..., object]) -> tuple[str, ...]:
parameters: Final = inspect.signature(original_function).parameters.values()
positional: Final = tuple(
return tuple(
parameter.name
for parameter in parameters
if parameter.kind in (inspect.Parameter.POSITIONAL_ONLY, inspect.Parameter.POSITIONAL_OR_KEYWORD)
)
def _request_logging_parameters(original_function: Callable[..., object]) -> tuple[str, ...]:
positional: Final = _positional_request_parameter_names(original_function)
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 _positional_request_parameter_names(synchronous)
return ()

View file

@ -232,7 +232,9 @@ def test_gateway_base_preserves_prefix_without_duplicating_endpoint(base, expect
@pytest.mark.parametrize("prefix", ["/p%2Fq", "/p%2fq", "/p%252Fq", "/p%3Fq", "/p%23q"])
@pytest.mark.parametrize("suffix", ["/", "/api/v1/tts/jobs", "/api/v1/tts/jobs/"])
def test_gateway_prefix_preserves_encoded_path(prefix, suffix):
url = FlowSpeechTextToSpeechConfig().get_complete_url("flowspeech-tts", f"https://gateway.example{prefix}{suffix}", {})
url = FlowSpeechTextToSpeechConfig().get_complete_url(
"flowspeech-tts", f"https://gateway.example{prefix}{suffix}", {}
)
assert httpx.URL(url).raw_path == f"{prefix}/api/v1/tts/jobs".encode("ascii")
assert httpx.URL(url).query == b""
assert httpx.URL(url).fragment == ""
@ -426,7 +428,7 @@ async def test_positional_key_debug_controls(
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:
async def call() -> None:
if asynchronous:
async with httpx.AsyncClient(transport=httpx.MockTransport(respond)) as client:
await litellm.aspeech(
@ -437,6 +439,9 @@ async def test_positional_key_debug_controls(
litellm.speech(
"flowspeech/flowspeech-tts", "Hello", "Kore", SECRET_SENTINEL, client=client, max_retries=0
)
with pytest.raises(litellm.BadRequestError if asynchronous else FlowSpeechException) as error:
await call()
captured: Final = capsys.readouterr()
assert SECRET_SENTINEL not in caplog.text + captured.out + captured.err
assert ("Request to litellm:" in caplog.text) is debug

View file

@ -7049,6 +7049,51 @@ class TestVerboseRequestLineRedaction:
assert self.FAKE_API_KEY not in caplog.text
assert "litellm.opaque('REDACTED', model='synthetic-model')" in caplog.text
@pytest.mark.parametrize("async_prefix", [False, True])
def test_async_varargs_without_sync_signature_hide_credentials(
self,
async_prefix: bool,
caplog: pytest.LogCaptureFixture,
) -> None:
from litellm.utils import print_args_passed_to_litellm
async def opaque_async(*args: object, **kwargs: object) -> None:
pass
async def aopaque(*args: object, **kwargs: object) -> None:
pass
function: Final = aopaque if async_prefix else opaque_async
with caplog.at_level(logging.DEBUG, logger=verbose_logger.name):
print_args_passed_to_litellm(function, (self.FAKE_API_KEY,), {"model": "synthetic-model"})
assert self.FAKE_API_KEY not in caplog.text
assert f"litellm.{function.__name__}('REDACTED', model='synthetic-model')" in caplog.text
@pytest.mark.parametrize("error_type", [TypeError, ValueError])
def test_unavailable_signature_hides_all_positional_values(
self,
error_type: type[Exception],
caplog: pytest.LogCaptureFixture,
) -> None:
from litellm.utils import print_args_passed_to_litellm
class OpaqueCallable:
__name__ = "opaque"
@property
def __signature__(self) -> inspect.Signature:
raise error_type("signature unavailable")
def __call__(self, *args: object, **kwargs: object) -> None:
pass
with caplog.at_level(logging.DEBUG, logger=verbose_logger.name):
print_args_passed_to_litellm(
OpaqueCallable(), (self.FAKE_API_KEY, self.FAKE_API_KEY), {"model": "synthetic-model"}
)
assert self.FAKE_API_KEY not in caplog.text
assert "litellm.opaque('REDACTED', 'REDACTED', model='synthetic-model')" in caplog.text
def test_positional_only_and_surplus_arguments_are_sanitized(
self,
caplog: pytest.LogCaptureFixture,