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]

Add User ID validation to ensure it is not an email or phone number (#10102)

raz-alonfada9c79beec
diff --git a/litellm/llms/anthropic/chat/transformation.py b/litellm/llms/anthropic/chat/transformation.py
index ad8c6b275f2..1c766536b7e 100644
--- a/litellm/llms/anthropic/chat/transformation.py
+++ b/litellm/llms/anthropic/chat/transformation.py
@@ -1,5 +1,6 @@
import json
import time
+import re
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union, cast
import httpx
@@ -641,6 +642,7 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig):
_litellm_metadata
and isinstance(_litellm_metadata, dict)
and "user_id" in _litellm_metadata
+ and not _valid_user_id(_litellm_metadata.get("user_id", None))
):
optional_params["metadata"] = {"user_id": _litellm_metadata["user_id"]}
@@ -948,3 +950,19 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig):
message=error_message,
headers=cast(httpx.Headers, headers),
)
+
+
+def _valid_user_id(user_id: str) -> bool:
+ """
+ Validate that user_id is not an email or phone number.
+ Returns: bool: True if valid (not email or phone), False otherwise
+ """
+ email_pattern = r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"
+ phone_pattern = r"^\+?[\d\s\(\)-]{7,}$"
+
+ if re.match(email_pattern, user_id):
+ return False
+ if re.match(phone_pattern, user_id):
+ return False
+
+ return True