liveBook · Manning

Guardrail

As explained in Building Reliable AI Systems

A constraint that defines behavior a model should or should not exhibit.

A guardrail constrains model behavior so responses stay within intended capabilities and safe operating boundaries. For an agent, it can keep a travel assistant focused on travel planning and away from sensitive or off-topic requests. section 6.4.2

A basic content filter can reject text that matches terms in blocked categories, such as medical, legal, or personal-data topics. Keyword matching is brittle: users can bypass it with synonyms or creative phrasing. Semantic evaluation and multilayer safety systems are identified as more advanced approaches. section 6.4.2

from langchain_core.prompts import PromptTemplate
from typing import List

def content_filter(text: str, blocked_categories: dict) -> bool:     #1
   """Filter content for safety and relevance"""
   for category, terms in blocked_categories.items():
       if any(term.lower() in text.lower() for term in terms):
           return False
   return True

blocked_categories = {                                              #2
   "off_topic": ["crypto", "gambling", "medical", "legal"],
   "sensitive": ["personal data", "financial", "private"],
   "inappropriate": ["adult content", "offensive", "harmful"]
}

template = """
You are a travel assistant. Keep responses:
- Focused on travel planning
- Free of sensitive/personal information
- Safe and appropriate

Input: {user_input}
Reasoning:
"""

prompt = PromptTemplate(template=template, input_variables=["user_input"])

query = "Help me invest in crypto while traveling"                #3
agent_output = "Let me show you some crypto investment tips..."      #4

if not content_filter(agent_output, blocked_categories):
   print("Content violation. Please keep requests travel-related.")

The Python example defines blocked categories, checks input text for matching terms, and sets a travel-focused prompt. section 6.4.2

How Building Reliable AI Systems covers it

How deep it goes
Define boundaries and demonstrate keyword filtering, while warning that brittle matching falls short of semantic, multilayer safety.
Examples
Filter categories such as crypto, medical, legal, personal data, and harmful content in a travel-assistant scenario.
What you can do after
Build a basic Python keyword filter that rejects text matching blocked categories, while recognizing its synonym-bypass weakness.
How it connects
Connect guardrails with prompt engineering, hallucination prevention, and reliable outputs.

Also explained in

Compare how 14 books explain guardrail →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Reliable AI Systems. The section links point to the parts of the book it draws on.