Phrase Matcher Engine

gllm-guardrailarrow-up-right | Tutorial: Guardrail | Engine: Phrase matcher | API Referencearrow-up-right

What it does

PhraseMatcherEngine is a lightweight, rule-based engine that blocks content when it contains any configured banned phrases.

Key behavior:

  1. The same banned phrases list is used for both input and output checks.

  2. Matching uses spaCy PhraseMatcher if enabled and available, otherwise it falls back to a regex-based matcher.

Cases it can handle

1) Banned phrases

Examples of phrases you might ban:

  1. Disallowed instructions (e.g., "make a bomb")

  2. Sensitive terms or internal keywords (e.g., "secret password")

2) Detecting simple patterns (prefix-style)

This engine is phrase-based, but you can still catch many “pattern-like” strings by banning a distinctive prefix.

Example: if you want to filter API keys that look like sk-xxxxxxxx, you can add "sk-" to banned_phrases.

circle-exclamation

Use default config

By default:

  1. Engine mode is GuardrailMode.INPUT_ONLY (checks input only).

  2. If spaCy is not installed, the engine uses regex matching automatically.

Use custom config

Custom banned phrases

Check both input and output

Using spaCy PhraseMatcher

1) Install the optional dependency

2) Download a spaCy model

3) Enable spaCy mode in the engine

circle-info

If spaCy fails to initialize (missing model, incompatible environment, etc.), the engine automatically falls back to regex matching.

Last updated

Was this helpful?