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(guardrails): cite message scoping only when an unscoped pass finds content

yucheng5ceec4c21e9d
diff --git a/litellm/llms/openai/chat/guardrail_translation/handler.py b/litellm/llms/openai/chat/guardrail_translation/handler.py
index 0be4b6a3a20..e8179e9921e 100644
--- a/litellm/llms/openai/chat/guardrail_translation/handler.py
+++ b/litellm/llms/openai/chat/guardrail_translation/handler.py
@@ -211,10 +211,22 @@ class OpenAIChatCompletionsHandler(BaseTranslation):
)
elif not images_to_check and not guardrail_to_apply.records_own_guardrail_information:
+ unscoped_texts: Final[list[str]] = []
+ unscoped_tool_calls: Final[list[ChatCompletionToolParam]] = []
+ for unscoped_idx, unscoped_message in enumerate(messages):
+ self._extract_inputs(
+ message=unscoped_message,
+ msg_idx=unscoped_idx,
+ texts_to_check=unscoped_texts,
+ images_to_check=[],
+ tool_calls_to_check=unscoped_tool_calls,
+ text_task_mappings=[],
+ tool_call_task_mappings=[],
+ )
guardrail_to_apply.add_standard_logging_guardrail_information_to_request_data(
guardrail_json_response=(
"no scannable content after message scoping"
- if skip_system or skip_tool or scan_only_tool_results
+ if unscoped_texts or unscoped_tool_calls
else "no scannable content"
),
request_data=data,
diff --git a/tests/test_litellm/llms/openai/chat/guardrail_translation/test_openai_guardrail_handler.py b/tests/test_litellm/llms/openai/chat/guardrail_translation/test_openai_guardrail_handler.py
index 40dc5e2df2c..754c89d3d20 100644
--- a/tests/test_litellm/llms/openai/chat/guardrail_translation/test_openai_guardrail_handler.py
+++ b/tests/test_litellm/llms/openai/chat/guardrail_translation/test_openai_guardrail_handler.py
@@ -1920,9 +1920,11 @@ class TestNoScannableContentRecordsNotRun:
assert entries[0]["guardrail_response"] == "no scannable content after message scoping"
@pytest.mark.asyncio
- async def test_empty_content_without_scoping_does_not_blame_scoping(self):
+ @pytest.mark.parametrize("skip_system", [False, True])
+ async def test_empty_content_does_not_blame_scoping(self, skip_system: bool):
handler = OpenAIChatCompletionsHandler()
guardrail = MockGuardrail(guardrail_name="unscoped-guardrail")
+ guardrail.skip_system_message_in_guardrail = skip_system
data = {"messages": [{"role": "user", "content": None}]}
await handler.process_input_messages(data=data, guardrail_to_apply=guardrail)