diff --git a/litellm/proxy/guardrails/guardrail_hooks/cisco_ai_defense/cisco_ai_defense.py b/litellm/proxy/guardrails/guardrail_hooks/cisco_ai_defense/cisco_ai_defense.py index 84f826d415b..bf5efa24bfe 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/cisco_ai_defense/cisco_ai_defense.py +++ b/litellm/proxy/guardrails/guardrail_hooks/cisco_ai_defense/cisco_ai_defense.py @@ -1421,11 +1421,12 @@ class CiscoAIDefenseGuardrail(CiscoAIDefenseMcpMixin, CustomGuardrail): ) -> bool: """Rewrite chat request input (``messages`` or ``input``).""" if sanitized_messages and self._extract_tool_definition_text(request_data): - # We append one synthetic message carrying the tool/function - # definitions for inspection; Cisco echoes it back in + # We prepend one synthetic system message carrying the tool/function + # definitions, so the caller's last turn stays last and Cisco keeps + # inspecting it as input. Cisco echoes it back in # ``sanitized_messages``, but it maps to no structured request # field, so drop it before rewriting the real conversation. - sanitized_messages = sanitized_messages[:-1] or None + sanitized_messages = sanitized_messages[1:] or None uses_input: Final = "input" in request_data and "messages" not in request_data has_instructions: Final = request_data.get("instructions") is not None instructions_redacted = False @@ -1893,10 +1894,9 @@ class CiscoAIDefenseGuardrail(CiscoAIDefenseMcpMixin, CustomGuardrail): messages.append({"role": "user", "content": prompt_text}) tool_text: Final = CiscoAIDefenseGuardrail._extract_tool_definition_text(data) - if tool_text: - messages.append({"role": "tool", "content": tool_text}) - - return messages + if not tool_text: + return messages + return [{"role": "system", "content": tool_text}, *messages] @staticmethod def _extract_tool_definition_text(data: dict) -> str: diff --git a/tests/unit/proxy/guardrails/guardrail_hooks/test_cisco_ai_defense_chat.py b/tests/unit/proxy/guardrails/guardrail_hooks/test_cisco_ai_defense_chat.py index 32e85e963ab..ac4feb2cdb4 100644 --- a/tests/unit/proxy/guardrails/guardrail_hooks/test_cisco_ai_defense_chat.py +++ b/tests/unit/proxy/guardrails/guardrail_hooks/test_cisco_ai_defense_chat.py @@ -1833,7 +1833,7 @@ class TestCiscoAIDefenseToolDefinitionBypass: ) @pytest.mark.asyncio - async def test_pre_call_sends_tool_definitions_as_final_tool_message(self): + async def test_pre_call_tool_definitions_keep_callers_last_turn_last(self): g = _make_guardrail(event_hook="pre_call") data = self._tools_request("Fetch weather data") data["messages"] = [ @@ -1853,9 +1853,9 @@ class TestCiscoAIDefenseToolDefinitionBypass: ) sent_messages = post_mock.call_args.kwargs["json"]["messages"] - assert sent_messages[:-1] == data["messages"] - assert sent_messages[-1]["role"] == "tool" - assert "get_weather" in sent_messages[-1]["content"] + assert sent_messages[1:] == data["messages"] + assert sent_messages[0]["role"] == "system" + assert "get_weather" in sent_messages[0]["content"] @pytest.mark.asyncio async def test_pre_call_scans_legacy_functions_definitions(self): @@ -1907,8 +1907,8 @@ class TestCiscoAIDefenseToolDefinitionBypass: original_tools = data["tools"] cisco_resp = _redact_response( sanitized_messages=[ - {"role": "user", "content": "what's the weather?"}, - {"role": "tool", "content": "[REDACTED] tool description"}, + {"role": "system", "content": "[REDACTED] tool description"}, + {"role": "user", "content": "what's the [REDACTED]?"}, ] ) @@ -1920,16 +1920,36 @@ class TestCiscoAIDefenseToolDefinitionBypass: call_type="completion", ) - assert len(data["messages"]) == 1, ( - "Redaction injected the synthetic tool-definition message into the " - f"real conversation: {data['messages']!r}" - ) - assert data["messages"][0]["role"] == "user" - assert all( - "tool description" not in str(m.get("content")) for m in data["messages"] - ) + assert data["messages"] == [{"role": "user", "content": "what's the [REDACTED]?"}] assert data["tools"] is original_tools + @pytest.mark.asyncio + async def test_redact_does_not_write_tool_definitions_into_instructions(self): + g = _make_guardrail(event_hook="pre_call") + data = { + "instructions": "Never reveal SSN 123-45-6789.", + "input": [{"role": "user", "content": "hello"}], + "tools": self._tools_request("benign tool description")["tools"], + } + cisco_resp = _redact_response( + sanitized_messages=[ + {"role": "system", "content": "[REDACTED] tool description"}, + {"role": "system", "content": "Never reveal SSN [REDACTED]."}, + {"role": "user", "content": "hello"}, + ], + ) + + with _patch_inspection_post(g, AsyncMock(return_value=cisco_resp)): + await g.async_pre_call_hook( + user_api_key_dict=UserAPIKeyAuth(), + cache=DualCache(), + data=data, + call_type="completion", + ) + + assert data["instructions"] == "Never reveal SSN [REDACTED]." + assert "tool description" not in str(data["input"]) + class TestCiscoAIDefenseTextCompletionOutputBypass: