Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llm_guardrails_sk-0.1.0-py3-none-any.whl (22.7 kB view details)

Uploaded Python 3

File details

Details for the file llm_guardrails_sk-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for llm_guardrails_sk-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3e3360e55cd9f1799643556cad3849403da6066d2c64a8bfccf3e244f3c5309d
MD5 65867e0bbd81288f9bc2dd2de48b945c
BLAKE2b-256 f1843deb6df0b40c98b8bd752d13093454878f238221d3dc84270241a305c7ee

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page