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): keep the assistant turn when scoping empties the request history

yuchengd5e056491c37
diff --git a/litellm/llms/base_llm/guardrail_translation/base_translation.py b/litellm/llms/base_llm/guardrail_translation/base_translation.py
index 2fad7d7a192..033a0180553 100644
--- a/litellm/llms/base_llm/guardrail_translation/base_translation.py
+++ b/litellm/llms/base_llm/guardrail_translation/base_translation.py
@@ -30,6 +30,7 @@ class RequestScanContext:
structured_messages: tuple["AllMessageValues", ...] = ()
tools: tuple["ChatCompletionToolParam", ...] = ()
+ conversation_supplied: bool = False
@staticmethod
def scoped(
@@ -51,6 +52,7 @@ class RequestScanContext:
return RequestScanContext(
structured_messages=tuple(structured_messages[index] for index in scoped_indices),
tools=() if scan_only_tool_results else tuple(tools),
+ conversation_supplied=bool(structured_messages),
)
@@ -308,7 +310,7 @@ class BaseTranslation(ABC):
if request_data is None:
return inputs
context: Final = self.request_scan_context(request_data, guardrail_to_apply)
- if not context.structured_messages:
+ if not context.conversation_supplied:
return inputs
assistant_turn: Final = response_assistant_turn(inputs.get("texts") or (), inputs.get("tool_calls") or ())
contextual_inputs: Final[GenericGuardrailAPIInputs] = {
diff --git a/litellm/llms/openai/responses/guardrail_translation/handler.py b/litellm/llms/openai/responses/guardrail_translation/handler.py
index ce32f930b62..4842c8461e9 100644
--- a/litellm/llms/openai/responses/guardrail_translation/handler.py
+++ b/litellm/llms/openai/responses/guardrail_translation/handler.py
@@ -454,8 +454,9 @@ class OpenAIResponsesHandler(BaseTranslation):
def request_scan_context(self, data: dict, guardrail_to_apply: "CustomGuardrail") -> RequestScanContext:
raw_tools: Final = data.get("tools")
+ structured_messages: Final = tuple(self.get_structured_messages(data) or ())
return RequestScanContext(
- structured_messages=tuple(self.get_structured_messages(data) or ()),
+ structured_messages=structured_messages,
tools=tuple(
cast(ChatCompletionToolParam, tool) # cast-ok: mcp tools ride along in the guardrail's tool list
for form in LiteLLMCompletionResponsesConfig.responses_tools_to_chat_forms(
@@ -463,6 +464,7 @@ class OpenAIResponsesHandler(BaseTranslation):
)
for tool in form.chat_tools
),
+ conversation_supplied=bool(structured_messages),
)
async def process_input_messages(
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 f0bd5efe5e1..c88159de76b 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
@@ -2360,6 +2360,19 @@ class TestResponseScanCarriesRequestConversation:
assert [m["role"] for m in inputs["structured_messages"]] == ["tool", "assistant"]
assert "tools" not in inputs
+ @pytest.mark.asyncio
+ async def test_scan_only_tool_results_without_tool_turns_still_carries_the_reply(self):
+ handler = OpenAIChatCompletionsHandler()
+ guardrail = InputsRecordingGuardrail()
+ guardrail.scan_only_tool_results = True
+ request = {**self._request(), "messages": [{"role": "user", "content": "Delete everything"}]}
+
+ await handler.process_output_response(self._tool_call_response(), guardrail, request_data=request)
+
+ [(_, inputs)] = guardrail.seen
+ assert [m["role"] for m in inputs["structured_messages"]] == ["assistant"]
+ assert inputs["structured_messages"][0]["tool_calls"][0]["function"]["name"] == "run_shell"
+
@pytest.mark.asyncio
async def test_response_scan_without_request_data_stays_response_only(self):
guardrail = InputsRecordingGuardrail()
diff --git a/tests/test_litellm/llms/openai/responses/test_openai_responses_guardrail_handler.py b/tests/test_litellm/llms/openai/responses/test_openai_responses_guardrail_handler.py
index 23e3b20783f..bb378a9bb34 100644
--- a/tests/test_litellm/llms/openai/responses/test_openai_responses_guardrail_handler.py
+++ b/tests/test_litellm/llms/openai/responses/test_openai_responses_guardrail_handler.py
@@ -3397,3 +3397,15 @@ class TestResponsesResponseScanCarriesRequestConversation:
"assistant",
]
assert inputs["structured_messages"][-1] == {"role": "assistant", "content": "Paris is the capital"}
+
+ @pytest.mark.asyncio
+ async def test_response_scan_without_request_input_stays_response_only(self):
+ handler = OpenAIResponsesHandler()
+ guardrail = TypedInputsRecordingGuardrail()
+ request = {k: v for k, v in self._request().items() if k not in ("input", "instructions")}
+
+ await handler.process_output_response(self._tool_call_response(), guardrail, request_data=request)
+
+ [(_, inputs)] = guardrail.seen
+ assert "structured_messages" not in inputs
+ assert "tools" not in inputs