Coverage for .venv/lib/python3.13/site-packages/litellm/proxy/guardrails/guardrail_hooks/microsoft_purview/__init__.py: 24%

24 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-10 12:01 +0000

1from typing import TYPE_CHECKING, Final 

2 

3from litellm.types.guardrails import SupportedGuardrailIntegrations 

4 

5from .purview_dlp import MicrosoftPurviewDLPGuardrail 

6 

7if TYPE_CHECKING: 7 ↛ 8line 7 didn't jump to line 8 because the condition on line 7 was never true

8 from litellm.types.guardrails import Guardrail, LitellmParams 

9 

10 

11def initialize_guardrail(litellm_params: "LitellmParams", guardrail: "Guardrail"): 

12 import litellm 

13 

14 tenant_id: Final = getattr(litellm_params, "tenant_id", None) 

15 client_id: Final = getattr(litellm_params, "client_id", None) 

16 

17 # client_secret can be passed via the standard api_key field or as 

18 # a dedicated client_secret parameter. 

19 client_secret: Final = litellm_params.api_key or getattr(litellm_params, "client_secret", None) 

20 

21 if not tenant_id: 

22 raise ValueError("Microsoft Purview: tenant_id is required") 

23 if not client_id: 

24 raise ValueError("Microsoft Purview: client_id is required") 

25 if not client_secret: 

26 raise ValueError("Microsoft Purview: client_secret (or api_key) is required") 

27 

28 guardrail_name: Final = guardrail.get("guardrail_name") 

29 if not guardrail_name: 

30 raise ValueError("Microsoft Purview: guardrail_name is required") 

31 

32 purview_guardrail: Final = MicrosoftPurviewDLPGuardrail( 

33 guardrail_name=guardrail_name, 

34 tenant_id=str(tenant_id), 

35 client_id=str(client_id), 

36 client_secret=str(client_secret), 

37 purview_app_name=str(getattr(litellm_params, "purview_app_name", None) or "LiteLLM"), 

38 user_id_field=str(getattr(litellm_params, "user_id_field", None) or "user_id"), 

39 event_hook=litellm_params.mode, 

40 default_on=litellm_params.default_on, 

41 ) 

42 

43 litellm.logging_callback_manager.add_litellm_callback(purview_guardrail) 

44 return purview_guardrail 

45 

46 

47guardrail_initializer_registry: Final = { 

48 SupportedGuardrailIntegrations.MICROSOFT_PURVIEW.value: initialize_guardrail, 

49} 

50 

51guardrail_class_registry: Final = { 

52 SupportedGuardrailIntegrations.MICROSOFT_PURVIEW.value: MicrosoftPurviewDLPGuardrail, 

53}