mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-14 09:03:16 +00:00
fix(logging): keep raw_request when a request has no metadata
pre_call wrote raw_request into a new dict whenever litellm_params had no metadata or an empty one, so callbacks never saw it. SDK calls usually pass no metadata, which made log_raw_request_response do nothing for them Create the metadata dict on litellm_params when it is missing so the raw request reaches the callbacks
This commit is contained in:
parent
baddc67041
commit
27f73672a1
2 changed files with 23 additions and 1 deletions
|
|
@ -1677,7 +1677,9 @@ class Logging(LiteLLMLoggingBaseClass):
|
|||
# log raw request to provider (like LangFuse) -- if opted in.
|
||||
if self.log_raw_request_response is True or litellm.log_raw_request_response is True:
|
||||
_litellm_params: Final = self.model_call_details.get("litellm_params", {})
|
||||
_metadata: Final = _litellm_params.get("metadata", {}) or {}
|
||||
if _litellm_params.get("metadata") is None:
|
||||
_litellm_params["metadata"] = {}
|
||||
_metadata: Final = _litellm_params["metadata"]
|
||||
try:
|
||||
self.model_call_details["raw_request_typed_dict"] = RawRequestTypedDict(
|
||||
raw_request_api_base=self._get_masked_api_base(str(additional_args.get("api_base") or "")),
|
||||
|
|
|
|||
|
|
@ -8662,6 +8662,26 @@ def test_pre_call_redacts_and_masks_raw_request(logging_obj):
|
|||
assert "key=*****" in raw_api_base
|
||||
|
||||
|
||||
@pytest.mark.parametrize("litellm_params", [{}, {"metadata": None}, {"metadata": {}}])
|
||||
def test_pre_call_keeps_raw_request_when_request_has_no_metadata(logging_obj, litellm_params):
|
||||
"""Callbacks read raw_request from litellm_params metadata, which SDK calls often leave unset."""
|
||||
logging_obj.model_call_details["litellm_params"] = litellm_params
|
||||
logging_obj.log_raw_request_response = True
|
||||
|
||||
logging_obj.pre_call(
|
||||
input="hi",
|
||||
api_key="",
|
||||
additional_args={
|
||||
"api_base": "https://api.openai.com/v1/chat/completions",
|
||||
"headers": {},
|
||||
"complete_input_dict": {"model": "gpt-4o-mini"},
|
||||
},
|
||||
)
|
||||
|
||||
raw_request = logging_obj.model_call_details["litellm_params"]["metadata"]["raw_request"]
|
||||
assert "https://api.openai.com/v1/chat/completions" in raw_request
|
||||
|
||||
|
||||
_PRIVATE_RAW_REQUEST_ARGS: Final = {
|
||||
"api_base": "https://api.openai.com/v1/chat/completions",
|
||||
"headers": {},
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue