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(proxy): guardrail header dedupe, mypy during_call, test mock kwargs

Milan9577d87158d7
diff --git a/litellm/proxy/common_utils/callback_utils.py b/litellm/proxy/common_utils/callback_utils.py
index e31c76dcac1..7ddd722a80e 100644
--- a/litellm/proxy/common_utils/callback_utils.py
+++ b/litellm/proxy/common_utils/callback_utils.py
@@ -433,7 +433,8 @@ def add_guardrail_to_applied_guardrails_header(
return
_metadata = request_data.get("metadata", None) or {}
if "applied_guardrails" in _metadata:
- _metadata["applied_guardrails"].append(guardrail_name)
+ if guardrail_name not in _metadata["applied_guardrails"]:
+ _metadata["applied_guardrails"].append(guardrail_name)
else:
_metadata["applied_guardrails"] = [guardrail_name]
# Ensure metadata is set back to request_data (important when metadata didn't exist)
diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.py
index 561f8e5c553..712853a33c4 100644
--- a/litellm/proxy/utils.py
+++ b/litellm/proxy/utils.py
@@ -1541,12 +1541,11 @@ class ProxyLogging:
else:
user_api_key_auth_dict = user_api_key_dict
# Add task to list for parallel execution
- use_unified_during = (
+ if (
"apply_guardrail" in type(callback).__dict__
and user_api_key_dict is not None
and not getattr(callback, "use_native_during_call_hook", False)
- )
- if use_unified_during:
+ ):
data["guardrail_to_apply"] = callback
guardrail_task = self._run_guardrail_task_with_enrichment(
callback,
diff --git a/tests/guardrails_tests/test_bedrock_guardrails.py b/tests/guardrails_tests/test_bedrock_guardrails.py
index 7eaac60bf2d..54357216208 100644
--- a/tests/guardrails_tests/test_bedrock_guardrails.py
+++ b/tests/guardrails_tests/test_bedrock_guardrails.py
@@ -1107,7 +1107,12 @@ async def test_convert_to_bedrock_format_post_call_streaming_hook():
# Mock the make_bedrock_api_request method to track calls
async def mock_make_bedrock_api_request(
- source, messages=None, response=None, request_data=None
+ source,
+ messages=None,
+ response=None,
+ request_data=None,
+ logging_event_type=None,
+ **kwargs,
):
bedrock_calls.append(
{
@@ -1115,6 +1120,7 @@ async def test_convert_to_bedrock_format_post_call_streaming_hook():
"messages": messages,
"response": response,
"request_data": request_data,
+ "logging_event_type": logging_event_type,
}
)
# Return the mock bedrock response