Guardrails
Guardrails evaluate and filter AI inputs and responses for safety, compliance, and quality enforcement.
A guardrail is a collection of rules that evaluate AI content at runtime to prevent unsafe, non-compliant, or low-quality responses. Guardrails run in the pre-generate phase for input filtering and the post-generate phase for response validation. Together, these phases provide comprehensive control over what goes into and comes out of AI operations.
What Guardrails Store
Id
Unique identifier (GUID)
Alias
Unique string for programmatic lookup
Name
Display name shown in the backoffice
Rules
Ordered collection of evaluation rules
Version
Current version number (for version history)
Guardrail Rules
Each guardrail contains one or more rules. A rule references a registered evaluator and specifies when and how it is applied.
Id
Unique identifier (GUID)
EvaluatorId
The registered evaluator to use (e.g., "contains")
Name
Display name for the rule
Phase
When the rule runs: PreGenerate or PostGenerate
Action
What happens when flagged: Block, Warn, or Redact
Config
Evaluator-specific configuration (JSON)
SortOrder
Controls evaluation order within the guardrail
Phases
Rules specify when they run in the AI pipeline:
PreGenerate
Evaluated before the request is sent to the AI provider.
PostGenerate
Evaluated after the AI provider returns a response.
Actions
Rules specify what happens when content is flagged:
Block
Stop processing and throw AIGuardrailBlockedException.
Warn
Allow the content through unchanged and log a warning.
Redact
Replace flagged content with [REDACTED] before it reaches the AI model or caller.
Example Guardrail Configurations
content-safety
General safety
Contains for competitor brands (Block), Regex for SSNs (Block)
brand-compliance
Brand voice enforcement
LLM Safety Judge with brand criteria (Warn)
data-protection
Data protection
Regex for emails pre-generate (Redact), Regex for phone numbers post (Redact)
quality-assurance
Output quality
LLM Safety Judge for accuracy (Warn)
Built-in Evaluators
Umbraco.AI ships with these evaluators:
Contains
contains
Code-based
Yes
Flags content containing a specific substring. Config: SearchPattern, IgnoreCase
Regex Match
regex
Code-based
Yes
Flags content matching a regular expression pattern. Config: Pattern, IgnoreCase, Multiline
LLM Safety Judge
llm-judge
Model-based
No
Uses an AI model to evaluate content against configurable criteria. Config: ProfileId, EvaluationCriteria, SafetyThreshold
Decision Safety Judge
decision-judge
Model-based
No
Asks a Decision profile whether content is safe by configurable criteria (experimental). Config: ProfileId, EvaluationCriteria, SafetyThreshold
Evaluator Types
CodeBased
Deterministic evaluation using patterns and rules. Fast, runs during streaming
ModelBased
Uses an AI model for evaluation. More nuanced, runs after stream completes
Using Guardrails in Code
Getting a Guardrail
Creating a Guardrail
This example creates a guardrail that combines a Block rule for competitor brand mentions with a Redact rule for email addresses:
Assigning Guardrails to a Profile
Guardrails are assigned to chat profiles via GuardrailIds in AIChatProfileSettings:
How Guardrail Evaluation Works
When an AI operation executes, the guardrail middleware resolves all applicable guardrails and runs pre-generate rules against the input before it reaches the AI provider, then runs post-generate rules against the response. Rules flagged with Block throw an AIGuardrailBlockedException, rules with Redact replace matched text with [REDACTED], and rules with Warn allow content through and log a warning. Action precedence when multiple rules flag the same content: Block > Redact > Warn.
Streaming Behavior
During streaming responses:
Code-based evaluators (Contains, Regex Match) hold back a trailing 100-character window of the response. They don't release text to the caller the instant it arrives.
Each new chunk re-checks the whole held-back window. This catches a pattern split across two provider chunks before any of it is released.
Block throws before the held-back text is released. Flagged content never reaches the caller.
Redact replaces matches with
[REDACTED]inside the window before release. It works the same way it does for a non-streamed response.A match longer than the window can still leak partially. This is an inherent limit of scanning a live stream instead of a completed response. A rule's
Actiondoesn't control this.
Model-based evaluators (LLM Safety Judge) still run only after the stream completes. They evaluate the full aggregated response.
A Block or Redact action on a model-based post-generate rule can't stop already-streamed content. It can only fail the request afterwards.
Use a code-based evaluator instead if you need a guarantee during streaming.
Pre-generate Redact rules work normally during streaming. Redaction happens before the stream starts.
Handling Blocked Content
Managing Guardrails
You can create, edit, and delete guardrails through the backoffice. See Managing Guardrails for step-by-step instructions. For programmatic management, see the IAIGuardrailService reference.
Version History
Guardrails support version history. Every time you save a guardrail, a new version is created. You can:
View the version history.
Compare different versions.
Roll back to a previous version.
See Version History for more information.
Related
Profiles - Assign guardrails to chat profiles
Managing Guardrails - Backoffice guide
Last updated
Was this helpful?