Guardrail actions: RETRY, SKIP, and RAISE with GuardrailConfig
Choose exactly what happens when a guardrail fails
Not every failure wants the same response. GuardrailConfig pairs your validator with GuardrailAction: RETRY (ask the model to fix its output up to max_retries), SKIP (drop the bad step cleanly), or RAISE (abort loudly on critical compliance breaches).
Example
from praisonai import GuardrailConfig, GuardrailAction
def require_citation(text: str) -> tuple[bool, str]:
if '[DOC-' not in text:
return False, 'Missing [DOC-x] source citation'
return True, text
strict_cfg = GuardrailConfig(
validator=require_citation,
max_retries=2,
on_fail=GuardrailAction.RETRY,
)
print('Actions available:', [a.value for a in GuardrailAction])
print('Bad output check :', require_citation('Refunds take 5 days.'))
print('Good output check:', require_citation('Refunds take 5 days [DOC-2].'))
print('Configured policy:', strict_cfg.on_fail.value, '| max_retries:', strict_cfg.max_retries)Example explained
- GuardrailAction.RETRY — Feeds the failure reason back to the model so it self-corrects within max_retries.
- GuardrailAction.RAISE — Fails loudly immediately on security or regulatory violations.
Write actionable failure reasons
Returning (False, 'Missing [DOC-x] source citation') tells the model *exactly* how to fix its reply on retry — vague messages like 'invalid' waste retries.
Common pitfall: setting max_retries=10
If a model fails the same guardrail 2–3 times in a row, 7 more retries rarely help and multiply cost 10x. Cap max_retries at 2–3 and fall back cleanly.
Reference: documentation