Skip to main content

Guardrails

Guardrails let you intercept and filter AI responses before they are converted to speech. Use them to block inappropriate content, enforce compliance, or replace sensitive responses.

Basic Usage

Guardrail Interface

Creating Guardrails

Use the guardrail() static method for convenience:
The default replacement is "I'm sorry, I can't respond to that." when not specified.

Blocked Terms

Blocked terms are matched case-insensitively against the AI response text:

Custom Check Function

For more complex filtering, use a check function that receives the full response text and returns true to block it:

Combining Blocked Terms and Check

A guardrail triggers if either blockedTerms match or the check function returns true. Blocked terms are evaluated first:

Multiple Guardrails

You can stack multiple guardrails. They are evaluated in order, and the first match triggers:

How It Works

  1. The AI model generates a text response.
  2. Before TTS conversion, each guardrail is checked in order.
  3. If a guardrail triggers, the original response is discarded and the replacement text is spoken instead.
  4. If no guardrail triggers, the original response is spoken normally.
The guardrail name is logged when triggered, so you can monitor which guardrails are firing in production.