GuardrailRule

Models & Types · AiDotNet.Safety.Guardrails

Defines a declarative guardrail rule with a condition expression and corresponding action.

For Beginners

A rule is like an if-then statement for safety. The condition says “if the text contains X” and the action says “then do Y (block, warn, log, or modify).” You can build complex safety policies by combining multiple rules.

How It Works

A guardrail rule pairs a condition (what to detect) with an action (what to do about it). Rules can check for regex patterns, keyword lists, length limits, or custom predicates. Unlike CustomRule which uses raw delegates, GuardrailRule uses a structured format that enables serialization, logging, and composition.

References:

  • Guardrails AI: Structured validators (2024)
  • NeMo Guardrails: Colang rule language (NVIDIA, 2024)

Constructors

Constructor Summary
GuardrailRule()

Properties

Property Summary
Func<string, bool> CustomPredicate { get; set; } Gets or sets an optional custom predicate for Custom rules.
GuardrailConditionType ConditionType { get; set; } Gets or sets the type of condition this rule uses.
GuardrailDirection Direction { get; set; } Gets or sets the direction this rule applies to.
SafetyAction Action { get; set; } Gets or sets the action to take when the condition is met.
SafetyCategory Category { get; set; } Gets or sets the safety category to assign when the rule triggers.
SafetySeverity Severity { get; set; } Gets or sets the severity to assign when the rule triggers.
bool CaseInsensitive { get; set; } Gets or sets whether pattern matching is case-insensitive.
string Description { get; set; } Gets or sets the human-readable description of what this rule checks.
string Name { get; set; } Gets or sets the unique name for this rule.
string[] Patterns { get; set; } Gets or sets the patterns or keywords to check for (interpretation depends on ConditionType).

Methods

Method Summary
bool Evaluate(string text) Evaluates this rule against the given text.