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
fix: address req changes
Harshit28j1ba42d1d9941
diff --git a/tests/guardrails_tests/test_presidio_pii.py b/tests/guardrails_tests/test_presidio_pii.pyindex 0d730288e63..eda0c7bb5b5 100644--- a/tests/guardrails_tests/test_presidio_pii.py+++ b/tests/guardrails_tests/test_presidio_pii.py@@ -1,20 +1,19 @@import sysimport os-import io, asyncioimport pytest-import timefrom litellm import mock_completion-from unittest.mock import MagicMock, AsyncMock, patch+from unittest.mock import patch+sys.path.insert(0, os.path.abspath("../.."))import litellm-from litellm.proxy.guardrails.guardrail_hooks.presidio import _OPTIONAL_PresidioPIIMasking, PresidioPerRequestConfig+from litellm.proxy.guardrails.guardrail_hooks.presidio import (+ _OPTIONAL_PresidioPIIMasking,+ PresidioPerRequestConfig,+)from litellm.types.guardrails import PiiEntityType, PiiActionfrom litellm.proxy._types import UserAPIKeyAuthfrom litellm.caching.caching import DualCachefrom litellm.exceptions import BlockedPiiEntityError-from litellm.types.utils import CallTypes as LitellmCallTypes--@pytest.mark.asyncio@@ -26,42 +25,37 @@ async def test_presidio_with_entities_config():PiiEntityType.CREDIT_CARD: PiiAction.MASK,PiiEntityType.EMAIL_ADDRESS: PiiAction.MASK,}-+presidio_guardrail = _OPTIONAL_PresidioPIIMasking(pii_entities_config=pii_entities_config,presidio_analyzer_api_base=os.environ.get("PRESIDIO_ANALYZER_API_BASE"),- presidio_anonymizer_api_base=os.environ.get("PRESIDIO_ANONYMIZER_API_BASE")+ presidio_anonymizer_api_base=os.environ.get("PRESIDIO_ANONYMIZER_API_BASE"),)-+# Test text with different PII typestest_text = "My credit card number is 4111-1111-1111-1111, my email is [email protected], and my phone is 555-123-4567"-+# Test the analyze request configurationanalyze_request = presidio_guardrail._get_presidio_analyze_request_payload(- text=test_text,- presidio_config=None,- request_data={}+ text=test_text, presidio_config=None, request_data={})-+# Verify entities were passed correctlyassert "entities" in analyze_requestassert set(analyze_request["entities"]) == set(pii_entities_config.keys())-+# Test the check_pii method - this will call the actual Presidio APIredacted_text = await presidio_guardrail.check_pii(- text=test_text,- output_parse_pii=True,- presidio_config=None,- request_data={}+ text=test_text, output_parse_pii=True, presidio_config=None, request_data={})-+# Verify PII has been masked/replaced/redacted in the resultassert "4111-1111-1111-1111" not in redacted_textassert "[email protected]" not in redacted_text# Since this entity is not in the config, it should not be maskedassert "555-123-4567" in redacted_text-+# The specific replacements will vary based on Presidio's implementationprint(f"Redacted text: {redacted_text}")@@ -73,10 +67,12 @@ async def test_presidio_apply_guardrail():presidio_guardrail = _OPTIONAL_PresidioPIIMasking(pii_entities_config={},presidio_analyzer_api_base=os.environ.get("PRESIDIO_ANALYZER_API_BASE"),- presidio_anonymizer_api_base=os.environ.get("PRESIDIO_ANONYMIZER_API_BASE")+ presidio_anonymizer_api_base=os.environ.get("PRESIDIO_ANONYMIZER_API_BASE"),)- test_text = "My credit card number is 4111-1111-1111-1111 and my email is [email protected]"+ test_text = (+ "My credit card number is 4111-1111-1111-1111 and my email is [email protected]"+ )response = await presidio_guardrail.apply_guardrail(inputs={"texts": [test_text]},request_data={},@@ -91,6 +87,7 @@ async def test_presidio_apply_guardrail():assert "4111-1111-1111-1111" not in modified_textassert "[email protected]" not in modified_text+@pytest.mark.asyncioasync def test_presidio_with_blocked_entities():"""Test for Presidio guardrail with blocked entities - requires actual Presidio API"""@@ -100,36 +97,33 @@ async def test_presidio_with_blocked_entities():PiiEntityType.CREDIT_CARD: PiiAction.BLOCK, # This entity should cause a blockPiiEntityType.EMAIL_ADDRESS: PiiAction.MASK, # This entity should be masked}-+presidio_guardrail = _OPTIONAL_PresidioPIIMasking(pii_entities_config=pii_entities_config,presidio_analyzer_api_base=os.environ.get("PRESIDIO_ANALYZER_API_BASE"),- presidio_anonymizer_api_base=os.environ.get("PRESIDIO_ANONYMIZER_API_BASE")+ presidio_anonymizer_api_base=os.environ.get("PRESIDIO_ANONYMIZER_API_BASE"),)-+# Test text with blocked PII type- test_text = "My credit card number is 4111-1111-1111-1111 and my email is [email protected]"-+ test_text = (+ "My credit card number is 4111-1111-1111-1111 and my email is [email protected]"+ )+# Verify the analyze request configurationanalyze_request = presidio_guardrail._get_presidio_analyze_request_payload(- text=test_text,- presidio_config=None,- request_data={}+ text=test_text, presidio_config=None, request_data={})-+# Verify entities were passed correctlyassert "entities" in analyze_requestassert set(analyze_request["entities"]) == set(pii_entities_config.keys())-+# Test that BlockedPiiEntityError is raised when check_pii is calledwith pytest.raises(BlockedPiiEntityError) as excinfo:await presidio_guardrail.check_pii(- text=test_text,- output_parse_pii=True,- presidio_config=None,- request_data={}+ text=test_text, output_parse_pii=True, presidio_config=None, request_data={})-+# Verify the error contains the correct entity typeassert excinfo.value.entity_type == PiiEntityType.CREDIT_CARDassert excinfo.value.guardrail_name == presidio_guardrail.guardrail_name@@ -143,37 +137,40 @@ async def test_presidio_pre_call_hook_with_blocked_entities():PiiEntityType.CREDIT_CARD: PiiAction.BLOCK, # This entity should cause a blockPiiEntityType.EMAIL_ADDRESS: PiiAction.MASK, # This entity should be masked}-+presidio_guardrail = _OPTIONAL_PresidioPIIMasking(pii_entities_config=pii_entities_config,presidio_analyzer_api_base=os.environ.get("PRESIDIO_ANALYZER_API_BASE"),- presidio_anonymizer_api_base=os.environ.get("PRESIDIO_ANONYMIZER_API_BASE")+ presidio_anonymizer_api_base=os.environ.get("PRESIDIO_ANONYMIZER_API_BASE"),)-+# Create a sample chat completion request with PII datadata = {"messages": [{"role": "system", "content": "You are a helpful assistant."},- {"role": "user", "content": "My credit card is 4111-1111-1111-1111 and my email is [email protected]."}+ {+ "role": "user",+ "content": "My credit card is 4111-1111-1111-1111 and my email is [email protected].",+ },],- "model": "gpt-3.5-turbo"+ "model": "gpt-3.5-turbo",}-+# Mock objects needed for the pre-call hookuser_api_key_dict = UserAPIKeyAuth(api_key="test_key")cache = DualCache()-+# Call the pre-call hook and expect BlockedPiiEntityErrorwith pytest.raises(BlockedPiiEntityError) as excinfo:await presidio_guardrail.async_pre_call_hook(user_api_key_dict=user_api_key_dict,cache=cache,data=data,- call_type="completion"+ call_type="completion",)-+print(f"got error: {excinfo}")-+# Verify the error contains the correct entity typeassert excinfo.value.entity_type == PiiEntityType.CREDIT_CARDassert excinfo.value.guardrail_name == presidio_guardrail.guardrail_name@@ -188,44 +185,46 @@ async def test_presidio_pre_call_hook_with_different_call_types(call_type):PiiEntityType.CREDIT_CARD: PiiAction.MASK,PiiEntityType.EMAIL_ADDRESS: PiiAction.MASK,}-+presidio_guardrail = _OPTIONAL_PresidioPIIMasking(pii_entities_config=pii_entities_config,presidio_analyzer_api_base=os.environ.get("PRESIDIO_ANALYZER_API_BASE"),- presidio_anonymizer_api_base=os.environ.get("PRESIDIO_ANONYMIZER_API_BASE")+ presidio_anonymizer_api_base=os.environ.get("PRESIDIO_ANONYMIZER_API_BASE"),)-+# Create a sample request with PII datadata = {"messages": [{"role": "system", "content": "You are a helpful assistant."},- {"role": "user", "content": "My credit card is 4111-1111-1111-1111 and my email is [email protected]. My phone number is 555-123-4567"}+ {+ "role": "user",+ "content": "My credit card is 4111-1111-1111-1111 and my email is [email protected]. My phone number is 555-123-4567",+ },],- "model": "gpt-3.5-turbo"+ "model": "gpt-3.5-turbo",}-+# Mock objects needed for the pre-call hookuser_api_key_dict = UserAPIKeyAuth(api_key="test_key")cache = DualCache()-+# Call the pre-call hook with the specified call typemodified_data = await presidio_guardrail.async_pre_call_hook(- user_api_key_dict=user_api_key_dict,- cache=cache,- data=data,- call_type=call_type+ user_api_key_dict=user_api_key_dict, cache=cache, data=data, call_type=call_type)-+# Verify the messages have been modified to mask PII- assert modified_data["messages"][0]["content"] == "You are a helpful assistant." # System prompt should be unchanged-+ assert (+ modified_data["messages"][0]["content"] == "You are a helpful assistant."+ ) # System prompt should be unchanged+user_message = modified_data["messages"][1]["content"]assert "4111-1111-1111-1111" not in user_messageassert "[email protected]" not in user_message# Since this entity is not in the config, it should not be maskedassert "555-123-4567" in user_message-+print(f"Modified user message for call_type={call_type}: {user_message}")@@ -243,7 +242,7 @@ def test_validate_environment_missing_http(base_url):# Use patch.dict to temporarily modify environment variables only for this testenv_vars = {"PRESIDIO_ANALYZER_API_BASE": f"{base_url}/analyze",- "PRESIDIO_ANONYMIZER_API_BASE": f"{base_url}/anonymize"+ "PRESIDIO_ANONYMIZER_API_BASE": f"{base_url}/anonymize",}with patch.dict(os.environ, env_vars):pii_masking.validate_environment()@@ -294,8 +293,12 @@ async def test_output_parsing():new_response = await pii_masking.async_post_call_success_hook(user_api_key_dict=UserAPIKeyAuth(),data={- "messages": [{"role": "system", "content": "You are an helpfull assistant"}],- "pii_tokens": {"<PERSON>": "Jane Doe", "<PHONE_NUMBER>": "034453334"},+ "messages": [+ {"role": "system", "content": "You are an helpfull assistant"}+ ],+ "metadata": {+ "pii_tokens": {"<PERSON>": "Jane Doe", "<PHONE_NUMBER>": "034453334"}+ },},response=response,)@@ -440,24 +443,26 @@ async def test_presidio_pii_masking_logging_output_only_no_pre_api_hook():@pytest.mark.asyncio[email protected](os.environ, {- "PRESIDIO_ANALYZER_API_BASE": "http://localhost:5002",- "PRESIDIO_ANONYMIZER_API_BASE": "http://localhost:5001"-})+ os.environ,+ {+ "PRESIDIO_ANALYZER_API_BASE": "http://localhost:5002",+ "PRESIDIO_ANONYMIZER_API_BASE": "http://localhost:5001",+ },+)async def test_presidio_pii_masking_logging_output_only_logged_response_guardrails_config():from typing import Dict, List, Optionalimport litellmfrom litellm.proxy.guardrails.init_guardrails import initialize_guardrailsfrom litellm.types.guardrails import (- GuardrailItem,GuardrailItemSpec,GuardrailEventHooks,)litellm.set_verbose = True# Environment variables are now patched via the decorator instead of setting them directly-+guardrails_config: List[Dict[str, GuardrailItemSpec]] = [{"pii_masking": {@@ -499,60 +504,53 @@ async def test_presidio_pii_masking_logging_output_only_logged_response_guardraiasync def test_presidio_language_configuration():"""Test that presidio_language parameter is properly set and used in analyze requests"""litellm._turn_on_debug()-+# Test with German language using mock testing to avoid API callspresidio_guardrail_de = _OPTIONAL_PresidioPIIMasking(pii_entities_config={},presidio_language="de",- mock_testing=True # This bypasses the API validation+ mock_testing=True, # This bypasses the API validation)-+test_text = "Meine Telefonnummer ist +49 30 12345678"-+# Test the analyze request configurationanalyze_request = presidio_guardrail_de._get_presidio_analyze_request_payload(- text=test_text,- presidio_config=None,- request_data={}+ text=test_text, presidio_config=None, request_data={})-+# Verify the language is set to Germanassert analyze_request["language"] == "de"assert analyze_request["text"] == test_text-+# Test with Spanish languagepresidio_guardrail_es = _OPTIONAL_PresidioPIIMasking(- pii_entities_config={},- presidio_language="es",- mock_testing=True+ pii_entities_config={}, presidio_language="es", mock_testing=True)-+test_text_es = "Mi número de teléfono es +34 912 345 678"-+analyze_request_es = presidio_guardrail_es._get_presidio_analyze_request_payload(- text=test_text_es,- presidio_config=None,- request_data={}+ text=test_text_es, presidio_config=None, request_data={})-+# Verify the language is set to Spanishassert analyze_request_es["language"] == "es"assert analyze_request_es["text"] == test_text_es-+# Test default language (English) when not specifiedpresidio_guardrail_default = _OPTIONAL_PresidioPIIMasking(- pii_entities_config={},- mock_testing=True+ pii_entities_config={}, mock_testing=True)-+test_text_en = "My phone number is +1 555-123-4567"-- analyze_request_default = presidio_guardrail_default._get_presidio_analyze_request_payload(- text=test_text_en,- presidio_config=None,- request_data={}++ analyze_request_default = (+ presidio_guardrail_default._get_presidio_analyze_request_payload(+ text=test_text_en, presidio_config=None, request_data={}+ ))-+# Verify the language defaults to Englishassert analyze_request_default["language"] == "en"assert analyze_request_default["text"] == test_text_en@@ -562,36 +560,30 @@ async def test_presidio_language_configuration():async def test_presidio_language_configuration_with_per_request_override():"""Test that per-request language configuration overrides the default configured language"""litellm._turn_on_debug()-+# Set up guardrail with German as default languagepresidio_guardrail = _OPTIONAL_PresidioPIIMasking(- pii_entities_config={},- presidio_language="de",- mock_testing=True+ pii_entities_config={}, presidio_language="de", mock_testing=True)-+test_text = "Test text with PII"-+# Test with per-request config overriding the default languagepresidio_config = PresidioPerRequestConfig(language="fr")-+analyze_request = presidio_guardrail._get_presidio_analyze_request_payload(- text=test_text,- presidio_config=presidio_config,- request_data={}+ text=test_text, presidio_config=presidio_config, request_data={})-+# Verify the per-request language (French) overrides the default (German)assert analyze_request["language"] == "fr"assert analyze_request["text"] == test_text-+# Test without per-request config - should use default languageanalyze_request_default = presidio_guardrail._get_presidio_analyze_request_payload(- text=test_text,- presidio_config=None,- request_data={}+ text=test_text, presidio_config=None, request_data={})-+# Verify the default language (German) is usedassert analyze_request_default["language"] == "de"assert analyze_request_default["text"] == test_text