惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
MongoDB | Blog
MongoDB | Blog
博客园_首页
博客园 - 三生石上(FineUI控件)
博客园 - 聂微东
B
Blog RSS Feed
D
Docker
IT之家
IT之家
大猫的无限游戏
大猫的无限游戏
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
Recent Announcements
Recent Announcements
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
A
About on SuperTechFans
The GitHub Blog
The GitHub Blog
G
Google Developers Blog
V
V2EX
量子位
雷峰网
雷峰网
月光博客
月光博客
云风的 BLOG
云风的 BLOG
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tailwind CSS Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Composable Output Guardrails: Filter Agent Responses Befo...
Mukunda Rao · 2026-05-26 · via DEV Community

The model returned a response that contained a phone number. Someone's real phone number, lifted from the training data. It went straight to the user.

The model returned a response that started with "As an AI language model, I..." — a generic hedge that made the product look unpolished.

The model returned a response with an HTML injection attempt embedded in user-provided text that it had reflected back verbatim.

All of these are output problems. agent-guard-rails is a composable pipeline of output filters that catch them before responses reach users.


The Shape of the Fix

from agent_guard_rails import GuardRails, RuleResult

rails = GuardRails([
    "strip_ai_preamble",          # remove "As an AI..." opener
    "no_pii",                     # flag SSN, cards, phones
    "no_html_injection",          # strip <script>, <img>, HTML tags
    "max_length:2000",            # cap response length
    "no_empty",                   # flag empty responses
])

llm_response = call_llm(prompt)
result: RuleResult = rails.apply(llm_response.content)

if result.blocked:
    return fallback_response(result.rule_name)

# Safe to send
return result.cleaned_text

Enter fullscreen mode Exit fullscreen mode

Each rule either passes (with optional cleaning) or blocks. The first blocking rule stops the pipeline. result.cleaned_text has any cleaning applied by passing rules.


What It Does NOT Do

agent-guard-rails does not use an LLM to evaluate the response. All rules are regex and string-operation based. For semantic guardrails (is this response harmful?), you need a separate classifier.

It does not guarantee safety. Rules catch known patterns. Novel jailbreak outputs or novel PII formats may not be caught. Defense in depth applies here: guardrails are one layer, not the only layer.

It does not block at the request level. It filters responses. For request-level filtering (blocking certain user inputs), you need a separate pre-call filter.


Inside the Library

Rules are composed in a pipeline. Each rule is a function (text: str) -> RuleResult where RuleResult has passed, blocked, cleaned_text, and rule_name.

Built-in rules:

BUILTIN_RULES = {
    "strip_ai_preamble": strip_ai_preamble_rule,
    "no_pii": no_pii_rule,
    "no_html_injection": no_html_injection_rule,
    "no_empty": no_empty_rule,
    "no_repetition": no_repetition_rule,  # detect looping/stuck output
}

# Parameterized rules use "rule_name:param" syntax
def parse_rule(spec: str) -> Callable:
    if ":" in spec:
        name, param = spec.split(":", 1)
        if name == "max_length":
            return max_length_rule(int(param))
        if name == "forbidden_pattern":
            return forbidden_pattern_rule(param)
    return BUILTIN_RULES[spec]

Enter fullscreen mode Exit fullscreen mode

Custom rules: pass callables in the rule list. GuardRails(["no_pii", my_custom_rule]) where my_custom_rule(text) -> RuleResult.

strip_ai_preamble: removes "As an AI language model,", "I'm an AI assistant,", "As a large language model," and similar openers. These make products look unpolished and add no value.

no_html_injection: removes <script>, <iframe>, <img src=...>, and javascript: URIs. Does not strip all HTML (some responses legitimately use markdown-style HTML). Targets injection patterns specifically.

no_repetition: flags output where the same sentence appears 3+ times consecutively. This catches stuck-generation loops where the model starts repeating itself.


When to Use It

Use it for any user-facing agent response. Customer support bots, document Q&A, coding assistants. Anywhere a response goes directly from the LLM to a user without intermediate editing.

The preamble rule is low-risk and high-value. Strip it everywhere. "As an AI language model" adds nothing and signals a lack of product polish.

The PII rule requires thought: do you want to block responses with PII (return a fallback) or clean them (strip the PII from the text)? The blocking approach is safer for compliance. The cleaning approach is better for user experience. Configure per use case.

Skip it for intermediate LLM calls (tool planning, internal reasoning steps). Guardrails are for the final response, not internal agent state.


Install

pip install git+https://github.com/MukundaKatta/agent-guard-rails

Enter fullscreen mode Exit fullscreen mode

from agent_guard_rails import GuardRails, RuleResult

def make_rails_for_context(channel: str) -> GuardRails:
    base_rules = [
        "strip_ai_preamble",
        "no_empty",
        "no_repetition",
        "max_length:3000",
    ]

    if channel == "web":
        base_rules.append("no_html_injection")

    if channel in ("public", "web", "mobile"):
        base_rules.append("no_pii")

    return GuardRails(base_rules)

web_rails = make_rails_for_context("web")

def respond_to_user(user_message: str, channel: str = "web") -> str:
    llm_text = call_llm(user_message)
    result = web_rails.apply(llm_text)

    if result.blocked:
        logger.warning("response_blocked", rule=result.rule_name, channel=channel)
        return "I couldn't generate a complete response. Please try again."

    return result.cleaned_text

Enter fullscreen mode Exit fullscreen mode


Sibling Libraries

Library What it solves
llm-output-validator Rule-based validation of structured output shape
llm-pii-redact Reversible PII redaction with restore support
tool-secret-scrubber Redact API keys from tool output
prompt-shield Pattern-based prompt-injection detection on inputs
agentvet Validate tool call arguments before execution

The combined pipeline: prompt-shield on user inputs (block injection attempts), agentvet on tool calls (validate args), agent-guard-rails on LLM outputs (clean and filter responses).


What's Next

Configurable action per rule: right now rules either block or clean. A warn action that logs but passes would be useful for auditing without blocking production traffic. This would make it easier to tune rules on real traffic before enabling blocking.

Rule composition operators: GuardRails(["no_pii AND no_html_injection", "max_length:2000"]) where AND means both must fail to block. Right now every rule failure blocks independently.

Telemetry integration: emit a structured event every time a rule fires with rule name, input length, and outcome. Feed this into your observability stack to understand how often guardrails trigger in production.


Built as part of the agent-stack family: composable Python primitives for production LLM agents.