Skip to main content
NeMo Guardrails provides a straightforward Python API for adding programmable guardrails to your LLM-based applications. The API is async-first and integrates seamlessly with your existing code.

Core Classes

LLMRails

The LLMRails class is the main entry point for using guardrails programmatically.

Initialization

Constructor Parameters:
RailsConfig
required
A rails configuration loaded from a directory or created programmatically.
BaseLLM | BaseChatModel
default:"None"
An optional LLM engine to use. If provided, this will be used as the main LLM and will take precedence over any main LLM specified in the config.
bool
default:"False"
Whether the logging should be verbose or not.

RailsConfig

The RailsConfig class represents a guardrails configuration.

Loading Configuration

classmethod
Loads a RailsConfig from the specified path. The path should contain:
  • config.yml or config.yaml - Main configuration file
  • *.co - Colang files defining rails and flows
  • config.py - Optional initialization code
  • actions.py - Optional custom actions

Generation Methods

generate_async()

The primary async method for generating responses with guardrails applied.
Parameters:
str
default:"None"
The prompt to be used for completion. Cannot be used with messages.
List[dict]
default:"None"
The history of messages to generate the next message. Cannot be used with prompt.
GenerationOptions | dict
default:"None"
Options specific for the generation (e.g., output variables, logging).
State | dict
default:"None"
The state object that should be used as the starting point.
StreamingHandler
default:"None"
If specified, and the config supports streaming, the provided handler will be used for streaming.
Returns:
  • When using prompt: Returns a string with the completion
  • When using messages: Returns a dict with the assistant’s message
  • When using options: Returns a GenerationResponse object with additional metadata

Message Format

Messages follow the OpenAI Chat Completions API format:
Supported roles:
  • user - User messages
  • assistant - Assistant/bot messages
  • context - Context variables (must be a dict)
  • event - Custom events
  • system - System messages
  • tool - Tool/function call results

generate()

Synchronous wrapper around generate_async().
The synchronous method is provided for convenience but internally uses the async API. For best performance, use generate_async() in async contexts.

Streaming

stream_async()

Streams the response token-by-token with guardrails applied.
Parameters:
str
default:"None"
The prompt to be used for completion.
List[dict]
default:"None"
The history of messages.
GenerationOptions | dict
default:"None"
Generation options.
State | dict
default:"None"
The state object to use as starting point.
bool
default:"False"
If True, yields dicts with text and metadata keys. If False, yields strings.
Streaming with output rails requires enabling streaming support in your configuration:
config.yml

Complete Examples

Basic Usage

Using Async API

With Generation Options

Streaming Example

With Context Variables

With Custom LLM

Conversation History

Advanced Features

Updating the LLM

You can update the LLM used by the rails instance:

Registering Custom Actions

Register custom Python functions as actions:

Error Handling

Next Steps

Server API

Deploy guardrails as a REST API server

CLI Tools

Use command-line tools for testing and development

LangChain Integration

Integrate with LangChain chains and agents

Configuration Guide

Learn how to configure guardrails