windlass.providers.guardrails.rules¶
rules
¶
Rule-based guardrails — no dependencies, no model calls, no latency.
This is the guardrail you should reach for first. It catches the failure modes that actually occur in production — leaked PII, prompt injection in retrieved documents, banned terminology, competitor names in generated copy — using deterministic checks that add microseconds rather than a model round trip.
Layer :class:~windlass.providers.guardrails.nemo.NeMoGuardrail on top when you
need conversational policy, topical rails or a model-based classifier.
Example
guard = RuleGuardrail(pii=True, injection=True, on_violation="redact") guard.validate("email me at a@b.com") 'email me at [EMAIL]' guard.check("Ignore all previous instructions.").allowed False
RuleGuardrail
¶
RuleGuardrail(
*,
pii: bool = True,
pii_kinds: Sequence[str] | None = None,
injection: bool = True,
secrets: bool = True,
banned_words: Sequence[str] | None = None,
banned_patterns: Sequence[str] | None = None,
max_length: int | None = None,
required_patterns: Sequence[str] | None = None,
on_violation: str = "block",
stages: tuple[str, ...] = ("input", "output"),
**config: Any
)
Bases: Guardrail
Deterministic content policy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pii
|
bool
|
Detect personal data using
:func: |
True
|
pii_kinds
|
Sequence[str] | None
|
Which PII categories to check. |
None
|
injection
|
bool
|
Detect prompt-injection attempts. Recommended on the
|
True
|
secrets
|
bool
|
Detect leaked credentials. Recommended on the |
True
|
banned_words
|
Sequence[str] | None
|
Terms that must not appear. Matched case-insensitively on
word boundaries, so |
None
|
banned_patterns
|
Sequence[str] | None
|
Extra regular expressions to check. |
None
|
max_length
|
int | None
|
Reject content longer than this. A cheap defence against context-stuffing. |
None
|
required_patterns
|
Sequence[str] | None
|
Patterns that must be present, for output-format enforcement (a citation marker, a JSON envelope). |
None
|
on_violation
|
str
|
|
'block'
|
stages
|
tuple[str, ...]
|
Which stages this guardrail runs at. |
('input', 'output')
|
**config
|
Any
|
Forwarded to :class: |
{}
|
Performance
Pure regex; roughly microseconds per kilobyte. Safe to run on every request and on every retrieved chunk.
Note
Pattern matching catches known attack shapes, not novel ones. Treat this as defence in depth alongside least-privilege tool design and human approval for consequential actions — not as a complete solution to prompt injection.
Source code in src\windlass\providers\guardrails\rules.py
acheck
async
¶
acheck(
content: str, *, stage: str = "input", context: dict[str, Any] | None = None
) -> GuardrailResult
Run every enabled rule against content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
str
|
The text to inspect. |
required |
stage
|
str
|
|
'input'
|
context
|
dict[str, Any] | None
|
Extra signals. Ignored by this guardrail. |
None
|
Returns:
| Type | Description |
|---|---|
GuardrailResult
|
The verdict. |
GuardrailResult
|
found, so a |
Source code in src\windlass\providers\guardrails\rules.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | |