From 02df4c6b30ed262c31da82f0c67c149abc62ec22 Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Mon, 27 Oct 2025 13:45:09 -0700 Subject: [PATCH] [Fix] DD logging - ensure key's metadata + guardrail is logged on DD (#15980) * fix get_sanitized_user_information_from_key * test_get_sanitized_user_information_from_key_includes_guardrails_metadata --- litellm/proxy/litellm_pre_call_utils.py | 3 ++- litellm/proxy/proxy_config.yaml | 3 +++ .../proxy/test_litellm_pre_call_utils.py | 21 +++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/litellm/proxy/litellm_pre_call_utils.py b/litellm/proxy/litellm_pre_call_utils.py index 3534695d9f..1e53655dd0 100644 --- a/litellm/proxy/litellm_pre_call_utils.py +++ b/litellm/proxy/litellm_pre_call_utils.py @@ -585,7 +585,8 @@ class LiteLLMProxyRequestSetup: if user_api_key_dict.budget_reset_at else None ), - user_api_key_auth_metadata=None, + + user_api_key_auth_metadata=user_api_key_dict.metadata, ) return user_api_key_logged_metadata diff --git a/litellm/proxy/proxy_config.yaml b/litellm/proxy/proxy_config.yaml index c727d14dc9..f28af41c26 100644 --- a/litellm/proxy/proxy_config.yaml +++ b/litellm/proxy/proxy_config.yaml @@ -21,3 +21,6 @@ search_tools: litellm_params: search_provider: exa_ai api_key: os.environ/EXA_API_KEY + +litellm_settings: + callbacks: ["datadog"] \ No newline at end of file diff --git a/tests/test_litellm/proxy/test_litellm_pre_call_utils.py b/tests/test_litellm/proxy/test_litellm_pre_call_utils.py index bb95bd4109..bcc0e5ac2d 100644 --- a/tests/test_litellm/proxy/test_litellm_pre_call_utils.py +++ b/tests/test_litellm/proxy/test_litellm_pre_call_utils.py @@ -1119,3 +1119,24 @@ def test_add_internal_user_from_user_mapping_no_header_or_mapping_returns_unchan ) assert result is user_api_key_dict assert user_api_key_dict.user_id is None + + +def test_get_sanitized_user_information_from_key_includes_guardrails_metadata(): + """ + Test that get_sanitized_user_information_from_key includes guardrails field from key metadata in the returned payload + """ + user_api_key_dict = UserAPIKeyAuth( + api_key="test-key-hash", + key_alias="test-alias", + user_id="test-user", + metadata={"guardrails": ["presidio", "aporia"], "other_field": "value"}, + ) + + result = LiteLLMProxyRequestSetup.get_sanitized_user_information_from_key( + user_api_key_dict=user_api_key_dict + ) + + assert result["user_api_key_auth_metadata"] is not None + assert "guardrails" in result["user_api_key_auth_metadata"] + assert result["user_api_key_auth_metadata"]["guardrails"] == ["presidio", "aporia"] + assert result["user_api_key_auth_metadata"]["other_field"] == "value"