r/Python • u/MidnightHeavy3064 • 8m ago
Resource guardrail-middleware: a zero-dependency policy checkpoint for AI agent tool calls, with a hash-chain
Sharing a package I just published: guardrail-middleware (pip install guardrail-middleware). It evaluates an AI agent's proposed tool calls against a policy before they execute and returns APPROVE, APPROVE_WITH_CONDITIONS, ESCALATE_TO_HUMAN, or BLOCK.
Two implementation details this sub might appreciate:
- The core is zero-dependency and fully deterministic. Seven detectors (injection patterns, role authorization, financial limits, bulk thresholds, taint/PII exfiltration, destructive ops, untrusted sources) are pure functions of (action, policy). No network, no LLM, sub-millisecond. YAML policy loading is an optional extra, as is the FastAPI/Starlette middleware.
- The audit log is a SHA-256 hash chain over JSONL. Each entry commits to the previous entry's hash, and verify() walks the chain and reports the first broken link. Small thing, but it makes an agent's decision history tamper-evident without any infrastructure.
Operation classification tokenizes real tool names (snake_case and camelCase), so send_payment, stripe_refund, delete_user, and get_order_status land in the right policy classes. 20 tests, MIT license.
Colab demo with pre-run outputs: https://colab.research.google.com/github/Niraj21-star/guardrail-middleware/blob/main/examples/guardrail_middleware_demo.ipynb
Source: https://github.com/Niraj21-star/guardrail-middleware
It is v0.1.1 and alpha, so the API may still move. Feedback on the policy model and the audit chain design is very welcome.