From 2e223a5418398ec8bd6bee05722394e0780c6d86 Mon Sep 17 00:00:00 2001 From: Ryan Crabbe Date: Fri, 23 Jan 2026 16:52:22 -0800 Subject: [PATCH 1/2] perf: Optimize get_standard_logging_metadata with set intersection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Cache StandardLoggingMetadata.__annotations__.keys() as module-level frozenset - Use set intersection to iterate only keys present in both metadata and supported keys - Single lookup for user_api_key instead of 3 separate .get() calls Results: - get_standard_logging_metadata: 1.55s → 1.41s (9.2% faster) --- litellm/litellm_core_utils/litellm_logging.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/litellm/litellm_core_utils/litellm_logging.py b/litellm/litellm_core_utils/litellm_logging.py index d3bcfe8200e..ef8cccb840b 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 ### sentry_sdk_instance = None @@ -4455,17 +4459,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 + for key in metadata.keys() & _STANDARD_LOGGING_METADATA_KEYS: + 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 + 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 From e659bbeb54cb8a279da18cb5f14f9c94e6cbc21a Mon Sep 17 00:00:00 2001 From: Ryan Crabbe Date: Wed, 28 Jan 2026 13:21:02 -0800 Subject: [PATCH 2/2] test: add unit tests for get_standard_logging_metadata non-string user_api_key handling --- .../test_standard_logging_payload.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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",