Appearance
Guardrails
guardrails checks what goes in and out of an agent, and which tools each user role may call. It sits on the Agent block next to model and tools, either as a named type: GuardrailSystem block or the same map written inline.
The checks themselves come from elsai-guardrails. This page covers the YAML for them.
Installation
bash
pip install --upgrade --extra-index-url https://elsai-agents.elsai.ai/root/ "elsai-yaml[guardrails]==0.4.3"This installs elsai-guardrails 0.1.8.
When the checks run
| Key | Default | When it runs |
|---|---|---|
input_checks | true | On the latest user message, before each model call |
output_checks | true | On the assistant's reply text. Turns that call a tool are skipped |
tool_authorization | Off | Before each tool call. See Tool authorization |
A blocked input raises ValueError, and the message starts with GUARDRAIL BLOCKED:. A blocked output replaces the assistant text with GUARDRAIL BLOCKED: <reason>.
Available checks
Each check is turned on with its check_ key. The checks that have a block_ key only stop the message when that key is true.
| Check | Default | Related keys | What it catches |
|---|---|---|---|
check_toxicity | true | toxicity_threshold (0.0–1.0, default 0.7), block_toxic | Abusive, hateful, or threatening language scored above the threshold |
check_sensitive_data | true | block_sensitive_data | Email addresses, phone numbers, card numbers, social security numbers, IP addresses, and similar PII |
check_semantic | true | None | Jailbreaks, prompt injection, malicious requests, and code injection, matched by similarity |
check_off_topic | false | allowed_topics, block_off_topic | Messages that do not match any allowed topic |
check_sql_syntax | false | sql_dialect (default mysql) | SQL with syntax errors |
Turn off check_semantic for short prompts
check_semantic is on by default. Short, harmless prompts can match a jailbreak pattern, so set it to false unless you need it.
check_semantic and check_off_topic compare text with an embedding encoder. Pick the encoder with the ENCODER_TYPE environment variable. The default is a local HuggingFace model. See Semantic classification.
Toxicity
yaml
toxicity_rails:
type: GuardrailSystem
check_toxicity: true
toxicity_threshold: 0.7
block_toxic: true
check_sensitive_data: false
check_semantic: falseA lower threshold blocks more. 0.5 is strict; 0.9 is permissive.
Sensitive data
yaml
pii_rails:
type: GuardrailSystem
check_sensitive_data: true
block_sensitive_data: true
check_toxicity: false
check_semantic: falseJailbreak and prompt injection
yaml
injection_rails:
type: GuardrailSystem
input_checks: true
output_checks: false
check_semantic: true
check_toxicity: false
check_sensitive_data: falseOff-topic
Each topic has a name and a description. The user message is compared with the descriptions, so write them as the kind of question you expect.
yaml
topic_rails:
type: GuardrailSystem
output_checks: false
check_off_topic: true
block_off_topic: true
allowed_topics:
- name: Billing
description: Questions about invoices, refunds, payments, and plans
- name: Technical support
description: Help with installation, errors, and troubleshooting
check_semantic: falseSQL syntax
sql_dialect is one of postgresql, mysql, sqlserver, sqlite, mongodb, oracle, or redshift.
yaml
sql_rails:
type: GuardrailSystem
check_sql_syntax: true
sql_dialect: postgresql
check_semantic: falseContent checks example
agent.yaml:
yaml
openai:
type: OpenAIModel
model_id: gpt-4o-mini
content_rails:
type: GuardrailSystem
input_checks: true
output_checks: true
check_toxicity: true
toxicity_threshold: 0.7
block_toxic: true
check_sensitive_data: true
block_sensitive_data: true
check_semantic: false
check_off_topic: true
block_off_topic: true
allowed_topics:
- name: Billing
description: Questions about invoices, refunds, payments, and plans
support:
type: Agent
name: Billing support
system_prompt: You answer billing questions.
model: ${openai}
tools: []
guardrails: ${content_rails}python
from elsai.config import yaml_to_agent
agent = yaml_to_agent("agent.yaml", entry="support")
print(agent("How do I get a refund for last month's invoice?"))
try:
agent("My card number is 4111 1111 1111 1111, please store it.")
except ValueError as exc:
print(exc) # GUARDRAIL BLOCKED: ...Tool authorization
tool_authorization decides which tools a role may call. Names are tool names such as add or calculator, not import paths such as elsai_tools.calculator.
| Key | What it does |
|---|---|
enabled | Must be true for the check to run |
denied_tools | Blocked for every role |
sensitive_tools | Need approval on the call, and must still be allowed for the role |
roles.<name>.allowed_tools | The tools that role may call |
default_role | Used when the call does not pass a role |
The role comes from invocation_state["user_role"] on each call. A blocked tool does not raise. The model gets GUARDRAIL BLOCKED: ... as the tool result and can continue without it.
Set input_checks and output_checks to false when the block is only for tool authorization.
agent.yaml, with tools/add.py from Tools:
yaml
openai:
type: OpenAIModel
model_id: gpt-4o-mini
tool_rails:
type: GuardrailSystem
input_checks: false
output_checks: false
tool_authorization:
enabled: true
denied_tools:
- shell
sensitive_tools:
- calculator
roles:
analyst:
allowed_tools:
- add
engineer:
allowed_tools:
- add
- calculator
default_role: analyst
math:
type: Agent
name: Math
system_prompt: Use the tools for arithmetic.
model: ${openai}
tools:
- ./tools/add.py
- elsai_tools.calculator
guardrails: ${tool_rails}python
from elsai.config import yaml_to_agent
agent = yaml_to_agent("agent.yaml", entry="math")
# analyst may call add, but not calculator
print(agent("What is 2 + 3?", invocation_state={"user_role": "analyst"}))
# engineer may call calculator because it is approved on this call
print(agent(
"What is 15 * 3?",
invocation_state={
"user_role": "engineer",
"guardrail_metadata": {"approved": True},
},
))Content checks and tool authorization can share one block. The hidden structured-output tool is never checked, so do not list the Pydantic class name in allowed_tools.