Skip to content

Guardrails Integrations

Krishna Kishor Tirupati edited this page Jul 23, 2026 · 1 revision

Guardrails Integrations

PolicyAware can orchestrate NeMo Guardrails, Guardrails AI, or custom internal validators as part of one governed AI execution flow.

PolicyAware remains the control plane:

  • deny-by-default policy
  • risk classification
  • model routing
  • audit traces
  • evaluation
  • compliance evidence
  • local code scanning

Guardrails libraries become optional adapters:

  • NeMo Guardrails for conversational rails and dialog safety.
  • Guardrails AI for structured output validation and validators.
  • Custom guards for internal enterprise validation.

Install

Base install:

pip install policyaware

Optional guardrails integrations:

pip install "policyaware[nemo]"
pip install "policyaware[guardrails-ai]"
pip install "policyaware[full]"

Core API

API Purpose
GuardrailResult Normalized allow/block/transform result.
GuardrailAdapter Adapter protocol for input and output guards.
NeMoGuardrailsAdapter Optional adapter for NVIDIA NeMo Guardrails.
GuardrailsAIAdapter Optional adapter for Guardrails AI.
Gateway.add_input_guard(...) Run guard before model routing/execution.
Gateway.add_output_guard(...) Run guard after model output.
Gateway.add_guard(...) Run the same guard for input and output phases.

YAML-Driven Guards

PolicyAware policies can declare guard usage as policy-as-code.

id: full_stack_guardrails_policy
schema_version: "0.2"
default: deny

guards:
  input:
    - name: nemo
      config_path: rails/
      when:
        request.task_type: chatbot

  output:
    - name: guardrails_ai
      rail_spec: guardrails/spec.rail
      when:
        request.output_format: json

rules:
  - name: allow_support
    effect: allow
    when:
      user.role_in: [support_agent, developer]
      request.risk_in: [low, medium]

Known guard names:

Guard Name Adapter
nemo NeMoGuardrailsAdapter
nemoguardrails NeMoGuardrailsAdapter
guardrails_ai GuardrailsAIAdapter
guardrails-ai GuardrailsAIAdapter
guardrails GuardrailsAIAdapter

Custom Guardrails

For internal enterprise validators, add custom: true in YAML and register the guard object in Python.

guards:
  input:
    - name: internal_safety
      custom: true
      when:
        request.task_type: chatbot
from policyaware import Gateway, GatewayRequest, GuardrailResult, PolicyEngine


class InternalSafetyGuard:
    name = "internal_safety"

    def inspect_input(self, request: GatewayRequest) -> GuardrailResult:
        if "forbidden action" in request.prompt_text.lower():
            return GuardrailResult(
                name=self.name,
                allowed=False,
                reason="Blocked by internal safety guard.",
                score=0.0,
            )
        return GuardrailResult(name=self.name)

    def inspect_output(self, request: GatewayRequest, output_text: str) -> GuardrailResult:
        return GuardrailResult(name=self.name)


gateway = Gateway(
    policy_engine=PolicyEngine.from_file("policy.yaml"),
    guard_registry={"internal_safety": InternalSafetyGuard()},
)

NeMo Guardrails Example

from policyaware import Gateway, GatewayRequest, NeMoGuardrailsAdapter

gateway = Gateway.from_policy_file("policy.yaml")
gateway.add_input_guard(
    NeMoGuardrailsAdapter(config_path="rails/")
)

response = gateway.chat(
    GatewayRequest(
        tenant="acme",
        app="chatbot",
        user={"id": "u_123", "role": "support_agent"},
        context={"region": "us", "risk": "low", "task_type": "chatbot"},
        messages=[{"role": "user", "content": "Help me with this account question."}],
    )
)

print(response.policy.decision)
print(response.metadata["guardrails"])

Guardrails AI Example

from policyaware import Gateway, GatewayRequest, GuardrailsAIAdapter

gateway = Gateway.from_policy_file("policy.yaml")
gateway.add_output_guard(
    GuardrailsAIAdapter(rail_spec="guardrails/spec.rail")
)

response = gateway.chat(
    GatewayRequest(
        tenant="acme",
        app="structured-output-agent",
        user={"id": "u_456", "role": "analyst"},
        context={
            "region": "us",
            "risk": "medium",
            "task_type": "structured_output",
            "output_format": "json",
        },
        messages=[{"role": "user", "content": "Return a validated JSON summary."}],
    )
)

print(response.content)
print(response.metadata["guardrails"])

CLI

List guards declared in a policy:

policyaware guards list examples/full-stack-guardrails/policy.yaml

Run the full-stack guardrails demo:

cd examples/full-stack-guardrails
python demo.py

Expected output:

decision=allow
content=validated output
input_guard=demo_guard
output_guard=demo_guard

Execution Order

GatewayRequest
  -> data protection
  -> ML signals
  -> risk classification
  -> YAML policy decision
  -> input guard adapters
  -> model routing
  -> model provider
  -> output guard adapters
  -> runtime evaluation
  -> audit trace
  -> GatewayResponse

Guard Results

Guard results are recorded in:

response.metadata["guardrails"]

Shape:

{
  "input": [
    {
      "name": "nemo",
      "allowed": true,
      "transformed_text": null,
      "reason": "Allowed by NeMo Guardrails.",
      "score": 1.0,
      "metadata": {}
    }
  ],
  "output": [
    {
      "name": "guardrails_ai",
      "allowed": true,
      "transformed_text": null,
      "reason": "Allowed by Guardrails AI.",
      "score": 1.0,
      "metadata": {}
    }
  ]
}

Local Code Scan Coverage

policyaware scan now checks guardrail usage too.

It detects:

  • direct NeMo Guardrails / Guardrails AI usage outside PolicyAware orchestration
  • NeMo guard policy missing config_path or config
  • Guardrails AI guard policy missing rail_spec, rail, or spec
  • custom guard without custom: true
  • guard policy without a when condition

Example:

policyaware scan . --format html,json,sarif,markdown

Finding category:

Guardrails Integration

Compliance area:

Guardrails Orchestration

Related Pages

Clone this wiki locally