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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
add warning message for non premium users
Jugal Bhattfefc5fc33ac5
diff --git a/enterprise/litellm_enterprise/enterprise_callbacks/send_emails/base_email.py b/enterprise/litellm_enterprise/enterprise_callbacks/send_emails/base_email.pyindex 92ad14403b6..9052aa6f8d7 100644--- a/enterprise/litellm_enterprise/enterprise_callbacks/send_emails/base_email.py+++ b/enterprise/litellm_enterprise/enterprise_callbacks/send_emails/base_email.py@@ -114,21 +114,44 @@ class BaseEmailLogger(CustomLogger):Returns:EmailParams object containing logo_url, support_contact, base_url, recipient_email, subject, and signature"""- logo_url = os.getenv("EMAIL_LOGO_URL", None) or LITELLM_LOGO_URL- support_contact = os.getenv("EMAIL_SUPPORT_CONTACT", self.DEFAULT_SUPPORT_EMAIL)- base_url = os.getenv("PROXY_BASE_URL", "http://0.0.0.0:4000")- signature = os.getenv("EMAIL_SIGNATURE", EMAIL_FOOTER)+ # Get email parameters with premium check for custom values+ custom_logo = os.getenv("EMAIL_LOGO_URL", None)+ custom_support = os.getenv("EMAIL_SUPPORT_CONTACT", None)+ custom_signature = os.getenv("EMAIL_SIGNATURE", None)+ custom_subject_invitation = os.getenv("EMAIL_SUBJECT_INVITATION", None)+ custom_subject_key_created = os.getenv("EMAIL_SUBJECT_KEY_CREATED", None)++ # Track which custom values were not applied+ unused_custom_fields = []++ # Function to safely get custom value or default+ def get_custom_or_default(custom_value: Optional[str], default_value: str, field_name: str) -> str:+ if custom_value is not None: # Only check premium if trying to use custom value+ from litellm.proxy.proxy_server import premium_user+ if premium_user is not True:+ unused_custom_fields.append(field_name)+ return default_value+ return custom_value+ return default_value++ # Get parameters, falling back to defaults if custom values aren't allowed+ logo_url = get_custom_or_default(custom_logo, LITELLM_LOGO_URL, "logo URL")+ support_contact = get_custom_or_default(custom_support, self.DEFAULT_SUPPORT_EMAIL, "support contact")+ base_url = os.getenv("PROXY_BASE_URL", "http://0.0.0.0:4000") # Not a premium feature+ signature = get_custom_or_default(custom_signature, EMAIL_FOOTER, "email signature")# Get custom subject template based on email event typeif email_event == EmailEvent.new_user_invitation:- subject_template = os.getenv(- "EMAIL_SUBJECT_INVITATION",- self.DEFAULT_SUBJECT_TEMPLATES[EmailEvent.new_user_invitation]+ subject_template = get_custom_or_default(+ custom_subject_invitation,+ self.DEFAULT_SUBJECT_TEMPLATES[EmailEvent.new_user_invitation],+ "invitation subject template")elif email_event == EmailEvent.virtual_key_created:- subject_template = os.getenv(- "EMAIL_SUBJECT_KEY_CREATED",- self.DEFAULT_SUBJECT_TEMPLATES[EmailEvent.virtual_key_created]+ subject_template = get_custom_or_default(+ custom_subject_key_created,+ self.DEFAULT_SUBJECT_TEMPLATES[EmailEvent.virtual_key_created],+ "key created subject template")else:subject_template = "LiteLLM: {event_message}"@@ -149,6 +172,19 @@ class BaseEmailLogger(CustomLogger):user_id=user_id, base_url=base_url)+ # If any custom fields were not applied, log a warning+ if unused_custom_fields:+ import logging+ logger = logging.getLogger(__name__)+ fields_str = ", ".join(unused_custom_fields)+ warning_msg = (+ f"Email sent with default values instead of custom values for: {fields_str}. "+ "This is an Enterprise feature. To use custom email fields, please upgrade to LiteLLM Enterprise. "+ "Schedule a meeting here: https://calendly.com/d/4mp-gd3-k5k/litellm-1-1-onboarding-chat"+ )+ logger.warning(warning_msg)+ print(warning_msg)+return EmailParams(logo_url=logo_url,support_contact=support_contact,diff --git a/tests/proxy_admin_ui_tests/test_email_customization.py b/tests/proxy_admin_ui_tests/test_email_customization.pyindex 5d3798e9264..e72c2415fcb 100644--- a/tests/proxy_admin_ui_tests/test_email_customization.py+++ b/tests/proxy_admin_ui_tests/test_email_customization.py@@ -7,6 +7,7 @@ sys.path.insert(0, os.path.abspath("../.."))from enterprise.litellm_enterprise.enterprise_callbacks.send_emails.base_email import BaseEmailLoggerfrom enterprise.litellm_enterprise.types.enterprise_callbacks.send_emails import EmailEventfrom litellm.integrations.email_templates.email_footer import EMAIL_FOOTER+from litellm.proxy._types import CommonProxyErrors@pytest.fixturedef mock_env_vars(monkeypatch):@@ -17,34 +18,68 @@ def mock_env_vars(monkeypatch):monkeypatch.setenv("EMAIL_SUBJECT_INVITATION", "Welcome to Test Company!")monkeypatch.setenv("EMAIL_SUBJECT_KEY_CREATED", "Your Test Company API Key")monkeypatch.setenv("PROXY_BASE_URL", "http://test.com")+ monkeypatch.setenv("PROXY_API_URL", "https://test.com")@pytest.mark.asyncio-async def test_get_email_params_custom_templates(mock_env_vars):- """Test that _get_email_params returns correct values with custom templates"""- email_logger = BaseEmailLogger()-- # Test invitation email params- invitation_params = await email_logger._get_email_params(- email_event=EmailEvent.new_user_invitation,- user_email="[email protected]",- event_message="New User Invitation"- )-- assert invitation_params.subject == "Welcome to Test Company!"- assert invitation_params.signature == "Best regards,\nTest Company Team"- assert invitation_params.logo_url == "https://test-company.com/logo.png"- assert invitation_params.support_contact == "[email protected]"- assert invitation_params.base_url == "http://test.com"-- # Test key created email params- key_params = await email_logger._get_email_params(- email_event=EmailEvent.virtual_key_created,- user_email="[email protected]",- event_message="API Key Created"- )-- assert key_params.subject == "Your Test Company API Key"- assert key_params.signature == "Best regards,\nTest Company Team"+async def test_get_email_params_custom_templates_premium_user(mock_env_vars):+ """Test that _get_email_params returns correct values with custom templates for premium users"""+ # Mock premium_user as True+ with patch("litellm.proxy.proxy_server.premium_user", True):+ email_logger = BaseEmailLogger()++ # Test invitation email params+ invitation_params = await email_logger._get_email_params(+ email_event=EmailEvent.new_user_invitation,+ user_email="[email protected]",+ event_message="New User Invitation"+ )++ assert invitation_params.subject == "Welcome to Test Company!"+ assert invitation_params.signature == "Best regards,\nTest Company Team"+ assert invitation_params.logo_url == "https://test-company.com/logo.png"+ assert invitation_params.support_contact == "[email protected]"+ assert invitation_params.base_url == "http://test.com"++ # Test key created email params+ key_params = await email_logger._get_email_params(+ email_event=EmailEvent.virtual_key_created,+ user_email="[email protected]",+ event_message="API Key Created"+ )++ assert key_params.subject == "Your Test Company API Key"+ assert key_params.signature == "Best regards,\nTest Company Team"++async def test_get_email_params_non_premium_user(mock_env_vars):+ """Test that non-premium users get default templates even when custom ones are provided"""+ # Mock premium_user as False+ with patch("litellm.proxy.proxy_server.premium_user", False):+ email_logger = BaseEmailLogger()++ # Test invitation email params+ email_params = await email_logger._get_email_params(+ email_event=EmailEvent.new_user_invitation,+ user_email="[email protected]",+ event_message="New User Invitation"+ )++ # Should use default values even though custom values are set in env+ assert email_params.subject == "LiteLLM: New User Invitation"+ assert email_params.signature == EMAIL_FOOTER+ assert email_params.logo_url == "https://litellm-listing.s3.amazonaws.com/litellm_logo.png"+ assert email_params.support_contact == "[email protected]"+++ # Test key created email params+ key_params = await email_logger._get_email_params(+ email_event=EmailEvent.virtual_key_created,+ user_email="[email protected]",+ event_message="API Key Created"+ )++ assert key_params.subject == "LiteLLM: API Key Created"+ assert key_params.signature == EMAIL_FOOTER@pytest.mark.asyncioasync def test_get_email_params_default_templates(monkeypatch):@@ -54,24 +89,26 @@ async def test_get_email_params_default_templates(monkeypatch):monkeypatch.delenv("EMAIL_SUBJECT_KEY_CREATED", raising=False)monkeypatch.delenv("EMAIL_SIGNATURE", raising=False)- email_logger = BaseEmailLogger()-- # Test invitation email params with default template- invitation_params = await email_logger._get_email_params(- email_event=EmailEvent.new_user_invitation,- user_email="[email protected]",- event_message="New User Invitation"- )-- assert invitation_params.subject == "LiteLLM: New User Invitation"- assert invitation_params.signature == EMAIL_FOOTER-- # Test key created email params with default template- key_params = await email_logger._get_email_params(- email_event=EmailEvent.virtual_key_created,- user_email="[email protected]",- event_message="API Key Created"- )-- assert key_params.subject == "LiteLLM: API Key Created"- assert key_params.signature == EMAIL_FOOTER\ No newline at end of file+ # Mock premium_user as True (shouldn't matter since no custom values are set)+ with patch("litellm.proxy.proxy_server.premium_user", True):+ email_logger = BaseEmailLogger()++ # Test invitation email params with default template+ invitation_params = await email_logger._get_email_params(+ email_event=EmailEvent.new_user_invitation,+ user_email="[email protected]",+ event_message="New User Invitation"+ )++ assert invitation_params.subject == "LiteLLM: New User Invitation"+ assert invitation_params.signature == EMAIL_FOOTER++ # Test key created email params with default template+ key_params = await email_logger._get_email_params(+ email_event=EmailEvent.virtual_key_created,+ user_email="[email protected]",+ event_message="API Key Created"+ )++ assert key_params.subject == "LiteLLM: API Key Created"+ assert key_params.signature == EMAIL_FOOTER\ No newline at end of file