Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

perf: optimize get_standard_logging_metadata with set intersection (#19685)

ryan-crabbe8c7051686b40
diff --git a/litellm/litellm_core_utils/litellm_logging.py b/litellm/litellm_core_utils/litellm_logging.py
index 14015225f38..d70798060cf 100644
--- a/litellm/litellm_core_utils/litellm_logging.py
+++ b/litellm/litellm_core_utils/litellm_logging.py
@@ -203,6 +203,10 @@ except Exception as e:
EnterpriseStandardLoggingPayloadSetupVAR = None
_in_memory_loggers: List[Any] = []
+_STANDARD_LOGGING_METADATA_KEYS: frozenset = frozenset(
+ StandardLoggingMetadata.__annotations__.keys()
+)
+
### GLOBAL VARIABLES ###
# Cache custom pricing keys as frozenset for O(1) lookups instead of looping through 49 keys
@@ -4515,17 +4519,12 @@ class StandardLoggingPayloadSetup:
user_api_key_auth_metadata=None,
)
if isinstance(metadata, dict):
- # Filter the metadata dictionary to include only the specified keys
- supported_keys = StandardLoggingMetadata.__annotations__.keys()
- for key in supported_keys:
- if key in metadata:
- clean_metadata[key] = metadata[key] # type: ignore
-
- if metadata.get("user_api_key") is not None:
- if is_valid_sha256_hash(str(metadata.get("user_api_key"))):
- clean_metadata["user_api_key_hash"] = metadata.get(
- "user_api_key"
- ) # this is the hash
+ for key in metadata.keys() & _STANDARD_LOGGING_METADATA_KEYS:
+ clean_metadata[key] = metadata[key] # type: ignore
+
+ user_api_key = metadata.get("user_api_key")
+ if user_api_key and isinstance(user_api_key, str) and is_valid_sha256_hash(user_api_key):
+ clean_metadata["user_api_key_hash"] = user_api_key
_potential_requester_metadata = metadata.get(
"metadata", None
) # check if user passed metadata in the sdk request - e.g. metadata for langsmith logging - https://docs.litellm.ai/docs/observability/langsmith_integration#set-langsmith-fields
diff --git a/tests/logging_callback_tests/test_standard_logging_payload.py b/tests/logging_callback_tests/test_standard_logging_payload.py
index 3d8ffbf1f7f..53f555e7658 100644
--- a/tests/logging_callback_tests/test_standard_logging_payload.py
+++ b/tests/logging_callback_tests/test_standard_logging_payload.py
@@ -219,6 +219,22 @@ def test_get_standard_logging_metadata_invalid_user_api_key():
assert result["user_api_key_hash"] is None
+def test_get_standard_logging_metadata_non_string_user_api_key():
+ """Non-string user_api_key should not be set as user_api_key_hash."""
+ metadata = {"user_api_key": 12345}
+ result = StandardLoggingPayloadSetup.get_standard_logging_metadata(metadata)
+ all_fields_present(result)
+ assert result["user_api_key_hash"] is None
+
+
+def test_get_standard_logging_metadata_none_user_api_key():
+ """None user_api_key should not be set as user_api_key_hash."""
+ metadata = {"user_api_key": None}
+ result = StandardLoggingPayloadSetup.get_standard_logging_metadata(metadata)
+ all_fields_present(result)
+ assert result["user_api_key_hash"] is None
+
+
def test_get_standard_logging_metadata_invalid_keys():
metadata = {
"user_api_key_alias": "test_alias",