latent.guardrails.decorators¶
Functions¶
discover_guardrails¶
Scan an instance for @guardrail methods.
Factory methods ((self) signature) are called once to obtain scanner
instances. Inline methods are wrapped as scanner-protocol objects.
The class-level method list is cached after the first dir() walk.
Returns (pre_scanners, post_scanners) or None if no guardrails found.
guardrail¶
guardrail(timing: Timing = 'pre', outcome: Outcome = 'active', threshold: float = 0.5, on_error: OnError = 'ignore', message: str = 'Request blocked by guardrail.', every: int = 0, on_block: Callable | None = None) -> Callable
Mark an agent method as a guardrail.
Signature detection:
- (self) → factory, returns a scanner instance, called once on init
- (self, prompt) → pre inline logic
- (self, prompt, output) → post inline logic
Args:
timing: "pre" (check input) or "post" (check output).
outcome: "active" (block/retract) or "passive" (notify only).
threshold: Score threshold for float-returning rules.
on_error: "ignore" (swallow errors) or "raise" (propagate).
message: User-facing message when blocked.
every: For post rules during streaming — 0=response-only, N=every N tokens.
on_block: Optional callable (GuardrailBlockContext) -> str — both sync
and async are supported — invoked when an active rule blocks. Its
return value replaces the static message as the blocked_response.
Use this to terminate conversations, escalate, or run arbitrary
side-effects on block.