From 3332dcb4ada3afd969be352a1a7d800d3b3c9c19 Mon Sep 17 00:00:00 2001 From: yucheng Date: Mon, 2 Nov 2026 14:24:56 -0800 Subject: [PATCH] fix(guardrails): read Gemini contents when a pass-through body's chat field is empty --- litellm/proxy/guardrails/guardrail_hooks/lakera_ai_v2.py | 2 +- .../proxy/guardrails/guardrail_hooks/test_lakera_ai_v2.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/litellm/proxy/guardrails/guardrail_hooks/lakera_ai_v2.py b/litellm/proxy/guardrails/guardrail_hooks/lakera_ai_v2.py index b818cf897eb..faecb8e31e6 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/lakera_ai_v2.py +++ b/litellm/proxy/guardrails/guardrail_hooks/lakera_ai_v2.py @@ -283,7 +283,7 @@ def _has_gemini_contents(data: object, call_type: str | None) -> bool: fields: Final = as_str_mapping(data) or {} return ( call_type in _GEMINI_BODY_CALL_TYPES - and not (call_type == CallTypes.pass_through.value and ("messages" in fields or "input" in fields)) + and not (call_type == CallTypes.pass_through.value and (fields.get("messages") or fields.get("input"))) and ( isinstance(fields.get("contents"), list | dict) or isinstance(fields.get("systemInstruction") or fields.get("system_instruction"), dict) diff --git a/tests/unit/proxy/guardrails/guardrail_hooks/test_lakera_ai_v2.py b/tests/unit/proxy/guardrails/guardrail_hooks/test_lakera_ai_v2.py index 1ca2fc9b4d7..e8785551e4f 100644 --- a/tests/unit/proxy/guardrails/guardrail_hooks/test_lakera_ai_v2.py +++ b/tests/unit/proxy/guardrails/guardrail_hooks/test_lakera_ai_v2.py @@ -1847,6 +1847,14 @@ class TestGeminiContents: {"role": "user", "content": "card"} ] + @pytest.mark.parametrize("chat_field", ["messages", "input"]) + @pytest.mark.parametrize("empty", [[], None, ""]) + def test_an_empty_chat_field_does_not_hide_contents_on_a_pass_through_body(self, chat_field: str, empty: object): + data: Final = {chat_field: empty, "contents": [{"parts": [{"text": "card"}]}]} + assert _build_lakera_inspection_messages(data, call_type="pass_through_endpoint") == [ + {"role": "user", "content": "card"} + ] + @pytest.mark.parametrize("chat_field", ["messages", "input"]) def test_a_pass_through_chat_body_with_stray_contents_is_read_as_chat(self, chat_field: str): data: Final = {chat_field: [USER_MSG], "contents": [{"parts": [{"text": "stray"}]}]}