diff --git a/litellm/proxy/guardrails/guardrail_hooks/panw_prisma_airs/panw_prisma_airs.py b/litellm/proxy/guardrails/guardrail_hooks/panw_prisma_airs/panw_prisma_airs.py index df94fc5e5bc..9d5465b7e07 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/panw_prisma_airs/panw_prisma_airs.py +++ b/litellm/proxy/guardrails/guardrail_hooks/panw_prisma_airs/panw_prisma_airs.py @@ -1779,12 +1779,27 @@ class PanwPrismaAirsHandler(CustomGuardrail): if input_type == "request": structured_messages: Final = inputs.get("structured_messages") if structured_messages: - # Uses request_data["messages"] (original format), NOT structured_messages - # (which has injected system content from adapter translation). + # Neither message source aligns with `texts` on every request shape, + # so try both and let the count check in + # _get_latest_user_text_indices pick the one that does: + # - request_data["messages"] is the original, unfiltered list. It + # is the aligned source for Anthropic /v1/messages, whose + # structured_messages carries a system entry injected by + # translation that has no counterpart in `texts`. + # - structured_messages is built alongside `texts` by the + # translation handler. It is the aligned source when a skip flag + # scoped a message out upstream, and the only source at all for + # /v1/responses, whose request_data carries `input` rather than + # `messages`. + # If neither walks to the same text count, scannable_indices stays + # None and the existing role filter below engages unchanged. if self._use_latest_user_only(request_data, logging_obj): - original_messages: Final = request_data.get("messages") - if original_messages: - scannable_indices = self._get_latest_user_text_indices(texts, original_messages) + for candidate in (request_data.get("messages"), structured_messages): + if not candidate: + continue + scannable_indices = self._get_latest_user_text_indices(texts, candidate) + if scannable_indices is not None: + break # Fall through to existing role filtering if: # - latest-only not enabled for this request, OR # - no original messages, OR diff --git a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_panw_prisma_airs.py b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_panw_prisma_airs.py index 99ecf926f15..884743fc0f7 100644 --- a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_panw_prisma_airs.py +++ b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_panw_prisma_airs.py @@ -5051,12 +5051,14 @@ class TestPanwAirsLatestRoleMessageOnlyOpenAIShape: @pytest.mark.asyncio async def test_flag_true_count_mismatch_falls_back_to_role_filter(self): - """OpenAI shape + flag True + texts/messages count mismatch: safety fallback. + """OpenAI shape + flag True + request_data["messages"] mismatch: + structured_messages carries the scope. When request_data["messages"] does not walk to the same text count as the flattened texts (e.g. a message was scoped out upstream), - _get_latest_user_text_indices returns None and the existing role-filter - scan over structured_messages must engage. + structured_messages is the source that stays aligned with `texts`, so + latest-only scanning still engages through it rather than degrading to + a full-history scan. """ handler = make_handler(experimental_use_latest_role_message_only=True) @@ -5091,10 +5093,11 @@ class TestPanwAirsLatestRoleMessageOnlyOpenAIShape: input_type="request", ) - # Fallback role-filter scan: all system/user texts scanned - assert mock_api.call_count == 3 + # structured_messages aligns with texts, so only the latest user + # message is scanned - no degradation to a full-history scan. + assert mock_api.call_count == 1 scanned = [call.kwargs["content"] for call in mock_api.call_args_list] - assert scanned == ["system text", "user one", "user two"] + assert scanned == ["user two"] @pytest.mark.asyncio async def test_flag_true_no_user_message_falls_back(self): @@ -5244,6 +5247,116 @@ class TestPanwAirsLatestRoleMessageOnlyOpenAIShape: assert mock_api.call_count == 2 +class TestPanwAirsLatestRoleMessageOnlyResponsesShape: + """/v1/responses requests carry `input`, not `messages`, so request_data has no + messages list. structured_messages is the only aligned source there.""" + + @pytest.mark.asyncio + async def test_responses_shape_scans_latest_user_only(self): + handler = make_handler(experimental_use_latest_role_message_only=True) + + inputs: GenericGuardrailAPIInputs = { + "texts": ["first user turn", "first assistant turn", "latest user turn"], + "structured_messages": [ + {"role": "user", "content": "first user turn"}, + {"role": "assistant", "content": "first assistant turn"}, + {"role": "user", "content": "latest user turn"}, + ], + } + # No "messages" key - this is what a Responses request looks like. + request_data = { + "litellm_call_id": "test-call-id", + "model": "gpt-4.1", + "input": "latest user turn", + "proxy_server_request": {"url": "http://localhost:4000/v1/responses"}, + } + + with patch.object( + handler, "_call_panw_api", new_callable=AsyncMock + ) as mock_api: + mock_api.return_value = {"action": "allow", "category": "benign"} + + await handler.apply_guardrail( + inputs=inputs, request_data=request_data, input_type="request" + ) + + assert mock_api.call_count == 1 + scanned = [c.kwargs["content"] for c in mock_api.call_args_list] + assert scanned == ["latest user turn"] + + @pytest.mark.asyncio + async def test_responses_shape_full_history_when_flag_unset(self): + handler = make_handler() + + inputs: GenericGuardrailAPIInputs = { + "texts": ["first user turn", "latest user turn"], + "structured_messages": [ + {"role": "user", "content": "first user turn"}, + {"role": "user", "content": "latest user turn"}, + ], + } + request_data = { + "litellm_call_id": "test-call-id", + "model": "gpt-4.1", + "input": "latest user turn", + "proxy_server_request": {"url": "http://localhost:4000/v1/responses"}, + } + + with patch.object( + handler, "_call_panw_api", new_callable=AsyncMock + ) as mock_api: + mock_api.return_value = {"action": "allow", "category": "benign"} + + result = await handler.apply_guardrail( + inputs=inputs, request_data=request_data, input_type="request" + ) + + # Unset default is unchanged for non-Anthropic shapes: every user turn + # is sent for scanning, and all texts pass through on allow. + scanned = [c.kwargs["content"] for c in mock_api.call_args_list] + assert scanned == ["first user turn", "latest user turn"] + assert result["texts"] == ["first user turn", "latest user turn"] + + @pytest.mark.asyncio + async def test_both_sources_mismatch_falls_back_to_role_filter(self): + """When neither message source walks to the same text count, the existing + role-filter scan engages - the original safety fallback.""" + handler = make_handler(experimental_use_latest_role_message_only=True) + + inputs: GenericGuardrailAPIInputs = { + "texts": ["system text", "user one", "user two"], + "structured_messages": [ + {"role": "system", "content": "system text"}, + {"role": "user", "content": "user one"}, + {"role": "user", "content": "user two"}, + {"role": "user", "content": "extra, not in texts"}, + ], + } + request_data = { + "litellm_call_id": "test-call-id", + "model": "gemini-2.5-flash", + "messages": [ + {"role": "system", "content": "system text"}, + {"role": "user", "content": "user one"}, + {"role": "user", "content": "user two"}, + {"role": "user", "content": "also not in texts"}, + ], + } + + with patch.object( + handler, "_call_panw_api", new_callable=AsyncMock + ) as mock_api: + mock_api.return_value = {"action": "allow", "category": "benign"} + + await handler.apply_guardrail( + inputs=inputs, request_data=request_data, input_type="request" + ) + + assert mock_api.call_count == 3 + scanned = [c.kwargs["content"] for c in mock_api.call_args_list] + assert scanned == ["system text", "user one", "user two"] + + class TestPanwAirsLatestRoleMessageOnlyStartupWarning: """Enabling latest-only scanning trusts caller-supplied history, so the operator gets an explicit startup warning - same treatment as fallback_on_error='allow'."""