feat(guardrails): apply tool_calls returned by the Generic Guardrail API

The Generic Guardrail API already sends tool calls to the guardrail
service in both directions, but its response model had no tool_calls
field, so a service returning rewritten tool call arguments under
GUARDRAIL_INTERVENED had its correction silently dropped (#41111) -
arguments are the part of an agent loop that touches filesystems and
internal APIs, and blocking the whole turn was the only option.

Parse tool_calls from the guardrail response and pass them through in
the returned inputs; the request- and response-side application layers
already match them by index and rewrite the corresponding tool calls.
This commit is contained in:
rain 2026-09-15 05:34:34 +08:00
parent e484a7c89c
commit aa9434efa4
3 changed files with 77 additions and 1 deletions

View file

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

View file

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

View file

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