Coverage for .venv/lib/python3.13/site-packages/litellm/proxy/guardrails/guardrail_hooks/conduct/__init__.py: 62%
19 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-10 12:01 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-10 12:01 +0000
1from __future__ import annotations
3from collections.abc import Mapping
4from types import MappingProxyType
5from typing import TYPE_CHECKING, Final
7from litellm.types.guardrails import SupportedGuardrailIntegrations
9from .conduct import ConductGuardrail
11if TYPE_CHECKING: 11 ↛ 12line 11 didn't jump to line 12 because the condition on line 11 was never true
12 from litellm.integrations.custom_guardrail import CustomGuardrail
13 from litellm.types.guardrails import Guardrail, LitellmParams
15DEFAULT_TIMEOUT_SECONDS: Final = 8.0
16_NO_EXTRAS: Final[Mapping[str, object]] = MappingProxyType({})
19def initialize_guardrail(
20 litellm_params: LitellmParams,
21 guardrail: Guardrail,
22 guardrail_cls: type[CustomGuardrail] = ConductGuardrail,
23) -> CustomGuardrail:
24 import litellm
26 extras: Final = litellm_params.model_extra or _NO_EXTRAS
27 _callback: Final = guardrail_cls(
28 api_url=litellm_params.api_base,
29 agent_token=litellm_params.api_key,
30 workspace_id=extras.get("workspace_id"),
31 tool_name=extras.get("tool_name", "llm_call"),
32 unreachable_fallback=litellm_params.unreachable_fallback,
33 timeout=DEFAULT_TIMEOUT_SECONDS if litellm_params.timeout is None else litellm_params.timeout,
34 guardrail_name=guardrail.get("guardrail_name", ""),
35 event_hook=litellm_params.mode,
36 default_on=litellm_params.default_on,
37 supported_event_hooks=guardrail_cls.get_supported_event_hooks(),
38 )
39 litellm.logging_callback_manager.add_litellm_callback(_callback)
40 return _callback
43guardrail_initializer_registry: Final = { # mutable-ok: module-level registry, built once and never mutated
44 SupportedGuardrailIntegrations.CONDUCT.value: initialize_guardrail,
45}
47guardrail_class_registry: Final = { # mutable-ok: module-level registry, built once and never mutated
48 SupportedGuardrailIntegrations.CONDUCT.value: ConductGuardrail,
49}