Skip to content
Closed
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Any, Optional

from litellm.types.guardrails import SupportedGuardrailIntegrations

Expand All @@ -8,9 +8,21 @@
from litellm.types.guardrails import Guardrail, LitellmParams


def _get_config_value(
litellm_params: Any, optional_params: Any, attribute_name: str
) -> Optional[Any]:
if optional_params is not None:
value = getattr(optional_params, attribute_name, None)
Comment thread
veria-ai[bot] marked this conversation as resolved.
Resolved
if value is not None:
return value
return getattr(litellm_params, attribute_name, None)


def initialize_guardrail(litellm_params: "LitellmParams", guardrail: "Guardrail"):
import litellm

optional_params = getattr(litellm_params, "optional_params", None)

_generic_guardrail_api_callback = GenericGuardrailAPI(
api_base=litellm_params.api_base,
api_key=litellm_params.api_key,
Expand All @@ -25,6 +37,12 @@ def initialize_guardrail(litellm_params: "LitellmParams", guardrail: "Guardrail"
guardrail_name=guardrail.get("guardrail_name", ""),
event_hook=litellm_params.mode,
default_on=litellm_params.default_on,
streaming_end_of_stream_only=_get_config_value(
litellm_params, optional_params, "streaming_end_of_stream_only"
),
streaming_sampling_rate=_get_config_value(
litellm_params, optional_params, "streaming_sampling_rate"
),
)

litellm.logging_callback_manager.add_litellm_callback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

if TYPE_CHECKING:
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
from litellm.types.proxy.guardrails.guardrail_hooks.base import GuardrailConfigModel

GUARDRAIL_NAME = "generic_guardrail_api"

Expand Down Expand Up @@ -188,6 +189,8 @@ def __init__(
additional_provider_specific_params: Optional[Dict[str, Any]] = None,
unreachable_fallback: Literal["fail_closed", "fail_open"] = "fail_closed",
extra_headers: Optional[list] = None,
streaming_end_of_stream_only: Optional[bool] = None,
streaming_sampling_rate: Optional[int] = None,
**kwargs,
):
self.async_handler = get_async_httpx_client(
Expand Down Expand Up @@ -223,6 +226,17 @@ def __init__(
unreachable_fallback
)

# Read by UnifiedLLMGuardrails.async_post_call_streaming_iterator_hook
# via getattr(guardrail_to_apply, "streaming_*", default).
self.streaming_end_of_stream_only: bool = (
False
if streaming_end_of_stream_only is None
else streaming_end_of_stream_only
)
self.streaming_sampling_rate: int = (
5 if streaming_sampling_rate is None else streaming_sampling_rate
)

# Set supported event hooks
if "supported_event_hooks" not in kwargs:
kwargs["supported_event_hooks"] = [
Expand Down Expand Up @@ -511,3 +525,11 @@ async def apply_guardrail(
return self._handle_guardrail_request_error(
e, inputs, input_type, logging_obj, is_unreachable=False
)

@staticmethod
def get_config_model() -> Optional[type["GuardrailConfigModel"]]:
from litellm.types.proxy.guardrails.guardrail_hooks.generic_guardrail_api import (
GenericGuardrailAPIConfigModel,
)

return GenericGuardrailAPIConfigModel
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,29 @@ class GenericGuardrailAPIOptionalParams(BaseModel):
),
)

streaming_end_of_stream_only: Optional[bool] = Field(
default=None,
description=(
"If False (default when unset), the guardrail runs on sampled chunks during "
"the stream at the cadence set by streaming_sampling_rate, and an in-flight "
"BLOCKED stops further chunks from streaming. If True, the guardrail runs "
"once at end of stream over the assembled response; lower cost and latency, "
"but flagged content has already streamed to the client before the terminal "
"block. Defaults are applied in GenericGuardrailAPI.__init__ when None so "
"unset optional_params does not shadow top-level litellm_params."
),
)

streaming_sampling_rate: Optional[int] = Field(
default=None,
description=(
"When streaming_end_of_stream_only is False, the guardrail runs every Nth "
"streamed chunk. Ignored when streaming_end_of_stream_only is True. "
"Defaults to 5 in GenericGuardrailAPI.__init__ when None so unset "
"optional_params does not shadow top-level litellm_params."
),
)
Comment thread
schneidermr marked this conversation as resolved.
Resolved


class GenericGuardrailAPIConfigModel(
GuardrailConfigModel[GenericGuardrailAPIOptionalParams],
Expand Down
Loading
Loading