diff --git a/litellm/proxy/guardrails/guardrail_hooks/generic_guardrail_api/generic_guardrail_api.py b/litellm/proxy/guardrails/guardrail_hooks/generic_guardrail_api/generic_guardrail_api.py index 3d1a173635e..03c60bf2991 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/generic_guardrail_api/generic_guardrail_api.py +++ b/litellm/proxy/guardrails/guardrail_hooks/generic_guardrail_api/generic_guardrail_api.py @@ -188,7 +188,9 @@ class GenericGuardrailAPI(CustomGuardrail): { "action": "BLOCKED" | "NONE" | "GUARDRAIL_INTERVENED", "blocked_reason": str (optional, only if action is BLOCKED), - "text": str (optional, modified text if action is GUARDRAIL_INTERVENED) + "text": str (optional, modified text if action is GUARDRAIL_INTERVENED), + "tool_calls": list (optional, modified tool calls in the same shape they + were sent, applied under GUARDRAIL_INTERVENED semantics) } """ @@ -365,6 +367,8 @@ class GenericGuardrailAPI(CustomGuardrail): ) if rows_to_write_back is not None: return_inputs["structured_messages"] = list(rows_to_write_back) # mutable-ok: guardrail inputs take a list + if guardrail_response.tool_calls: + return_inputs["tool_calls"] = guardrail_response.tool_calls if guardrail_response.stream_holdback_chars is not None: return_inputs["stream_holdback_chars"] = guardrail_response.stream_holdback_chars return return_inputs diff --git a/litellm/types/proxy/guardrails/guardrail_hooks/generic_guardrail_api.py b/litellm/types/proxy/guardrails/guardrail_hooks/generic_guardrail_api.py index 44e2cc2404f..2d188d249d3 100644 --- a/litellm/types/proxy/guardrails/guardrail_hooks/generic_guardrail_api.py +++ b/litellm/types/proxy/guardrails/guardrail_hooks/generic_guardrail_api.py @@ -174,6 +174,7 @@ class GenericGuardrailAPIResponse: images: list[str] | None tools: list[GuardrailToolParam] | None structured_messages: Sequence[AllMessageValues] | None + tool_calls: list[dict[str, Any]] | None action: str blocked_reason: str | None stream_holdback_chars: list[int] | None @@ -185,6 +186,7 @@ class GenericGuardrailAPIResponse: blocked_reason: str | None = None, images: list[str] | None = None, tools: list[GuardrailToolParam] | None = None, + tool_calls: list[dict[str, Any]] | None = None, stream_holdback_chars: list[int] | None = None, structured_messages: Sequence[AllMessageValues] | None = None, ) -> None: @@ -194,6 +196,7 @@ class GenericGuardrailAPIResponse: self.images = images self.tools = tools self.structured_messages = structured_messages + self.tool_calls = tool_calls # Number of trailing chars, indexed the same as ``texts``, that the # framework must withhold from streaming emission until the next # processing round (word-boundary safety for text transformations). @@ -211,6 +214,7 @@ class GenericGuardrailAPIResponse: texts=data.get("texts"), images=data.get("images"), tools=data.get("tools"), + tool_calls=data.get("tool_calls"), stream_holdback_chars=stream_holdback_chars, structured_messages=structured_messages_from_response(data.get("structured_messages")), ) diff --git a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_generic_guardrail_api.py b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_generic_guardrail_api.py index a5e79f84ef1..50315a8a676 100644 --- a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_generic_guardrail_api.py +++ b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_generic_guardrail_api.py @@ -2141,3 +2141,71 @@ class TestFailOnError: request_data={}, input_type="response", ) + + +@pytest.mark.asyncio +async def test_apply_guardrail_returns_tool_calls_from_service(generic_guardrail): + """A guardrail service may return modified tool_calls under + GUARDRAIL_INTERVENED; they must reach the caller's inputs (#41111).""" + modified_tool_calls = [ + { + "id": "call_1", + "type": "function", + "function": {"name": "read_file", "arguments": '{"path": "/tmp/safe.txt"}'}, + } + ] + mock_response = MagicMock() + mock_response.status_code = 200 + mock_response.json.return_value = { + "action": "GUARDRAIL_INTERVENED", + "texts": ["safe text"], + "tool_calls": modified_tool_calls, + } + mock_response.raise_for_status = MagicMock() + + with patch.object(generic_guardrail.async_handler, "post", new=AsyncMock(return_value=mock_response)): + inputs = { + "texts": ["original text"], + "tool_calls": [ + { + "id": "call_1", + "type": "function", + "function": {"name": "read_file", "arguments": '{"path": "/etc/passwd"}'}, + } + ], + } + result = await generic_guardrail.apply_guardrail( + inputs=inputs, + request_data={}, + input_type="response", + ) + + assert result["tool_calls"] == modified_tool_calls + assert result["texts"] == ["safe text"] + + +@pytest.mark.asyncio +async def test_apply_guardrail_without_tool_calls_in_response_keeps_scanned_tools(generic_guardrail): + """No tool_calls in the guardrail response means no modification: the scanned + tool calls flow through unchanged and the modification key stays absent.""" + mock_response = MagicMock() + mock_response.status_code = 200 + mock_response.json.return_value = {"action": "NONE"} + mock_response.raise_for_status = MagicMock() + + scanned_tool_calls = [ + { + "id": "call_1", + "type": "function", + "function": {"name": "read_file", "arguments": '{"path": "/etc/passwd"}'}, + } + ] + with patch.object(generic_guardrail.async_handler, "post", new=AsyncMock(return_value=mock_response)): + inputs = {"texts": ["hello"], "tool_calls": scanned_tool_calls} + result = await generic_guardrail.apply_guardrail( + inputs=inputs, + request_data={}, + input_type="response", + ) + + assert "tool_calls" not in result