Skip to content

API Reference parrot.bots.guardrails.base

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.bots.guardrails.base

View source

Core data models and abstractions for the AI-Parrot guardrails infrastructure.

Defines the guardrail stages, verdicts, result/context carriers, and the Guardrail abstract base class that every built-in and custom guardrail implements. See sdd/specs/guardrails-infrastructure.spec.md (FEAT-396) for the full design.

Class Guardrail

Abstract base class for all guardrails.

A guardrail inspects (and optionally transforms, flags, or blocks) content at one or more pipeline stages. Concrete subclasses set name, stages, and priority and implement check().

Default priority bands (lower runs first within a stage): - 0-99: sanitizers (e.g. prompt-injection detection). - 100-199: transformers (e.g. secrets/PII redaction). - 200+: observers (e.g. moderation flags, groundedness scoring).

Attributes:

Attribute Type Description
name str Unique identifier for this guardrail (used in telemetry and AIMessage.metadata["guardrails"] report keys).
stages set[GuardrailStage] The set of stages at which this guardrail runs.
priority int | dict[GuardrailStage, int] Execution order within a stage; lower runs first. May be a mapping that assigns a distinct priority to each stage.
on_error Literal['fail_open', 'fail_closed'] Error-handling policy for unhandled exceptions raised by check(). "fail_open" (default) logs and continues as if the guardrail returned PASS; "fail_closed" treats the error as a BLOCK.
requires_system_prompt bool

Method Guardrail.priority_for

Resolve this guardrail's effective priority for stage.

A scalar priority applies to every stage (pre-FEAT-761 behavior). A {stage: int} mapping lets one guardrail sit in different bands per stage; a stage missing from the mapping (or stage=None) resolves to the mapping's lowest value.

priority_for(stage: GuardrailStage | None) -> int

Parameter Type Default Description
stage GuardrailStage | None The stage being ordered, or None for a stage-agnostic order.

Returns: int — The integer priority (lower runs first).

Method Guardrail.check

Inspect (and optionally act on) content for ctx.stage.

check(content: str, ctx: GuardrailContext) -> GuardrailResult

Parameter Type Default Description
content str The text content to inspect.
ctx GuardrailContext Contextual information about the current call.

Returns: GuardrailResult — A GuardrailResult describing the verdict.

Class GuardrailAction

Verdict returned by a guardrail's check() call.

Attributes:

Attribute Type Description
PASS Content is unchanged; no action taken.
TRANSFORM Content was rewritten; see GuardrailResult.content.
FLAG Content passes but is annotated; see GuardrailResult.report.
BLOCK Content is rejected; see GuardrailResult.reason.

Class GuardrailContext

Contextual information passed to a guardrail's check() call.

Fields:

Field Type Default Description
stage GuardrailStage The pipeline stage this context was built for.
agent_name str Name of the bot/agent invoking the guardrail.
user_id str | None None Identifier of the requesting user, if known.
session_id str | None None Identifier of the current conversation/session, if known.
method str '' The bot entrypoint that triggered this check (e.g. "invoke", "ask", "ask_stream").
tool_name str | None None Name of the tool whose output is being checked. Only populated at GuardrailStage.TOOL_OUTPUT.
extras dict[str, Any] Stage-specific additional data (e.g. the outgoing AIMessage at GuardrailStage.OUTPUT).

Class GuardrailResult

Outcome of a single guardrail's check() call.

Fields:

Field Type Default Description
action GuardrailAction The verdict for this guardrail run.
content str | None None The transformed content, populated when action is TRANSFORM.
report dict[str, Any] | None None Structured annotation data, populated when action is FLAG. Attached to AIMessage.metadata["guardrails"][<name>].
reason str | None None A category label (never the offending content) explaining a BLOCK verdict.

Class GuardrailStage

Pipeline stage at which a guardrail runs.

Attributes:

Attribute Type Description
INPUT User text, before it reaches the LLM.
TOOL_OUTPUT A tool's result, before it is returned to the agent loop.
OUTPUT The final answer, before it is returned to the caller.
OUTPUT_STREAM Individual chunks emitted while streaming a response.
TOOL_CALL Before tool execution; intercepts the call before guards/body.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally