fix(guardrails): read Gemini contents when a pass-through body's chat field is empty

This commit is contained in:
yucheng 2026-11-02 14:24:56 -08:00
parent b7d08bf5f8
commit 3332dcb4ad
2 changed files with 9 additions and 1 deletions

View file

@ -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)

View file

@ -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"}]}]}