How to beginner · 3 min read

How to set up NeMo Guardrails colang

Quick answer
To set up NeMo Guardrails with colang, install the nemo-guardrails Python package and define your guardrails using the colang DSL files. Then, initialize the Guardrails class in Python pointing to your colang files to enforce safety and control in your AI chat applications.

PREREQUISITES

  • Python 3.8+
  • pip install nemo-guardrails
  • Basic knowledge of Python and YAML
  • OpenAI API key or compatible LLM API key

Setup

Install the nemo-guardrails package via pip and prepare your environment variables for your LLM API key. Create colang files to define your guardrails rules and flows.

bash
pip install nemo-guardrails

Step by step

Create a colang file defining your conversational guardrails, then load it in Python to run a guarded chat session.

python
from nemo_guardrails import Guardrails

# Path to your colang file defining the guardrails
colang_file = "example_guardrails.colang"

# Initialize Guardrails with the colang file
rails = Guardrails(colang_file=colang_file)

# Example user input
user_input = "Hello, can you help me?"

# Run the guardrails chat
response = rails.chat(user_input)
print("AI response:", response)
output
AI response: Hello! How can I assist you today?

Common variations

  • Use async API with await rails.chat_async() for asynchronous calls.
  • Integrate with OpenAI or other LLMs by setting environment variables like OPENAI_API_KEY.
  • Customize guardrails by editing colang files to add constraints, safety checks, or response formatting.
python
import asyncio

async def async_chat():
    response = await rails.chat_async("Tell me a joke.")
    print("Async AI response:", response)

asyncio.run(async_chat())
output
Async AI response: Why did the chicken cross the road? To get to the other side!

Troubleshooting

  • If you see errors loading colang files, verify the file path and syntax.
  • For API authentication errors, ensure your environment variable for the LLM API key is set correctly.
  • If responses are not constrained as expected, check your guardrails definitions for completeness and correctness.

Key Takeaways

  • Install nemo-guardrails and define your conversational rules in colang files.
  • Initialize Guardrails in Python with your colang file to enforce AI safety and control.
  • Use async methods and customize guardrails for flexible, safe AI interactions.
  • Always verify environment variables and guardrails syntax to avoid runtime errors.
Verified 2026-04
Verify ↗