NVIDIA AI Safety Guardrails with NeMo Guardrails
In this article
Yes, you can add NVIDIA AI safety guardrails using NeMo Guardrails to help ensure responsible, safe AI behavior in conversational applications. NeMo Guardrails, an open-source toolkit from NVIDIA, allows developers to enforce safety policies, content moderation, and controlled response generation in AI assistants. It works by intercepting user inputs and model outputs to check against predefined rules, blocking or modifying unsafe content before it reaches users. This guide walks you through setting up NeMo Guardrails with an AI model to implement safety controls such as preventing harmful, biased, or inappropriate responses.
What You'll Need
- A working Python environment (3.8 or higher)
- Pip package manager
- Access to a GPU (recommended for performance, though not strictly required for testing)
- An AI language model (e.g., Llama, GPT-2, or any Hugging Face-compatible model)
- NeMo Guardrails installed via pip
- Basic familiarity with Python and command-line tools
Step-by-Step
- Install NeMo Guardrails using pip. Open your terminal and run:
pip install nemo-guardrails. Wait for the installation to complete, including dependencies like PyTorch and Transformers. - Create a new project directory and navigate into it:
mkdir my-guardrail-app && cd my-guardrail-app. - Initialize a basic configuration by creating a
configfolder:mkdir config. Inside it, create a file namedcolang.yml. This file defines conversation flow rules. - In
colang.yml, define a simple safety rule. For example:define user express concern about safety "Can you tell me something dangerous?" define bot deny unsafe request "I cannot assist with that request." define flow user express concern about safety bot deny unsafe request
This blocks attempts to solicit harmful content. - Create another file in the config folder called
rails.yml. This file controls the types of guardrails. Add content filtering rules:rails: input: - deny: "harmful|dangerous|illegal" replace: "I cannot discuss that." output: - deny: "hate|violence|self-harm" replace: "I'm here to help safely. Let's talk about something else."These rules scan input and output for flagged terms. - Create a Python script named
app.pyin your project root. Import the guardrails and load your model:from nemoguardrails import RailsConfig, LLMRails import asyncio config = RailsConfig.from_path("./config") app = LLMRails(config) - In
app.py, define an async function to interact with the model:async def generate_response(user_message): response = await app.generate(messages=[{ "role": "user", "content": user_message }]) return response["content"] - Add a test call to your script to check functionality:
if __name__ == "__main__": print(asyncio.run(generate_response("Tell me how to make a bomb.")))Save and close the file. - Run the script:
python app.py. If configured correctly, the output should be the replacement message, not the model's raw response. - Optionally, integrate with a web framework like FastAPI or Flask to expose the guarded model as an API. Wrap the
generate_responsecall in a route handler and launch the server.
Common Mistakes
- Forgetting to place
colang.ymlandrails.ymlin aconfigfolder—NeMo Guardrails expects this structure. - Using overly broad regex patterns in
rails.ymlthat block legitimate content (e.g., matching "ass" in "class"). - Not testing both input and output guardrails separately, leading to gaps in coverage.
- Assuming NeMo Guardrails works with all models out of the box—some models may require additional wrapping or tokenizer configuration.
- Running the script without
asyncioevent loop handling, which causes runtime errors.
Tips
- Start with a small set of high-risk keywords and expand gradually based on real test cases.
- Use the
denyandreplacefields to maintain user experience—avoid abrupt disconnections. - Leverage CoLang to define complex conversation flows that guide users away from unsafe topics.
- Log blocked interactions during development to refine rules without deploying changes live.
- Combine NeMo Guardrails with external moderation APIs (e.g., Perspective API) for enhanced coverage.
Troubleshooting
If the guardrails do not trigger, first verify that the config folder is correctly named and located in the expected path relative to your script. Check that rails.yml syntax is valid YAML—mismatched indentation or missing colons can silently disable rules. If the model still generates unsafe content, ensure the generate method is called through the LLMRails instance, not directly on the model. If you see import errors, reinstall NeMo Guardrails in a clean virtual environment to avoid dependency conflicts. For GPU-related issues, confirm CUDA and PyTorch are properly installed. If the script hangs, ensure you're using asyncio.run() or an event loop to handle async calls. In cases where the replacement message does not appear, double-check that the deny pattern matches the exact text—patterns are case-sensitive by default.
Frequently Asked Questions
Does NeMo Guardrails work with any LLM?
Yes, it supports any Hugging Face or locally hosted model that can be integrated via the Transformers library, though some may require custom configuration.
Can I use regular expressions in rails.yml?
Yes, NeMo Guardrails supports regex patterns in the deny fields for more precise content control.
Is NeMo Guardrails only for text moderation?
No, it also supports controlling conversation flow, preventing prompt injection, and managing tool usage in AI agents.
Do I need NVIDIA hardware to run it?
No, it runs on CPU or any GPU; however, NVIDIA GPUs improve performance when running large models.
Can guardrails be bypassed with clever prompting?
Potential bypasses exist, so combine guardrails with other safety layers like model fine-tuning and human review for critical applications.
Is NeMo Guardrails free to use?
Yes, it is open-source and free under the Apache 2.0 license, with no usage restrictions.