Skip to content

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]

fix(xecguard): use StandardLoggingGuardrailInformation in logging hook (#32911)

yucheng-berrif947ef14a2a7
diff --git a/litellm/proxy/guardrails/guardrail_hooks/xecguard/xecguard.py b/litellm/proxy/guardrails/guardrail_hooks/xecguard/xecguard.py
index 1b663c16d5b..f4a6f0aeb3b 100644
--- a/litellm/proxy/guardrails/guardrail_hooks/xecguard/xecguard.py
+++ b/litellm/proxy/guardrails/guardrail_hooks/xecguard/xecguard.py
@@ -49,7 +49,11 @@ from litellm.llms.custom_httpx.http_handler import (
httpxSpecialProvider,
)
from litellm.types.guardrails import GuardrailEventHooks
-from litellm.types.utils import GenericGuardrailAPIInputs, GuardrailStatus
+from litellm.types.utils import (
+ GenericGuardrailAPIInputs,
+ GuardrailStatus,
+ StandardLoggingGuardrailInformation,
+)
if TYPE_CHECKING:
from litellm.litellm_core_utils.litellm_logging import (
@@ -246,16 +250,21 @@ class XecGuardGuardrail(CustomGuardrail):
"guardrail_intervened" if scan_result.get("decision") == "UNSAFE" else "success"
)
end_time = datetime.now()
- kwargs["standard_logging_object"]["guardrail_information"] = {
- "duration": (end_time - start_time).total_seconds(),
- "end_time": end_time.timestamp(),
- "guardrail_mode": "logging_only",
- "guardrail_name": "xecguard",
- "guardrail_response": scan_result,
- "guardrail_status": guardrail_status,
- "masked_entity_count": None,
- "start_time": start_time.timestamp(),
- }
+ slg = StandardLoggingGuardrailInformation(
+ guardrail_name=self.guardrail_name or "xecguard",
+ guardrail_mode=GuardrailEventHooks.logging_only,
+ guardrail_response=scan_result,
+ guardrail_status=guardrail_status,
+ start_time=start_time.timestamp(),
+ end_time=end_time.timestamp(),
+ duration=(end_time - start_time).total_seconds(),
+ masked_entity_count=None,
+ )
+ existing = kwargs["standard_logging_object"].get("guardrail_information")
+ if isinstance(existing, list):
+ existing.append(slg)
+ else:
+ kwargs["standard_logging_object"]["guardrail_information"] = [slg]
except Exception as exc:
verbose_proxy_logger.debug(
diff --git a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_xecguard.py b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_xecguard.py
index 60c595c64e5..f64967abbb0 100644
--- a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_xecguard.py
+++ b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_xecguard.py
@@ -1644,12 +1644,37 @@ class TestXecGuardLoggingHook:
)
assert out_kwargs is kwargs
assert out_result is result
- info = kwargs["standard_logging_object"]["guardrail_information"]
+ info_list = kwargs["standard_logging_object"]["guardrail_information"]
+ assert isinstance(info_list, list), "guardrail_information must be a list"
+ assert len(info_list) == 1
+ info = info_list[0]
assert info["guardrail_mode"] == "logging_only"
- assert info["guardrail_name"] == "xecguard"
+ assert info["guardrail_name"] == "test-xecguard"
assert info["guardrail_status"] == "success"
assert info["guardrail_response"]["trace_id"] == "lg-1"
+ @pytest.mark.asyncio
+ async def test_async_logging_hook_appends_to_existing_guardrail_info(
+ self, xecguard_guardrail, mock_request_data
+ ):
+ resp = _make_response({"decision": "SAFE", "trace_id": "lg-4"})
+ prior_entry = {"guardrail_name": "other-guardrail"}
+ with patch.object(xecguard_guardrail.async_handler, "post", return_value=resp):
+ kwargs = {
+ **mock_request_data,
+ "standard_logging_object": {"guardrail_information": [prior_entry]},
+ }
+ await xecguard_guardrail.async_logging_hook(
+ kwargs=kwargs,
+ result=_build_model_response("some answer"),
+ call_type="acompletion",
+ )
+ info_list = kwargs["standard_logging_object"]["guardrail_information"]
+ assert len(info_list) == 2
+ assert info_list[0] is prior_entry
+ assert info_list[1]["guardrail_name"] == "test-xecguard"
+ assert info_list[1]["guardrail_response"]["trace_id"] == "lg-4"
+
@pytest.mark.asyncio
async def test_async_logging_hook_without_response_records_info(
self, xecguard_guardrail, mock_request_data
@@ -1680,7 +1705,9 @@ class TestXecGuardLoggingHook:
result=_build_model_response("x"),
call_type="acompletion",
)
- info = kwargs["standard_logging_object"]["guardrail_information"]
+ info_list = kwargs["standard_logging_object"]["guardrail_information"]
+ assert isinstance(info_list, list), "guardrail_information must be a list"
+ info = info_list[0]
assert info["guardrail_status"] == "guardrail_intervened"
@pytest.mark.asyncio