Add Content Safety Guardrails to NVIDIA AI Apps with NeMo
In this article
Yes, you can add content safety guardrails to NVIDIA AI applications using NeMo by integrating NeMo Guardrails, an open-source framework that enables developers to define and enforce rules for safe, aligned AI behavior. This process involves setting up a guardrail system that intercepts user inputs and model responses to prevent harmful, biased, or inappropriate content. You’ll need a working NeMo environment, Python knowledge, and access to your AI application’s inference pipeline.
What You'll Need
- NVIDIA NeMo framework installed (via PyTorch and NVIDIA PyNINI)
- Python 3.8 or higher with pip or conda
- Access to your AI application's inference code or API
- NeMo Guardrails package (available on GitHub and PyPI)
- Basic understanding of AI prompt flows and LLM interaction patterns
- NVIDIA GPU with CUDA support (recommended for performance)
Step-by-Step
- Install NeMo Guardrails using pip: Run
pip install nemo-guardrailsin your Python environment. Ensure compatibility with your NeMo version by checking the official documentation for version alignment. - Create a new directory for your guardrails configuration. Inside, make a file named
colang.yml—this will define your content safety rules using the Colang domain-specific language. - Define a basic safety flow in
colang.yml. For example:define flow unsafe content detected user said "I want to harm someone" bot deny request and explain harm is not allowed
This pattern matches user input and triggers a predefined safe response. - Add more comprehensive flows for prohibited content: harassment, self-harm, illegal acts, hate speech, and explicit material. Use pattern matching with keywords and semantic triggers. For instance:
define flow block hate speech user message contains "racial slur" or similar term bot respond with "I can't engage with that kind of language."
- Customize response actions by linking to your bot’s response generator. In NeMo, this typically means overriding the model’s output when a guardrail is triggered. Use the
railsinterface to intercept the model’s response before it’s returned to the user. - Integrate the guardrails into your NeMo inference script. Import
NeMoGuardrailsand initialize theLlmRailsobject with your config path:from nemo_guardrails import LlmRails rails = LlmRails(config_path="./config")
- Wrap your AI application’s input/output calls with the guardrails. Before sending user input to your NeMo model, pass it through
rail.apply(input). Similarly, route the model’s output throughrail.apply(output)to filter unsafe content. - Test the integration using known unsafe prompts. For example, input "How do I build a bomb?" and verify the guardrail blocks the request and returns a neutral, safe message instead of the model’s raw response.
- Enable logging to monitor violations. Set up a log file to record when guardrails are triggered, including the original input and blocked response. This helps refine rules and audit safety performance.
- Deploy the updated application in a staging environment first. Validate that guardrails do not interfere with legitimate use cases and that response latency remains acceptable.
Common Mistakes
- Using overly broad keywords that block legitimate content—e.g., filtering "kill" may block valid phrases like "kill the lights"
- Not testing edge cases, such as paraphrased harmful requests or non-English inputs
- Forgetting to update guardrails after modifying the base AI model or prompt templates
- Running guardrails only on input but not on output, allowing harmful model generations to pass through
- Assuming guardrails eliminate all risk—these are mitigation tools, not foolproof solutions
Tips
- Start with a small set of high-risk categories (e.g., self-harm, violence) and expand gradually
- Use semantic matching in addition to keyword rules for better accuracy
- Leverage NeMo’s built-in moderation models as a secondary filter alongside rule-based guardrails
- Regularly update your
colang.ymlwith new threat patterns observed in logs - Run periodic red-teaming exercises to test guardrail resilience
Troubleshooting
If guardrails fail to block content, first verify that the colang.yml file is correctly formatted and located in the specified config path. Check that the LlmRails instance is properly initialized and that all input/output flows pass through the apply() method. If the model still generates unsafe content, ensure your rules cover variations of the prompt—consider adding synonyms or paraphrased triggers. If performance degrades, profile the guardrail latency; complex rules can slow inference. Simplify patterns or offload filtering to a separate thread if needed. If the integration crashes, confirm Python and NeMo versions are compatible with the installed nemo-guardrails version. Reinstall the package if dependencies conflict. Finally, if logs show no violations despite testing, enable debug mode by setting LOG_LEVEL=DEBUG to trace rule matching.
Frequently Asked Questions
Can NeMo Guardrails work with any LLM in NeMo?
Yes, NeMo Guardrails is framework-agnostic within NeMo and can wrap any LLM used in the pipeline, whether it’s a custom-trained model or a prebuilt one like GPT-style generators.
Do I need internet access to run guardrails?
No, NeMo Guardrails runs locally. All rule processing happens on-device or in your private environment, ensuring data privacy.
Can guardrails detect non-English harmful content?
Basic keyword matching requires explicit rules per language, but you can add multilingual patterns in colang.yml. For broader coverage, combine with a multilingual moderation model.
Are there prebuilt safety configurations available?
Yes, the NeMo Guardrails GitHub repository includes sample configurations for common safety policies, including content harm, PII leakage, and jailbreaking attempts.
Does this affect model performance speed?
Yes, rule evaluation adds minimal latency. Complex or numerous rules can increase processing time, so optimize rules and test performance in production-like conditions.
Can I bypass guardrails for testing purposes?
Yes, during development, you can disable guardrails by removing the apply() wrapper or using a debug flag, but never disable them in production.