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













