From c482013a8a2f5c7dfb072cfb8b74ad9546c59956 Mon Sep 17 00:00:00 2001 From: Agung Besti <35904444+abesticode@users.noreply.github.com> Date: Tue, 17 Nov 2026 06:18:08 +0700 Subject: [PATCH] fix(guardrails): log requests that a guardrail blocks on /apply_guardrail (#51014) * fix(guardrails): record the guardrail verdict on a request dict that starts empty The Bedrock guardrail and the shared `log_guardrail_information` wrapper both picked the request dict with `or {}`. An empty dict is falsy, so a caller that passed one had the verdict written to a throwaway dict. /apply_guardrail passes an empty dict for a request that carries no messages and no metadata, so nothing downstream could see what the guardrail decided. The verdict now goes to the dict the caller passed, and only a missing dict falls back to a new one. * fix(guardrails): log requests that a guardrail blocks on /apply_guardrail A request a guardrail blocked on /apply_guardrail was missing from the logs. The endpoint skipped the failure handlers for an HTTPException, the spend log failure hook wrote no row because the route is not an LLM API route, and the guardrail's verdict stayed on a request dict no logger reads. The endpoint now runs the failure handlers for every exception and copies the verdict onto the logged request, so allowed and blocked requests both carry it along with the guardrail's cost. It marks the failure of a request it had set up with a sentinel, and the spend log failure hook writes its row for that mark. A request rejected before setup, such as one with a bad key or an unknown guardrail, is still not logged. Guardrail records a caller sends in metadata are dropped for both response formats so a verdict cannot be forged in the logs. * fix(guardrails): keep Presidio and Gray Swan verdicts on empty request data Presidio's text-only path and Gray Swan's API-failure path replaced an empty request dict with a throwaway one, so the verdict never reached the logged request. The apply_guardrail logging tests now run the real request setup and read what a logging callback receives. * test(guardrails): cover the endpoint failure row, the sync decorator, and an unreachable Gray Swan without real sockets * fix(logging): redact prompt text in guardrail records when message logging is off * fix(logging): redact the failure payload when message logging is off * fix(logging): keep guardrail details for other callbacks when one opts out of message logging * fix(logging): redact failure events per callback and keep guardrail compression stats A callback that turns off message logging now gets the same redacted copy on failure events as on success events, so a blocked /apply_guardrail call no longer hands it the raw prompt and verdict Guardrail redaction keeps the numeric compression stats (tokens saved, compression ratio) that spend tracking reads, through one helper shared with the spend-log sanitizer * fix(policy): test each batch input on its own copy of the request data The guardrail logging decorator now records a verdict on an empty request dict instead of dropping it. The policy test endpoint shared one request dict across every input and merged it into the agent call, so each later input and agent request carried the earlier inputs' verdicts. Each input now gets its own copy, which keeps the caller's request data unchanged * test(policy): look up the counting guardrail through the real registry The batch isolation test now registers its guardrail as a litellm callback, so the endpoint finds it through the real guardrail registry instead of a mocked one, and its apply_guardrail override carries the same concrete types as the base class --------- Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com> --- litellm/integrations/custom_guardrail.py | 16 +- litellm/litellm_core_utils/litellm_logging.py | 15 +- litellm/litellm_core_utils/redact_messages.py | 63 ++++- .../proxy/guardrails/guardrail_endpoints.py | 43 ++- .../guardrail_hooks/bedrock_guardrails.py | 16 +- .../guardrail_hooks/grayswan/grayswan.py | 2 +- .../guardrails/guardrail_hooks/presidio.py | 2 +- litellm/proxy/guardrails/response_handlers.py | 13 +- .../proxy/hooks/proxy_track_cost_callback.py | 5 +- .../policy_endpoints/endpoints.py | 2 +- .../spend_tracking/spend_tracking_utils.py | 24 +- litellm/proxy/utils.py | 5 + .../integrations/test_custom_guardrail.py | 53 ++++ .../test_redact_messages.py | 61 +++++ .../test_bedrock_guardrails.py | 94 +++++++ .../guardrail_hooks/test_grayswan.py | 26 ++ .../guardrail_hooks/test_presidio.py | 15 ++ .../guardrails/test_guardrail_endpoints.py | 248 +++++++++++++++++- .../hooks/test_proxy_track_cost_callback.py | 42 +++ .../test_policy_endpoints.py | 42 +++ 20 files changed, 718 insertions(+), 69 deletions(-) diff --git a/litellm/integrations/custom_guardrail.py b/litellm/integrations/custom_guardrail.py index fafbdb1f95a..42d8fd560da 100644 --- a/litellm/integrations/custom_guardrail.py +++ b/litellm/integrations/custom_guardrail.py @@ -1989,7 +1989,13 @@ def log_guardrail_information(func): async def async_wrapper(*args, **kwargs): start_time: Final = datetime.now() # Move start_time inside the wrapper self: Final[CustomGuardrail] = args[0] - request_data: Final[dict] = kwargs.get("data") or kwargs.get("request_data") or {} + request_data: Final[dict] = ( + kwargs["data"] + if kwargs.get("data") is not None + else kwargs["request_data"] + if kwargs.get("request_data") is not None + else {} + ) event_type: Final = _infer_event_type_from_function_name(func.__name__) # Store original inputs for comparison (for apply_guardrail functions) @@ -2029,7 +2035,13 @@ def log_guardrail_information(func): def sync_wrapper(*args, **kwargs): start_time: Final = datetime.now() # Move start_time inside the wrapper self: Final[CustomGuardrail] = args[0] - request_data: Final[dict] = kwargs.get("data") or kwargs.get("request_data") or {} + request_data: Final[dict] = ( + kwargs["data"] + if kwargs.get("data") is not None + else kwargs["request_data"] + if kwargs.get("request_data") is not None + else {} + ) event_type: Final = _infer_event_type_from_function_name(func.__name__) # Store original inputs for comparison (for apply_guardrail functions) diff --git a/litellm/litellm_core_utils/litellm_logging.py b/litellm/litellm_core_utils/litellm_logging.py index 5986239999c..bb66c29c177 100644 --- a/litellm/litellm_core_utils/litellm_logging.py +++ b/litellm/litellm_core_utils/litellm_logging.py @@ -186,6 +186,7 @@ from litellm.litellm_core_utils.redact_messages import ( redact_message_input_output_from_logging, redacted_model_call_details_for_custom_logger, redacted_response_obj_for_custom_logger, + redacted_standard_logging_payload, should_redact_message_logging, ) from litellm.litellm_core_utils.served_output_texts import ( @@ -5203,7 +5204,7 @@ class Logging(LiteLLMLoggingBaseClass): ## STANDARDIZED LOGGING PAYLOAD - self.model_call_details["standard_logging_object"] = get_standard_logging_object_payload( + failure_payload: Final = get_standard_logging_object_payload( kwargs=self.model_call_details, init_response_obj={}, start_time=start_time, @@ -5214,6 +5215,11 @@ class Logging(LiteLLMLoggingBaseClass): original_exception=exception, standard_built_in_tools_params=self.standard_built_in_tools_params, ) + self.model_call_details["standard_logging_object"] = ( + redacted_standard_logging_payload(failure_payload) + if failure_payload is not None and should_redact_message_logging(self.model_call_details) + else failure_payload + ) return start_time, end_time def _should_estimate_failed_prompt(self) -> bool: @@ -5629,7 +5635,12 @@ class Logging(LiteLLMLoggingBaseClass): return if isinstance(callback, CustomLogger): # custom logger class await callback.async_log_failure_event( - kwargs=self.model_call_details, + kwargs=redacted_model_call_details_for_custom_logger( + model_call_details=callback.redact_standard_logging_payload_from_model_call_details( + model_call_details=self.model_call_details + ), + custom_logger=callback, + ), response_obj=result, start_time=start_time, end_time=end_time, diff --git a/litellm/litellm_core_utils/redact_messages.py b/litellm/litellm_core_utils/redact_messages.py index 7cfa1a6b244..43d3bb9562f 100644 --- a/litellm/litellm_core_utils/redact_messages.py +++ b/litellm/litellm_core_utils/redact_messages.py @@ -21,6 +21,7 @@ from litellm.constants import REDACTED_BY_LITELLM from litellm.integrations.custom_logger import CustomLogger from litellm.litellm_core_utils.classifier_logging import without_classifier_audit from litellm.litellm_core_utils.core_helpers import ( + as_object_list, attribute_or_none, get_metadata_variable_name_from_kwargs, has_attributes, @@ -33,7 +34,7 @@ from litellm.llms.vertex_ai.common_utils import ( ) from litellm.secret_managers.main import str_to_bool from litellm.types.router import BaselineRouteStamp -from litellm.types.utils import StandardCallbackDynamicParams +from litellm.types.utils import PROMPT_CARRYING_GUARDRAIL_FIELDS, StandardCallbackDynamicParams from litellm.types.vector_stores import RETRIEVAL_QUERY_PARAMS if TYPE_CHECKING: @@ -249,6 +250,42 @@ def _as_mapping(value: object) -> Mapping[str, object] | None: return value if isinstance(value, Mapping) else None # pyright: ignore[reportUnknownVariableType] # keys are read one by one +_NUMERIC_COMPRESSION_STAT_KEYS: Final = frozenset( + {"tokens_before", "tokens_after", "tokens_saved", "compression_ratio"} +) + + +def numeric_compression_stats(guardrail_entry: Mapping[str, object]) -> dict[str, int | float] | None: + fields: Final = _as_mapping(guardrail_entry.get("guardrail_response")) + if fields is None: + return None + stats: Final = { + key: value + for key, value in fields.items() + if key in _NUMERIC_COMPRESSION_STAT_KEYS and isinstance(value, (int, float)) and not isinstance(value, bool) + } + return stats or None + + +def _guardrail_entry_without_prompt_text(entry: object) -> object: + fields: Final = _as_mapping(entry) + if fields is None: + return entry + redacted: Final = { + field: REDACTED_BY_LITELLM if field in PROMPT_CARRYING_GUARDRAIL_FIELDS and value is not None else value + for field, value in fields.items() + } + stats: Final = numeric_compression_stats(fields) + return redacted if stats is None else {**redacted, "guardrail_response": stats} + + +def _guardrail_information_without_prompt_text(guardrail_information: object) -> object: + entries: Final = as_object_list(guardrail_information) + if entries is None: + return _guardrail_entry_without_prompt_text(guardrail_information) + return [_guardrail_entry_without_prompt_text(entry) for entry in entries] + + def _redact_provider_specific_fields(message: object) -> None: if (fields := _as_mapping(getattr(message, "provider_specific_fields", None))) is not None: setattr(message, "provider_specific_fields", _redacted_provider_specific_fields(fields)) # noqa: B010 # typed as object @@ -502,32 +539,34 @@ def _redacted_responses_api_response( def redacted_standard_logging_payload(payload: Mapping[str, object]) -> Mapping[str, object]: """ - Return a copy of a ``StandardLoggingPayload`` with its messages and response redacted. - - The success path redacts through ``perform_redaction`` before a callback ever sees the - payload, but the failure path does not, so a callback that batches both has to redact - the ones it is handed. + Return a copy of a ``StandardLoggingPayload`` with its messages, response and guardrail + prompt text redacted. """ return _redact_standard_logging_object(payload) def _redact_standard_logging_object( - payload: Mapping[str, object], *, redact_instructions: bool = True + payload: Mapping[str, object], *, redact_shared_fields: bool = True ) -> dict[str, object]: standard_logging_object: Final = copy.deepcopy(without_classifier_audit(payload)) redacted_str: Final = REDACTED_BY_LITELLM if standard_logging_object.get("messages") is not None: standard_logging_object["messages"] = [{"role": "user", "content": redacted_str}] + guardrail_information: Final = standard_logging_object.get("guardrail_information") + if guardrail_information is not None and redact_shared_fields: + standard_logging_object["guardrail_information"] = _guardrail_information_without_prompt_text( + guardrail_information + ) model_parameters: Final = _as_mapping(standard_logging_object.get("model_parameters")) - if model_parameters is not None and redact_instructions: + if model_parameters is not None and redact_shared_fields: standard_logging_object["model_parameters"] = _with_redacted_request_fields(model_parameters) response: Final = standard_logging_object.get("response") if response is not None: if isinstance(response, dict) and "output" in response: standard_logging_object["response"] = _redacted_responses_api_response( - response, redact_instructions=redact_instructions + response, redact_instructions=redact_shared_fields ) redact_vertex_ai_metadata_from_logged_object(standard_logging_object["response"]) elif isinstance(response, dict) and "choices" in response: @@ -639,8 +678,8 @@ def perform_redaction(model_call_details: dict[str, object], result, redact_stre redact_streaming_responses=False skips the in-place redaction of the shared streaming response entries; per-callback redaction hands each opted-out callback its own redacted copy via redacted_model_call_details_for_custom_logger instead. It also leaves the shared - request instructions, proxy request body and hidden provider fields as they are, since the - spend log and the other callbacks read them. + request instructions, guardrail prompt text, proxy request body and hidden provider fields + as they are, since the spend log and the other callbacks read them. """ # Redact model_call_details params: Final = _as_mapping(model_call_details.get("litellm_params")) @@ -665,7 +704,7 @@ def perform_redaction(model_call_details: dict[str, object], result, redact_stre standard_logging_object: Final = model_call_details.get("standard_logging_object") if isinstance(standard_logging_object, Mapping): model_call_details["standard_logging_object"] = _redact_standard_logging_object( - standard_logging_object, redact_instructions=redact_streaming_responses + standard_logging_object, redact_shared_fields=redact_streaming_responses ) redact_vertex_ai_metadata_from_litellm_params(model_call_details) diff --git a/litellm/proxy/guardrails/guardrail_endpoints.py b/litellm/proxy/guardrails/guardrail_endpoints.py index f6591ca00d5..6045114c773 100644 --- a/litellm/proxy/guardrails/guardrail_endpoints.py +++ b/litellm/proxy/guardrails/guardrail_endpoints.py @@ -18,7 +18,13 @@ from pydantic import BaseModel, TypeAdapter, ValidationError from litellm._logging import verbose_proxy_logger from litellm.constants import DEFAULT_MAX_RECURSE_DEPTH -from litellm.integrations.custom_guardrail import CustomGuardrail, GuardrailSetupError +from litellm.integrations.custom_guardrail import ( + CustomGuardrail, + GuardrailSetupError, + _append_slg_to_litellm_params, # pyright: ignore[reportPrivateUsage, reportUnknownVariableType] # share the guardrail logging bridge, which takes a bare list + _sync_guardrail_info_to_logging_obj, # pyright: ignore[reportPrivateUsage, reportUnknownVariableType] # share the guardrail logging bridge, which takes a bare dict +) +from litellm.litellm_core_utils.core_helpers import as_object_list, as_str_mapping from litellm.litellm_core_utils.safe_json_dumps import safe_dumps from litellm.proxy._types import AUDIT_ACTIONS, LitellmTableNames, LitellmUserRoles, UserAPIKeyAuth from litellm.proxy.auth.user_api_key_auth import user_api_key_auth @@ -2528,6 +2534,19 @@ async def _guardrail_response_after_success_hook( return response +def _record_verdict_for_logging( + request_data: dict[str, object], # mutable-ok: the logging bridge takes the request dict itself + data: dict[str, object], # mutable-ok: the logged request receives the guardrail's verdict + litellm_logging_obj: object, +) -> None: + metadata: Final = as_str_mapping(request_data.get("metadata")) or {} + records: Final = as_object_list(metadata.get("standard_logging_guardrail_information")) + if not records: + return + _append_slg_to_litellm_params(data, list(records)) + _sync_guardrail_info_to_logging_obj(request_data, litellm_logging_obj) + + async def _emit_guardrail_success_logs( response_handler: StandardGuardrailResponseHandler, proxy_logging_obj: _GuardrailProxyLogging, @@ -2586,7 +2605,11 @@ async def apply_guardrail( proxy_logging_obj, version, ) - from litellm.proxy.utils import handle_exception_on_proxy + from litellm.proxy.utils import ( + GUARDRAIL_ENDPOINT_FAILURE, + GUARDRAIL_ENDPOINT_FAILURE_KEY, + handle_exception_on_proxy, + ) data: dict[str, object] = { "guardrail_name": request.guardrail_name, @@ -2631,14 +2654,18 @@ async def apply_guardrail( response_handler: Final = GUARDRAIL_RESPONSE_HANDLERS[request.response_format] request_data: Final = dict(response_handler.prepare_request_data(request)) input_type: Final = response_handler.resolve_input_type(request, active_guardrail) - guardrailed_inputs: Final = await active_guardrail.apply_guardrail( - inputs={"texts": [request.text]}, - request_data=request_data, - input_type=input_type, - ) + try: + guardrailed_inputs: Final = await active_guardrail.apply_guardrail( + inputs={"texts": [request.text]}, + request_data=request_data, + input_type=input_type, + ) + finally: + _record_verdict_for_logging(request_data, data, litellm_logging_obj) response: Final = response_handler.build_response(request, guardrailed_inputs, request_data) except Exception as e: - if litellm_logging_obj is not None and not isinstance(e, HTTPException): + if litellm_logging_obj is not None: + data[GUARDRAIL_ENDPOINT_FAILURE_KEY] = GUARDRAIL_ENDPOINT_FAILURE try: await litellm_logging_obj.async_failure_handler( exception=e, diff --git a/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py b/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py index c3a979ad1bc..8df2d145573 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py +++ b/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py @@ -1385,7 +1385,7 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM): self.add_standard_logging_guardrail_information_to_request_data( guardrail_provider=self.guardrail_provider, guardrail_json_response=logged_json_response, - request_data=request_data or {}, + request_data=request_data if request_data is not None else {}, guardrail_status=self._get_bedrock_guardrail_response_status(response=httpx_response), start_time=start_time.timestamp(), end_time=datetime.now(timezone.utc).timestamp(), @@ -1415,7 +1415,7 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM): self.add_standard_logging_guardrail_information_to_request_data( guardrail_provider=self.guardrail_provider, guardrail_json_response=dict(merged_response), - request_data=request_data or {}, + request_data=request_data if request_data is not None else {}, guardrail_status=( "guardrail_failed_to_respond" if "Exception" in str((merged_response.get("Output") or {}).get("__type", "")) @@ -1453,7 +1453,7 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM): self.add_standard_logging_guardrail_information_to_request_data( guardrail_provider=self.guardrail_provider, guardrail_json_response=json_response, - request_data=request_data or {}, + request_data=request_data if request_data is not None else {}, guardrail_status="guardrail_failed_to_respond", start_time=start_time.timestamp(), end_time=datetime.now(timezone.utc).timestamp(), @@ -1866,7 +1866,7 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM): self.add_standard_logging_guardrail_information_to_request_data( guardrail_provider=self.guardrail_provider, guardrail_json_response={"error": detail_message}, - request_data=request_data or {}, + request_data=request_data if request_data is not None else {}, guardrail_status="guardrail_failed_to_respond", start_time=start_time.timestamp(), end_time=datetime.now(timezone.utc).timestamp(), @@ -1881,7 +1881,7 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM): self.add_standard_logging_guardrail_information_to_request_data( guardrail_provider=self.guardrail_provider, guardrail_json_response={"error": str(e)}, - request_data=request_data or {}, + request_data=request_data if request_data is not None else {}, guardrail_status="guardrail_failed_to_respond", start_time=start_time.timestamp(), end_time=datetime.now(timezone.utc).timestamp(), @@ -2009,7 +2009,7 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM): self.add_standard_logging_guardrail_information_to_request_data( guardrail_provider=self.guardrail_provider, guardrail_json_response={"error": detail_message}, - request_data=request_data or {}, + request_data=request_data if request_data is not None else {}, guardrail_status="guardrail_failed_to_respond", start_time=start_time.timestamp(), end_time=datetime.now(timezone.utc).timestamp(), @@ -2025,7 +2025,7 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM): self.add_standard_logging_guardrail_information_to_request_data( guardrail_provider=self.guardrail_provider, guardrail_json_response={"error": str(e)}, - request_data=request_data or {}, + request_data=request_data if request_data is not None else {}, guardrail_status="guardrail_failed_to_respond", start_time=start_time.timestamp(), end_time=datetime.now(timezone.utc).timestamp(), @@ -2043,7 +2043,7 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM): self.add_standard_logging_guardrail_information_to_request_data( guardrail_provider=self.guardrail_provider, guardrail_json_response=self._sanitize_invoke_checks_response_for_logging(json_response), - request_data=request_data or {}, + request_data=request_data if request_data is not None else {}, guardrail_status=self._get_invoke_checks_status(bool(violations)), start_time=start_time.timestamp(), end_time=datetime.now(timezone.utc).timestamp(), diff --git a/litellm/proxy/guardrails/guardrail_hooks/grayswan/grayswan.py b/litellm/proxy/guardrails/guardrail_hooks/grayswan/grayswan.py index 6d4fb30dede..d7de7d42673 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/grayswan/grayswan.py +++ b/litellm/proxy/guardrails/guardrail_hooks/grayswan/grayswan.py @@ -341,7 +341,7 @@ class GraySwanGuardrail(CustomGuardrail): status_code: Final = getattr(exc, "status_code", None) or getattr(exc, "exception_status_code", None) self._log_guardrail_failure( exc=exc, - request_data=request_data or {}, + request_data=request_data, start_time=start_time, end_time=end_time, status_code=status_code, diff --git a/litellm/proxy/guardrails/guardrail_hooks/presidio.py b/litellm/proxy/guardrails/guardrail_hooks/presidio.py index 1e4621622b3..7e881dd87bb 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/presidio.py +++ b/litellm/proxy/guardrails/guardrail_hooks/presidio.py @@ -1831,7 +1831,7 @@ class _OPTIONAL_PresidioPIIMasking(CustomGuardrail): text=text, output_parse_pii=self.output_parse_pii, presidio_config=None, - request_data=request_data or {}, + request_data=request_data, ) new_texts.append(modified_text) inputs["texts"] = new_texts diff --git a/litellm/proxy/guardrails/response_handlers.py b/litellm/proxy/guardrails/response_handlers.py index 752379daecf..b15ef77c6af 100644 --- a/litellm/proxy/guardrails/response_handlers.py +++ b/litellm/proxy/guardrails/response_handlers.py @@ -27,7 +27,11 @@ class StandardGuardrailResponseHandler: """Preserve the endpoint's existing text response and hook behavior.""" def prepare_request_data(self, request: ApplyGuardrailRequest) -> Mapping[str, object]: - return request.model_dump(include={"messages", "metadata"}, exclude_none=True) + return request.model_dump( + include={"messages", "metadata"}, + exclude={"metadata": {"standard_logging_guardrail_information"}}, + exclude_none=True, + ) def resolve_input_type( self, request: ApplyGuardrailRequest, guardrail: CustomGuardrail @@ -65,13 +69,6 @@ class _NativeGuardrailMetadata(BaseModel): class NativeGuardrailResponseHandler(StandardGuardrailResponseHandler): """Return structured findings recorded by this guardrail invocation.""" - def prepare_request_data(self, request: ApplyGuardrailRequest) -> Mapping[str, object]: - return request.model_dump( - include={"messages", "metadata"}, - exclude={"metadata": {"standard_logging_guardrail_information"}}, - exclude_none=True, - ) - def resolve_input_type( self, request: ApplyGuardrailRequest, guardrail: CustomGuardrail ) -> Literal["request", "response"]: diff --git a/litellm/proxy/hooks/proxy_track_cost_callback.py b/litellm/proxy/hooks/proxy_track_cost_callback.py index 3afe66754e9..7f37f8cb050 100644 --- a/litellm/proxy/hooks/proxy_track_cost_callback.py +++ b/litellm/proxy/hooks/proxy_track_cost_callback.py @@ -72,6 +72,8 @@ from litellm.proxy.spend_tracking.spend_tracking_utils import ( # noqa: F401 # should_store_prompts_for_call, ) from litellm.proxy.utils import ( + GUARDRAIL_ENDPOINT_FAILURE, + GUARDRAIL_ENDPOINT_FAILURE_KEY, REJECTED_BEFORE_REQUEST_SETUP, REJECTED_BEFORE_REQUEST_SETUP_KEY, BilledFailedAttempt, @@ -493,6 +495,7 @@ class _ProxyDBLogger(CustomLogger): return request_route: Final = user_api_key_dict.request_route + request_fields: Final = _str_keyed_fields(request_data) from litellm.proxy.pass_through_endpoints.pass_through_endpoints import InitPassThroughEndpointHelpers route_is_trackable: Final = ( @@ -500,6 +503,7 @@ class _ProxyDBLogger(CustomLogger): or RouteChecks.runs_inference_checks(request_route) or RouteChecks.is_info_route(route=request_route) or InitPassThroughEndpointHelpers.has_configured_route_ignoring_trailing_slash(route=request_route) + or request_fields.get(GUARDRAIL_ENDPOINT_FAILURE_KEY) is GUARDRAIL_ENDPOINT_FAILURE ) if ( ProxyDBLogger._should_track_errors_in_db() is False @@ -537,7 +541,6 @@ class _ProxyDBLogger(CustomLogger): ) ) - request_fields: Final = _str_keyed_fields(request_data) _metadata["used_client_oauth_token"] = _proxy_stamped_used_client_oauth_token(request_fields, request_route) existing_metadata: Final[dict] = request_data.get("metadata", None) or {} diff --git a/litellm/proxy/management_endpoints/policy_endpoints/endpoints.py b/litellm/proxy/management_endpoints/policy_endpoints/endpoints.py index 1effbbd8e0c..5267f3b6a86 100644 --- a/litellm/proxy/management_endpoints/policy_endpoints/endpoints.py +++ b/litellm/proxy/management_endpoints/policy_endpoints/endpoints.py @@ -334,7 +334,7 @@ async def test_policies_and_guardrails( item_result = await apply_policies( policy_names=data.policy_names, inputs=inp, - request_data=data.request_data, + request_data=copy.deepcopy(data.request_data), input_type=data.input_type, proxy_logging_obj=logging_obj, guardrail_names=data.guardrail_names, diff --git a/litellm/proxy/spend_tracking/spend_tracking_utils.py b/litellm/proxy/spend_tracking/spend_tracking_utils.py index a4c6d8a40c7..5321e9e598b 100644 --- a/litellm/proxy/spend_tracking/spend_tracking_utils.py +++ b/litellm/proxy/spend_tracking/spend_tracking_utils.py @@ -72,6 +72,7 @@ from litellm.litellm_core_utils.litellm_logging import ( ) from litellm.litellm_core_utils.prompt_templates.mid_conversation_system import message_field, parts_of from litellm.litellm_core_utils.ptu_pricing import azure_spillover +from litellm.litellm_core_utils.redact_messages import numeric_compression_stats from litellm.litellm_core_utils.safe_json_dumps import safe_dumps, strip_null_bytes from litellm.litellm_core_utils.secret_redaction import redact_tokens from litellm.litellm_core_utils.sensitive_data_masker import SensitiveDataMasker @@ -1790,27 +1791,6 @@ def _sanitize_guardrail_information_for_spend_logs( return [_redact_prompt_fields_in_guardrail_entry(entry) for entry in entries if isinstance(entry, dict)] -_NUMERIC_COMPRESSION_STAT_KEYS: Final = ( - "tokens_before", - "tokens_after", - "tokens_saved", - "compression_ratio", -) - - -def _numeric_compression_stats_from_guardrail_response( - guardrail_response: object, -) -> dict[str, int | float] | None: - if not isinstance(guardrail_response, dict): - return None - stats: Final = { - key: value - for key, value in guardrail_response.items() - if key in _NUMERIC_COMPRESSION_STAT_KEYS and isinstance(value, (int, float)) and not isinstance(value, bool) - } - return stats or None - - def _redact_prompt_fields_in_guardrail_entry( entry: StandardLoggingGuardrailInformation, ) -> StandardLoggingGuardrailInformation: @@ -1821,7 +1801,7 @@ def _redact_prompt_fields_in_guardrail_entry( stats-only dict; spend aggregation reads them via ``extract_compression_saved_tokens``. """ - preserved_stats: Final = _numeric_compression_stats_from_guardrail_response(entry.get("guardrail_response")) + preserved_stats: Final = numeric_compression_stats(entry) redacted: Final[StandardLoggingGuardrailInformation] = { **entry, **{key: REDACTED_BY_LITELM_STRING for key in PROMPT_CARRYING_GUARDRAIL_FIELDS if key in entry}, diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.py index 34a0e6dfe6c..8104606db60 100644 --- a/litellm/proxy/utils.py +++ b/litellm/proxy/utils.py @@ -1833,6 +1833,11 @@ object, meaning the request died (auth, body parsing, model resolution) before t object rather than ``True`` because ``request_data`` is the client body: JSON cannot forge this identity, so a client can neither hide a guardrail block nor force a row by sending the key or a ``litellm_call_id`` itself.""" +GUARDRAIL_ENDPOINT_FAILURE_KEY: Final = "litellm_guardrail_endpoint_failure" +GUARDRAIL_ENDPOINT_FAILURE: Final = object() +"""Stored under ``GUARDRAIL_ENDPOINT_FAILURE_KEY`` by ``/apply_guardrail`` when a request it had set up fails, so the +spend logs keep that failure although the route is not an LLM API route. A sentinel for the same reason as above.""" + _PROXY_ONLY_LLM_API_ERRORS: Final = (HTTPException, ProxyException, GuardrailRaisedException) _PROXY_ONLY_ERROR_LOGGED_FIELDS: Final = frozenset( {"model", "messages", "input", "prompt", "user", "stream", "litellm_call_id"} diff --git a/tests/unit/integrations/test_custom_guardrail.py b/tests/unit/integrations/test_custom_guardrail.py index c32c1b71f75..e88347fb4f7 100644 --- a/tests/unit/integrations/test_custom_guardrail.py +++ b/tests/unit/integrations/test_custom_guardrail.py @@ -2086,6 +2086,59 @@ class TestGuardrailInterventionClassification: slg = request_data["metadata"]["standard_logging_guardrail_information"][0] assert slg["guardrail_status"] == "guardrail_intervened" + @pytest.mark.asyncio + async def test_block_is_recorded_on_a_request_dict_that_starts_empty(self) -> None: + from fastapi.exceptions import HTTPException + + from litellm.integrations.custom_guardrail import log_guardrail_information + + class BlockingGuardrail(CustomGuardrail): + @log_guardrail_information + async def apply_guardrail( + self, + inputs: GenericGuardrailAPIInputs, + request_data: dict, + input_type: Literal["request", "response"], + logging_obj: Optional["LiteLLMLoggingObj"] = None, + ) -> GenericGuardrailAPIInputs: + raise HTTPException(status_code=400, detail="blocked by guardrail") + + request_data: dict = {} + + with pytest.raises(HTTPException): + await BlockingGuardrail(guardrail_name="block-rail").apply_guardrail( + inputs={"texts": ["hello"]}, request_data=request_data, input_type="request" + ) + + recorded = request_data["metadata"]["standard_logging_guardrail_information"] + assert [record["guardrail_status"] for record in recorded] == ["guardrail_intervened"] + + def test_sync_block_is_recorded_on_a_request_dict_that_starts_empty(self) -> None: + from fastapi.exceptions import HTTPException + + from litellm.integrations.custom_guardrail import log_guardrail_information + + class SyncBlockingGuardrail(CustomGuardrail): + @log_guardrail_information + def apply_guardrail( + self, + inputs: GenericGuardrailAPIInputs, + request_data: dict, + input_type: Literal["request", "response"], + logging_obj: Optional["LiteLLMLoggingObj"] = None, + ) -> GenericGuardrailAPIInputs: + raise HTTPException(status_code=400, detail="blocked by guardrail") + + request_data: Final[dict[str, object]] = {} + + with pytest.raises(HTTPException): + SyncBlockingGuardrail(guardrail_name="block-rail").apply_guardrail( + inputs={"texts": ["hello"]}, request_data=request_data, input_type="request" + ) + + recorded: Final = request_data["metadata"]["standard_logging_guardrail_information"] + assert [record["guardrail_status"] for record in recorded] == ["guardrail_intervened"] + class _ApplyStyleGuardrail(CustomGuardrail): """Overrides only apply_guardrail, like openai_moderation; async_pre_call_hook stays the CustomLogger no-op.""" diff --git a/tests/unit/litellm_core_utils/test_redact_messages.py b/tests/unit/litellm_core_utils/test_redact_messages.py index ee7ae89679e..665ca31648c 100644 --- a/tests/unit/litellm_core_utils/test_redact_messages.py +++ b/tests/unit/litellm_core_utils/test_redact_messages.py @@ -37,6 +37,7 @@ from litellm.litellm_core_utils.redact_messages import ( should_redact_message_logging, ) from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler, HTTPHandler +from litellm.proxy.spend_tracking.compression_savings import extract_compression_saved_tokens from litellm.responses.main import mock_responses_api_response from litellm.types.responses.main import build_web_search_call from litellm.types.router import BaselineRouteStamp @@ -2434,3 +2435,63 @@ def test_perform_redaction_strips_the_matched_text_from_the_bedrock_guardrail_tr assert "US_SSN" in redacted.model_dump_json(), "the finding stays, only its matched text goes" assert "123-45-6789" not in str(redacted_dict) assert "123-45-6789" in response.model_dump_json(), "the caller's own response is not redacted" + + +_GUARDRAIL_RECORD: Final = MappingProxyType( + { + "guardrail_name": "bedrock-guard", + "guardrail_status": "guardrail_intervened", + "guardrail_request": {"texts": ["my card is 4111 1111 1111 1111"]}, + "guardrail_response": {"outputs": [{"text": "my card is 4111 1111 1111 1111"}]}, + "match_details": [{"snippet": "4111 1111 1111 1111"}], + "classification": {"reason": "card 4111 1111 1111 1111"}, + "guardrail_cost": 0.00075, + "duration": 0.4, + } +) + + +@pytest.mark.parametrize( + "guardrail_information", + [[dict(_GUARDRAIL_RECORD)], dict(_GUARDRAIL_RECORD)], + ids=["list", "bare_record"], +) +def test_redaction_keeps_the_guardrail_verdict_and_drops_the_prompt_it_quotes( + guardrail_information: object, +) -> None: + payload: Final = {"messages": [{"role": "user", "content": "x"}], "guardrail_information": guardrail_information} + + redacted: Final = redacted_standard_logging_payload(payload)["guardrail_information"] + + records: Final = redacted if isinstance(redacted, list) else [redacted] + assert "4111" not in str(records) + assert [ + (record["guardrail_name"], record["guardrail_status"], record["guardrail_cost"], record["duration"]) + for record in records + ] == [("bedrock-guard", "guardrail_intervened", 0.00075, 0.4)] + assert "4111" in str(payload["guardrail_information"]), "the caller's payload is not redacted in place" + + +def test_redaction_keeps_the_compression_savings_a_guardrail_reports() -> None: + payload: Final = { + "messages": [{"role": "user", "content": "x"}], + "guardrail_information": [ + { + "guardrail_name": "headroom", + "guardrail_provider": "headroom", + "guardrail_status": "success", + "guardrail_response": { + "tokens_before": 1000, + "tokens_after": 400, + "tokens_saved": 600, + "compression_ratio": 0.4, + "compressed_text": "my card is 4111 1111 1111 1111", + }, + } + ], + } + + redacted: Final = redacted_standard_logging_payload(payload) + + assert "4111" not in str(redacted["guardrail_information"]) + assert extract_compression_saved_tokens(redacted) == extract_compression_saved_tokens(payload) == 600 diff --git a/tests/unit/proxy/guardrails/guardrail_hooks/test_bedrock_guardrails.py b/tests/unit/proxy/guardrails/guardrail_hooks/test_bedrock_guardrails.py index 1fb56509b7f..dc803b3bd8c 100644 --- a/tests/unit/proxy/guardrails/guardrail_hooks/test_bedrock_guardrails.py +++ b/tests/unit/proxy/guardrails/guardrail_hooks/test_bedrock_guardrails.py @@ -2,9 +2,11 @@ Unit tests for Bedrock Guardrails """ +import contextlib import json import asyncio import logging +from collections.abc import Callable, Mapping from typing import Final from datetime import datetime, timezone import sys @@ -20,6 +22,7 @@ import litellm from litellm._logging import verbose_proxy_logger from litellm.caching.caching import DualCache from litellm.exceptions import ModifyResponseException +from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler from litellm.proxy._types import UserAPIKeyAuth from litellm.constants import BEDROCK_APPLY_GUARDRAIL_CHUNK_BUDGET_CHARS from litellm.proxy.guardrails.guardrail_hooks.bedrock_guardrails import ( @@ -8168,3 +8171,94 @@ async def test_guardrail_secret_lookups_leave_the_event_loop_free( assert vault.read.is_set() assert not vault.read_on_loop.is_set() assert upstream.calls.last.request.url.host == "bedrock-runtime.us-west-2.amazonaws.com" + + +_APPLY_GUARDRAIL_API: Final = {"guardrailIdentifier": "gr-1", "guardrailVersion": "1"} +_GUARDRAIL_CHECKS_API: Final = { + "checks": {"contentFilter": {"categories": [{"category": "VIOLENCE"}]}}, + "content_filter_threshold": 0.5, +} +_APPLY_GUARDRAIL_BLOCKED: Final = { + "action": "GUARDRAIL_INTERVENED", + "outputs": [{"text": "blocked"}], + "assessments": [{"contentPolicy": {"filters": [{"type": "VIOLENCE", "confidence": "HIGH", "action": "BLOCKED"}]}}], +} + + +def _answering(status_code: int, body: object) -> Callable[[httpx.Request], httpx.Response]: + return lambda request: httpx.Response(status_code, json=body) + + +def _violence_scored(severity: float) -> Callable[[httpx.Request], httpx.Response]: + return _answering( + 200, {"results": {"contentFilter": {"results": [{"category": "VIOLENCE", "severityScore": severity}]}}} + ) + + +def _unreachable(request: httpx.Request) -> httpx.Response: + raise httpx.ConnectError("connection refused", request=request) + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + ("guardrail_config", "bedrock", "recorded_status"), + [ + pytest.param( + _APPLY_GUARDRAIL_API, + _answering(200, {"action": "NONE", "outputs": [], "assessments": []}), + "success", + id="apply_guardrail-allowed", + ), + pytest.param( + _APPLY_GUARDRAIL_API, + _answering(200, _APPLY_GUARDRAIL_BLOCKED), + "guardrail_intervened", + id="apply_guardrail-blocked", + ), + pytest.param( + _APPLY_GUARDRAIL_API, + _answering(500, {"message": "internal failure"}), + "guardrail_failed_to_respond", + id="apply_guardrail-error", + ), + pytest.param( + _APPLY_GUARDRAIL_API, _unreachable, "guardrail_failed_to_respond", id="apply_guardrail-unreachable" + ), + pytest.param(_GUARDRAIL_CHECKS_API, _violence_scored(0.1), "success", id="guardrail_checks-allowed"), + pytest.param( + _GUARDRAIL_CHECKS_API, _violence_scored(0.9), "guardrail_intervened", id="guardrail_checks-blocked" + ), + pytest.param( + _GUARDRAIL_CHECKS_API, + _answering(500, {"message": "internal failure"}), + "guardrail_failed_to_respond", + id="guardrail_checks-error", + ), + pytest.param( + _GUARDRAIL_CHECKS_API, + _answering(200, ["unexpected"]), + "guardrail_failed_to_respond", + id="guardrail_checks-unparseable", + ), + ], +) +async def test_verdict_is_recorded_on_a_request_dict_that_starts_empty( + guardrail_config: Mapping[str, object], + bedrock: Callable[[httpx.Request], httpx.Response], + recorded_status: str, +) -> None: + guardrail: Final = BedrockGuardrail( + guardrail_name="bedrock-guard", + aws_region_name="us-east-1", + aws_access_key_id="test-access-key", + aws_secret_access_key="test-secret-key", + async_handler=AsyncHTTPHandler(transport=httpx.MockTransport(bedrock)), + **guardrail_config, + ) + request_data: Final[dict[str, object]] = {} + + with contextlib.suppress(Exception): + await guardrail.apply_guardrail(inputs={"texts": ["hello"]}, request_data=request_data, input_type="request") + + recorded: Final = request_data["metadata"]["standard_logging_guardrail_information"] + assert [record["guardrail_status"] for record in recorded] == [recorded_status] diff --git a/tests/unit/proxy/guardrails/guardrail_hooks/test_grayswan.py b/tests/unit/proxy/guardrails/guardrail_hooks/test_grayswan.py index a5ba9dab0bd..cb9c9fafac3 100644 --- a/tests/unit/proxy/guardrails/guardrail_hooks/test_grayswan.py +++ b/tests/unit/proxy/guardrails/guardrail_hooks/test_grayswan.py @@ -1,10 +1,14 @@ +import contextlib from collections.abc import Mapping from types import MappingProxyType +from typing import Final +import httpx import pytest from fastapi import HTTPException from litellm.integrations.custom_guardrail import ModifyResponseException +from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler from litellm.llms.openai.completion.guardrail_translation.handler import OpenAITextCompletionHandler from litellm.proxy._types import UserAPIKeyAuth from litellm.proxy.guardrails.guardrail_hooks.grayswan import grayswan as grayswan_module @@ -912,3 +916,25 @@ async def test_post_call_never_forwards_the_proxy_key_metadata_of_the_request_me ) assert "litellm_metadata" not in client.calls[0]["json"] + + +@pytest.mark.asyncio +@pytest.mark.parametrize("fail_open", [True, False]) +async def test_unreachable_api_failure_is_recorded_on_a_request_dict_that_starts_empty(fail_open: bool) -> None: + def refuse_connection(request: httpx.Request) -> httpx.Response: + raise httpx.ConnectError("connection refused", request=request) + + guardrail: Final = GraySwanGuardrail( + guardrail_name="grayswan-unreachable", + api_key="test-key", + fail_open=fail_open, + event_hook=GuardrailEventHooks.pre_call, + ) + guardrail.async_handler = AsyncHTTPHandler(transport=httpx.MockTransport(refuse_connection)) + request_data: Final[dict[str, object]] = {} + + with contextlib.suppress(Exception): + await guardrail.apply_guardrail(inputs={"texts": ["hello"]}, request_data=request_data, input_type="request") + + recorded: Final = request_data["metadata"]["standard_logging_guardrail_information"] + assert [record["guardrail_status"] for record in recorded] == ["guardrail_failed_to_respond"] diff --git a/tests/unit/proxy/guardrails/guardrail_hooks/test_presidio.py b/tests/unit/proxy/guardrails/guardrail_hooks/test_presidio.py index cc0d2a8008f..2a1b1024fa5 100644 --- a/tests/unit/proxy/guardrails/guardrail_hooks/test_presidio.py +++ b/tests/unit/proxy/guardrails/guardrail_hooks/test_presidio.py @@ -5480,3 +5480,18 @@ def test_get_session_iterator_reuses_one_session_per_background_loop( assert len(sessions) == 10 assert all(session is sessions[0] for session in sessions) assert presidio_guardrail._http_session is None + + +@pytest.mark.asyncio +async def test_apply_guardrail_records_its_verdict_on_a_request_dict_that_starts_empty() -> None: + guardrail: Final = OPTIONAL_PresidioPIIMasking( + guardrail_name="presidio-pii", + mock_testing=True, + mock_redacted_text={"text": "my name is "}, + ) + request_data: Final[dict[str, object]] = {} + + await guardrail.apply_guardrail(inputs={"texts": ["my name is Jane"]}, request_data=request_data, input_type="request") + + recorded: Final = request_data["metadata"]["standard_logging_guardrail_information"] + assert [record["guardrail_name"] for record in recorded] == ["presidio-pii"] diff --git a/tests/unit/proxy/guardrails/test_guardrail_endpoints.py b/tests/unit/proxy/guardrails/test_guardrail_endpoints.py index cb11acc99cb..ba17e54e75a 100644 --- a/tests/unit/proxy/guardrails/test_guardrail_endpoints.py +++ b/tests/unit/proxy/guardrails/test_guardrail_endpoints.py @@ -1,10 +1,11 @@ import json import time from collections.abc import Awaitable, Callable, Mapping, Sequence -from datetime import datetime +from functools import partial +from datetime import datetime, timezone from types import MappingProxyType -from typing import Final, List, Literal, Optional -from unittest.mock import AsyncMock, Mock +from typing import Dict, Final, List, Literal, NamedTuple, Optional +from unittest.mock import AsyncMock, Mock, create_autospec import httpx import pytest @@ -13,9 +14,14 @@ from fastapi import FastAPI, HTTPException from pydantic import JsonValue from pytest_mock import MockerFixture +import litellm +from litellm.caching.dual_cache import DualCache from litellm.integrations.custom_guardrail import CustomGuardrail +from litellm.integrations.custom_logger import CustomLogger from litellm.litellm_core_utils.litellm_logging import Logging +from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler from litellm.proxy._types import LitellmUserRoles, ProxyException, UserAPIKeyAuth +from litellm.proxy.db.db_spend_update_writer import DBSpendUpdateWriter from litellm.proxy.guardrails.content_filter_data import DATA_ROOTS from litellm.proxy.guardrails.guardrail_endpoints import ( CreateGuardrailRequest, @@ -44,7 +50,9 @@ from litellm.proxy.guardrails.guardrail_endpoints import ( test_custom_code_guardrail as run_custom_code_test_endpoint, ) from litellm.proxy.guardrails.guardrail_hooks.azure.prompt_shield import AzureContentSafetyPromptShieldGuardrail +from litellm.proxy.guardrails.guardrail_hooks.bedrock_guardrails import BedrockGuardrail from litellm.proxy.guardrails.guardrail_hooks.litellm_content_filter.content_filter import ContentFilterGuardrail +from litellm.proxy.hooks.proxy_track_cost_callback import ProxyDBLogger MOCK_ADMIN_USER = UserAPIKeyAuth(user_role=LitellmUserRoles.PROXY_ADMIN) from litellm.proxy.guardrails.guardrail_registry import ( @@ -4204,3 +4212,237 @@ async def test_guardrail_success_delivers_sync_callback_once_with_overlapping_wo assert returned is response callback.assert_called_once() assert callback.call_args.args[0] == {"response": {"response_text": "masked"}} + + +_GUARDRAIL_RECORDS: Final = "standard_logging_guardrail_information" +_BEDROCK_TEXT_ALLOWED: Final = {"action": "NONE", "outputs": [], "assessments": []} +_BEDROCK_BLOCKED: Final = { + "action": "GUARDRAIL_INTERVENED", + "outputs": [{"text": "blocked"}], + "assessments": [{"contentPolicy": {"filters": [{"type": "VIOLENCE", "confidence": "HIGH", "action": "BLOCKED"}]}}], +} + + +class _BedrockGuardrailCall(NamedTuple): + response: httpx.Response + bedrock_payloads: Sequence[Mapping[str, object]] + logged: Mapping[str, object] + failure_hook_request: Mapping[str, object] + spend_rows: Sequence[Mapping[str, object]] + + +class _CapturingLogger(CustomLogger): + def __init__(self, message_logging: bool = True, legacy_message_logging: bool = True) -> None: + super().__init__(turn_off_message_logging=not message_logging, message_logging=legacy_message_logging) + self.logged: Final[list[Mapping[str, object]]] = [] + self.failure_hook_requests: Final[list[Mapping[str, object]]] = [] + + async def async_log_success_event( + self, kwargs: dict[str, object], response_obj: object, start_time: datetime, end_time: datetime + ) -> None: + self.logged.append(kwargs["standard_logging_object"]) + + async def async_log_failure_event( + self, kwargs: dict[str, object], response_obj: object, start_time: datetime, end_time: datetime + ) -> None: + self.logged.append(kwargs["standard_logging_object"]) + + async def async_post_call_failure_hook( + self, + request_data: dict, + original_exception: Exception, + user_api_key_dict: UserAPIKeyAuth, + traceback_str: str | None = None, + ) -> None: + self.failure_hook_requests.append(request_data) + + +def _bedrock_guard(handler: AsyncHTTPHandler, latest_user_message_only: bool = False) -> CustomGuardrail: + return BedrockGuardrail( + guardrail_name="bedrock-guard", + guardrailIdentifier="gr-1", + guardrailVersion="1", + aws_region_name="us-east-1", + aws_access_key_id="test-access-key", + aws_secret_access_key="test-secret-key", + async_handler=handler, + experimental_use_latest_role_message_only=latest_user_message_only, + event_hook=GuardrailEventHooks.pre_call, + ) + + +async def _apply_bedrock_guardrail_over_http( + mocker: MockerFixture, + bedrock_answers: Sequence[Mapping[str, object]], + body: Mapping[str, object], + guardrail: Callable[[AsyncHTTPHandler], CustomGuardrail] = _bedrock_guard, + capture_logger: Callable[[], _CapturingLogger] = _CapturingLogger, + headers: Mapping[str, str] = MappingProxyType({}), + other_loggers: Sequence[CustomLogger] = (), +) -> _BedrockGuardrailCall: + from litellm.proxy.auth.user_api_key_auth import user_api_key_auth + from litellm.proxy.guardrails.guardrail_endpoints import router + from litellm.proxy.proxy_server import ProxyConfig, openai_exception_handler + from litellm.proxy.utils import ProxyLogging + + sent: Final[list[httpx.Request]] = [] + + def bedrock(request: httpx.Request) -> httpx.Response: + sent.append(request) + return httpx.Response(200, json=bedrock_answers[len(sent) - 1]) + + capture: Final = capture_logger() + spend_writer: Final = create_autospec(DBSpendUpdateWriter, instance=True) + mocker.patch.object( + litellm, + "callbacks", + [ + guardrail(AsyncHTTPHandler(transport=httpx.MockTransport(bedrock))), + *other_loggers, + capture, + ProxyDBLogger(spend_writer=lambda: spend_writer), + ], + ) + mocker.patch.object(litellm, "_async_success_callback", []) + mocker.patch.object(litellm, "_async_failure_callback", []) + mocker.patch.object(litellm, "success_callback", []) + mocker.patch.object(litellm, "failure_callback", []) + mocker.patch("litellm.proxy.proxy_server.proxy_logging_obj", ProxyLogging(user_api_key_cache=DualCache())) + mocker.patch("litellm.proxy.proxy_server.general_settings", {}) + mocker.patch("litellm.proxy.proxy_server.proxy_config", ProxyConfig()) + mocker.patch("litellm.litellm_core_utils.thread_pool_executor.executor") + + app: Final = FastAPI() + app.include_router(router) + app.add_exception_handler(ProxyException, openai_exception_handler) + app.dependency_overrides[user_api_key_auth] = lambda: UserAPIKeyAuth( + api_key="test_api_key", request_route="/guardrails/apply_guardrail" + ) + async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://guardrails.test") as client: + response: Final = await client.post( + "/guardrails/apply_guardrail", json={"guardrail_name": "bedrock-guard", **body}, headers=dict(headers) + ) + assert len(capture.logged) == 1 + return _BedrockGuardrailCall( + response=response, + bedrock_payloads=tuple(json.loads(request.content) for request in sent), + logged=capture.logged[0], + failure_hook_request=capture.failure_hook_requests[0] if capture.failure_hook_requests else {}, + spend_rows=tuple(call.kwargs for call in spend_writer.update_database.await_args_list), + ) + + +def _logged_verdicts(call: _BedrockGuardrailCall) -> list[tuple[object, object]]: + return [(record["guardrail_name"], record["guardrail_status"]) for record in call.logged["guardrail_information"]] + + +@pytest.mark.asyncio +async def test_apply_guardrail_logs_a_blocked_request_with_the_guardrail_verdict(mocker: MockerFixture) -> None: + call: Final = await _apply_bedrock_guardrail_over_http(mocker, (_BEDROCK_BLOCKED,), {"text": "hello"}) + + assert call.response.status_code == 400 + assert call.logged["status"] == "failure" + assert _logged_verdicts(call) == [("bedrock-guard", "guardrail_intervened")] + assert [record["guardrail_status"] for record in call.failure_hook_request["metadata"][_GUARDRAIL_RECORDS]] == [ + "guardrail_intervened" + ] + assert [row["kwargs"]["litellm_params"]["metadata"]["status"] for row in call.spend_rows] == ["failure"] + assert [ + record["guardrail_status"] + for record in call.spend_rows[0]["kwargs"]["litellm_params"]["metadata"][_GUARDRAIL_RECORDS] + ] == ["guardrail_intervened"] + + +@pytest.mark.asyncio +async def test_apply_guardrail_logs_an_allowed_request_with_the_guardrail_verdict(mocker: MockerFixture) -> None: + call: Final = await _apply_bedrock_guardrail_over_http(mocker, (_BEDROCK_TEXT_ALLOWED,), {"text": "hello"}) + + assert call.response.status_code == 200 + assert call.logged["status"] == "success" + assert _logged_verdicts(call) == [("bedrock-guard", "success")] + + +@pytest.mark.asyncio +async def test_apply_guardrail_never_logs_a_verdict_the_caller_supplied(mocker: MockerFixture) -> None: + forged: Final = {"guardrail_name": "forged", "guardrail_status": "success"} + + call: Final = await _apply_bedrock_guardrail_over_http( + mocker, (_BEDROCK_TEXT_ALLOWED,), {"text": "hello", "metadata": {_GUARDRAIL_RECORDS: [forged]}} + ) + + assert _logged_verdicts(call) == [("bedrock-guard", "success")] + + +@pytest.mark.asyncio +@pytest.mark.parametrize("bedrock_action", ["ANONYMIZED", "BLOCKED"]) +@pytest.mark.parametrize( + ("capture_logger", "headers"), + [ + pytest.param( + partial(_CapturingLogger, message_logging=False), + MappingProxyType({}), + id="callback-turns-off-message-logging", + ), + pytest.param( + partial(_CapturingLogger, legacy_message_logging=False), + MappingProxyType({}), + id="callback-sets-message-logging-false", + ), + pytest.param(_CapturingLogger, MappingProxyType({"litellm-enable-message-redaction": "true"}), id="request-header"), + ], +) +async def test_apply_guardrail_keeps_the_prompt_out_of_the_verdict_a_redacting_callback_receives( + mocker: MockerFixture, bedrock_action: str, capture_logger: Callable[[], _CapturingLogger], headers: Mapping[str, str] +) -> None: + answer: Final = { + "action": "GUARDRAIL_INTERVENED", + "outputs": [{"text": "please bill my card {CREDIT_DEBIT_CARD_NUMBER}"}], + "assessments": [ + { + "sensitiveInformationPolicy": { + "piiEntities": [ + {"type": "CREDIT_DEBIT_CARD_NUMBER", "match": "4111111111111111", "action": bedrock_action} + ] + } + } + ], + } + + call: Final = await _apply_bedrock_guardrail_over_http( + mocker, + (answer,), + {"text": "please bill my card 4111111111111111"}, + capture_logger=capture_logger, + headers=headers, + ) + + assert [name for name, _ in _logged_verdicts(call)] == ["bedrock-guard"] + redacted_fields: Final = str({field: call.logged[field] for field in ("messages", "response", "guardrail_information")}) + assert "please bill my card" not in redacted_fields + assert "4111111111111111" not in redacted_fields + + +@pytest.mark.asyncio +async def test_one_callback_opting_out_of_message_logging_leaves_the_guardrail_details_to_the_others( + mocker: MockerFixture, +) -> None: + answer: Final = { + "action": "GUARDRAIL_INTERVENED", + "outputs": [{"text": "please bill my card {CREDIT_DEBIT_CARD_NUMBER}"}], + "assessments": [ + { + "sensitiveInformationPolicy": { + "piiEntities": [{"type": "CREDIT_DEBIT_CARD_NUMBER", "match": "4111111111111111", "action": "ANONYMIZED"}] + } + } + ], + } + opted_out: Final = _CapturingLogger(legacy_message_logging=False) + + call: Final = await _apply_bedrock_guardrail_over_http( + mocker, (answer,), {"text": "please bill my card 4111111111111111"}, other_loggers=(opted_out,) + ) + + assert "please bill my card" in str(call.logged["guardrail_information"]) + assert [record["guardrail_name"] for record in opted_out.logged[0]["guardrail_information"]] == ["bedrock-guard"] + assert "please bill my card" not in str(opted_out.logged[0]["guardrail_information"]) diff --git a/tests/unit/proxy/hooks/test_proxy_track_cost_callback.py b/tests/unit/proxy/hooks/test_proxy_track_cost_callback.py index c574c8cded4..1b5af737f1b 100644 --- a/tests/unit/proxy/hooks/test_proxy_track_cost_callback.py +++ b/tests/unit/proxy/hooks/test_proxy_track_cost_callback.py @@ -43,6 +43,8 @@ from litellm.proxy.spend_tracking.spend_event import SpendEventDecodeError, buil from litellm.proxy.spend_tracking.spend_event_producer import SpendEventProducer, UnixAddress from litellm.proxy.spend_tracking.spend_tracking_utils import get_logging_payload from litellm.proxy.utils import ( + GUARDRAIL_ENDPOINT_FAILURE, + GUARDRAIL_ENDPOINT_FAILURE_KEY, REJECTED_BEFORE_REQUEST_SETUP, REJECTED_BEFORE_REQUEST_SETUP_KEY, BilledFailedAttempt, @@ -984,6 +986,46 @@ async def test_async_post_call_failure_hook_tracks_config_defined_pass_through_r assert call_args.kwargs["kwargs"]["litellm_params"]["metadata"]["status"] == "failure" +@pytest.mark.asyncio +@pytest.mark.parametrize("route", ["/guardrails/apply_guardrail", "/apply_guardrail"]) +async def test_async_post_call_failure_hook_logs_a_failure_the_guardrail_endpoint_marked(route: str) -> None: + verdict: Final = [ + {"guardrail_name": "bedrock-guard", "guardrail_status": "guardrail_intervened", "guardrail_cost": 0.00075} + ] + spend_writer: Final = create_autospec(DBSpendUpdateWriter, instance=True) + + await ProxyDBLogger(spend_writer=lambda: spend_writer).async_post_call_failure_hook( + request_data={ + "metadata": {"standard_logging_guardrail_information": verdict}, + GUARDRAIL_ENDPOINT_FAILURE_KEY: GUARDRAIL_ENDPOINT_FAILURE, + }, + original_exception=HTTPException(status_code=400, detail="Violated guardrail policy"), + user_api_key_dict=UserAPIKeyAuth(api_key="test_api_key", request_route=route), + ) + + assert spend_writer.update_database.await_count == 1 + written: Final = spend_writer.update_database.call_args.kwargs + assert written["response_cost"] == 0.00075 + assert written["kwargs"]["litellm_params"]["metadata"]["status"] == "failure" + assert written["kwargs"]["litellm_params"]["metadata"]["standard_logging_guardrail_information"] == verdict + + +@pytest.mark.asyncio +@pytest.mark.parametrize("marker", [None, True, "litellm_guardrail_endpoint_failure"]) +async def test_async_post_call_failure_hook_skips_a_guardrail_endpoint_failure_the_endpoint_did_not_mark( # test-quality-ok: update_database is the DB write boundary, so whether it is awaited is the observable outcome + marker: object, +) -> None: + spend_writer: Final = create_autospec(DBSpendUpdateWriter, instance=True) + + await ProxyDBLogger(spend_writer=lambda: spend_writer).async_post_call_failure_hook( + request_data={"guardrail_name": "bedrock-guard", "text": "hello", GUARDRAIL_ENDPOINT_FAILURE_KEY: marker}, + original_exception=HTTPException(status_code=401, detail="Authentication Error"), + user_api_key_dict=UserAPIKeyAuth(api_key="test_api_key", request_route="/apply_guardrail"), + ) + + assert spend_writer.update_database.await_count == 0 + + @pytest.mark.asyncio @pytest.mark.parametrize( ("team_id", "route", "written"), diff --git a/tests/unit/proxy/management_endpoints/test_policy_endpoints.py b/tests/unit/proxy/management_endpoints/test_policy_endpoints.py index e334305dd98..2fe13c85a89 100644 --- a/tests/unit/proxy/management_endpoints/test_policy_endpoints.py +++ b/tests/unit/proxy/management_endpoints/test_policy_endpoints.py @@ -4,11 +4,15 @@ Unit tests for policy management endpoints. Tests apply_policies: resolving guardrails from policy names and applying them to inputs. """ +from typing import Final, Literal from unittest.mock import MagicMock, patch import pytest +from starlette.requests import Request +import litellm from litellm.integrations.custom_guardrail import CustomGuardrail +from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj from litellm.proxy.management_endpoints.policy_endpoints import apply_policies from litellm.types.utils import GenericGuardrailAPIInputs @@ -644,6 +648,44 @@ class TestApplyPoliciesDirectGuardrailNames: assert result["guardrail_errors"] == [] +class _VerdictCountingGuardrail(CustomGuardrail): + verdicts_seen: tuple[int, ...] = () + + async def apply_guardrail( + self, + inputs: GenericGuardrailAPIInputs, + request_data: dict[str, object], + input_type: Literal["request", "response"], + logging_obj: LiteLLMLoggingObj | None = None, + ) -> GenericGuardrailAPIInputs: + metadata: Final = request_data.get("metadata") + earlier_verdicts: Final = ( + metadata.get("standard_logging_guardrail_information", []) if isinstance(metadata, dict) else [] + ) + self.verdicts_seen = (*self.verdicts_seen, len(earlier_verdicts)) + return inputs + + +@pytest.mark.asyncio +async def test_each_batch_input_is_checked_without_verdicts_from_earlier_inputs() -> None: + from litellm.proxy._types import LitellmUserRoles, UserAPIKeyAuth + from litellm.proxy.management_endpoints.policy_endpoints import endpoints + + guardrail: Final = _VerdictCountingGuardrail(guardrail_name="counting") + litellm.logging_callback_manager.add_litellm_callback(guardrail) + + await endpoints.test_policies_and_guardrails( + request=Request(scope={"type": "http", "method": "POST", "path": "/utils/test_policies_and_guardrails"}), + data=endpoints.TestPoliciesAndGuardrailsRequest( + guardrail_names=["counting"], + inputs_list=[{"texts": ["first input"]}, {"texts": ["second input"]}], + ), + user_api_key_dict=UserAPIKeyAuth(user_role=LitellmUserRoles.PROXY_ADMIN), + ) + + assert guardrail.verdicts_seen == (0, 0) + + # --------------------------------------------------------------------------- # Tests for competitor enrichment helper functions # ---------------------------------------------------------------------------