Validate agent tool-call requests against an allowlist and blocked-pattern list before execution.
Full problem description visible. Upgrade to unlock the editor, test cases, and solution.
Implement guardrail_check(request, allowed_tools, blocked_patterns) that validates an agent's tool call request before execution.
request is a dict: {"tool": str, "args": dict}.
allowed_tools is a list of strings (tool names that are permitted).
blocked_patterns is a list of strings (substrings that must not appear in any argument value).
Rules (apply in this order):
1. If request["tool"] is not in allowed_tools, return {"allowed": False, "reason": "tool not allowed", "tool": request["tool"]}.
2. If any argument value (converted to string via str()) contains any blocked pattern (case-insensitive check), return {"allowed": False, "reason": "blocked pattern detected", "tool": request["tool"], "pattern": <matched_pattern>}. Return the first blocked pattern found.
3. If all checks pass, return {"allowed": True, "reason": "allowed", "tool": request["tool"]}.
Edge cases:
- If request is None or missing "tool" key, return {"allowed": False, "reason": "invalid request"}.
- If allowed_tools is empty, no tool is allowed.
- If blocked_patterns is empty, no pattern filtering is applied.
₹999/monthLimited period launch pricing.
Get the full problem statement, test cases, interactive editor, solution explanation, and visual diagram.
def guardrail_check(request, allowed_tools, blocked_patterns):
"""Validate an agent tool-call request against allowlist and blocklist."""Implement guardrail filter logic
Already have Pro access? Sign in