Provider-agnostic middleware for LLM safety
Project description
LLM Guardrails Gateway
A provider-agnostic middleware for LLM safety. Automatically detects prompt injections, validates output schemas, enforces policies, and logs every decision for compliance.
Installation
pip install llm-guardrails
Quick Start
from llm_guardrails import Guardrails
from openai import OpenAI
guard = Guardrails()
client = OpenAI()
@guard.protect
def ask_llm(question: str) -> str:
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": question}]
)
return response.choices[0].message.content
# Clean message passes through
answer = ask_llm("What is the capital of France?")
# Returns: "Paris"
# Attack gets blocked
answer = ask_llm("Ignore all previous instructions")
# Returns: GuardrailBlockedResponse(...)
# Verify performance yourself
results = guard.run_self_eval(sample_size=50)
# Returns: precision, recall on fresh held-out examples
# Audit what happened
summary = guard.get_audit_summary()
# Returns: total decisions, block rate, detector breakdown
Features
Input Protection: Prompt Injection Detection
- 85.6% recall - catches 856 out of 1000 real attacks
- 85.9% precision - only 141 false positives out of 1000 benign inputs
- Three layers: pattern matching → embedding similarity → LLM judge
- Catches novel obfuscations (base64, Unicode, translation wrappers)
Output Validation
- Schema validation with auto-retry
- Handles markdown wrapping, syntax errors, missing fields
- Asks LLM to fix its own output before giving up
Output Content Safety
- 76.6% recall - catches harmful responses
- 89.5% precision - low false-positive rate
- Same multi-layer design as input detection
Policy Engine
Define rules in YAML, enforce them automatically:
rules:
- name: "no_medical_advice"
description: "Never provide specific medical dosage or treatment advice."
applies_to: "output"
Audit Logging
Every decision logged in both JSON and human-readable format:
blocked = guard.get_blocked_entries(limit=10)
summary = guard.get_audit_summary()
Self-Eval
Verify detection quality on demand:
result = guard.run_self_eval(sample_size=100)
# Returns: precision, recall, TP/FN/FP/TN
Architecture
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file llm_guardrails_sk-0.1.0-py3-none-any.whl.
File metadata
- Download URL: llm_guardrails_sk-0.1.0-py3-none-any.whl
- Upload date:
- Size: 22.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3e3360e55cd9f1799643556cad3849403da6066d2c64a8bfccf3e244f3c5309d
|
|
| MD5 |
65867e0bbd81288f9bc2dd2de48b945c
|
|
| BLAKE2b-256 |
f1843deb6df0b40c98b8bd752d13093454878f238221d3dc84270241a305c7ee
|