GuardrailsAI Integration
NeMo Guardrails provides out-of-the-box support for GuardrailsAI validators, enabling comprehensive input and output validation using a rich ecosystem of community-built validators. GuardrailsAI offers validators for content safety, PII detection, toxic language filtering, jailbreak detection, topic restriction, and much more.
The integration provides access to both built-in validators and the entire Guardrails Hub ecosystem, allowing you to dynamically load and configure validators for your specific use cases.
Setup
To use GuardrailsAI validators, you need to install the guardrails-ai package:
You may also need to install specific validators from the Guardrails Hub:
Usage
The GuardrailsAI integration uses a flexible configuration system that allows you to define validators with their parameters and metadata, then reference them in your input and output rails.
Configuration Structure
Add GuardrailsAI validators to your config.yml:
Input Rails
To use GuardrailsAI validators for input validation:
Output Rails
To use GuardrailsAI validators for output validation:
Result Format in Colang Flows
The GuardrailsAI actions (validate_guardrails_ai_input and validate_guardrails_ai_output) return a dict that is stored in $result when used in flows. This dict contains:
validation_result: The raw GuardrailsAI validation outcome (e.g.,PassResultorFailResult).valid: A boolean derived from the GuardrailsAIvalidation_passedfield. Use this in flow conditions such asif not $result["valid"]to decide whether to block.
Built-in Validators
The integration includes support for the following validators that are pre-registered in the NeMo Guardrails validator registry. For detailed parameter specifications and usage examples, refer to the official GuardrailsAI Hub documentation for each validator:
competitor_check-hub://guardrails/competitor_checkdetect_jailbreak-hub://guardrails/detect_jailbreakguardrails_pii-hub://guardrails/guardrails_piione_line-hub://guardrails/one_lineprovenance_llm-hub://guardrails/provenance_llmregex_match-hub://guardrails/regex_matchrestricttotopic-hub://tryolabs/restricttotopictoxic_language-hub://guardrails/toxic_languagevalid_json-hub://guardrails/valid_jsonvalid_length-hub://guardrails/valid_length
Complete Example
Here’s a comprehensive example configuration:
Custom Validators from Guardrails Hub
You can use any validator from the Guardrails Hub by specifying its hub path:
The integration will automatically fetch validator information from the hub if it’s not in the built-in registry.
Performance Considerations
- Validators are cached to improve performance on repeated use
- Guard instances are reused when the same validator is called with identical parameters
- Consider the latency impact when chaining multiple validators
For a complete working example, see the GuardrailsAI example configuration.