diff --git a/litellm/litellm_core_utils/litellm_logging.py b/litellm/litellm_core_utils/litellm_logging.py index 1b3a687f1f3..5a0473ac89e 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 @@ -4525,17 +4529,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 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",