HuggingFace Classifier Integration
Content moderation using HuggingFace text classification models on input, output, and retrieval flows.
Overview
Fast, prompt-free alternative to LLM-based self-check rails. Supports four inference backends:
Setup
For the local backend, install the required dependencies:
The model is downloaded on first use from HuggingFace Hub. For air-gapped environments, set HF_HUB_OFFLINE=1 and point model to a local path.
For remote backends, a running inference server is required. No additional Python dependencies are needed.
Colang 2.x requires an explicit import in your Colang file (e.g., config.co):
Colang 1.0 auto-discovers library flows.
Usage
Configuration Structure
Add the classifier configuration to your config.yml:
The $classifier parameter must match the name under rails.config.hf_classifier.
Configuration Options
Common fields (all engines)
Blocked Labels
Values must match the label strings returned by the model or server. For local and vLLM backends with text-classification, labels come from the model’s id2label mapping (e.g., "toxic", "LABEL_1"). For token-classification with aggregation_strategy, labels are entity groups with the B-/I- prefix stripped (e.g., "PER", "LOC"). For FMS, labels come from the detection_type field in the server response. For KServe, labels are stringified class indices ("0", "1").
To discover labels, inspect id2label from the model config:
For remote servers, send a test request and inspect the response.
Local engine fields
Remote engine fields (vllm, kserve, fms)
Input Rails
Prompt injection detection using KServe:
Output Rails
HAP detection using FMS:
Retrieval Rails
The retrieval rail classifies the combined retrieved text as a single input. If any blocked label is detected above threshold, all retrieved chunks are cleared.
Complete Example
HAP (FMS), prompt injection (KServe), and language classification (vLLM) with streaming:
Return Value
Returns True if allowed, False if blocked. Triggered labels and scores are logged at INFO level:
mTLS and Custom CA
HF Classifier Rail Behavior
When blocked, input and output rails respond with "I'm sorry, I can't respond to that." and abort. If enable_rails_exceptions is set, an InputRailException or OutputRailException is raised instead. Retrieval rails clear all retrieved chunks if any blocked label is detected. With streaming enabled, the output rail checks the accumulated response after streaming completes.
Engine Support
hf classifier check retrieval clears the retrieved chunks when a blocked label is detected, which rewrites relevant_chunks.
IORails has no retrieval pipeline and nowhere to apply that rewrite, so the flow runs on LLMRails only.
This rail declares transformers as an optional dependency for its in-process backend.
IORails enforces this dependency only when the configuration selects the local backend.
With a vLLM, KServe, or FMS endpoint, the rail compiles without transformers installed.
LLMRails imports it lazily and fails on the first request instead.
For the full per-rail matrix, refer to Rail Engine Support.