Core Classes Reference
This guide covers the two fundamental classes in the NeMo Guardrails library: RailsConfig for loading configurations and LLMRails for generating responses with guardrails.
RailsConfig
The RailsConfig class represents a complete guardrails configuration, including models, rails, flows, prompts, and other settings. This class requires to load the configuration from a directory or a single file you created in the previous chapter About Configuring Guardrails.
Loading Configuration from a Directory
The most common way to load a configuration is from a directory containing config.yml and Colang files:
Expected directory structure:
Loading from a Single File
You can also load from a single YAML file:
Loading from Content
For dynamic configurations or testing, load directly from strings:
Loading from a Dictionary
You can also provide configuration as a Python dictionary:
Combining Configurations
Configurations can be combined using the + operator:
This is useful for:
- Adding rails to a base configuration.
- Layering environment-specific settings.
- Combining shared and application-specific configurations.
Key Configuration Properties
LLMRails
The LLMRails class is the main interface for generating responses with guardrails applied.
Initialization
Constructor parameters:
Using a Custom LLM
You can provide your own LLM instance:
When providing an LLM via the constructor, it takes precedence over any main LLM specified in the configuration.
Generating Responses
Using Messages (Chat Format)
Using a Prompt (Completion Format)
With Conversation History
Passing Context
You can pass additional context using the context role:
You can access context variables in Colang flows using $variable_name syntax:
Alternatively, you can access context variables through the context parameter. For example, you can set up an action function that uses a variable extracted from the context parameter as follows:
For detailed information about context variables, see Action Parameters: The Context Parameter and Colang 1.0 Language Syntax: Variables.
Asynchronous Generation
For async contexts, use generate_async:
Streaming Responses
For real-time token streaming:
For detailed streaming configuration, refer to Streaming.
Event-based Generation
For low-level control using events:
For detailed event-based API usage, refer to Event-Based API.
Generation Options
Fine-tune generation behavior using the options parameter:
For the message-based call shown above, passing options changes the return type.
Without options, generate() returns an OpenAI-style message dictionary that you index with response["content"].
With options, it returns a GenerationResponse object whose message list you read with response.response[0]["content"].
For a prompt-based LLMRails call, the optionless return and GenerationResponse.response are strings instead.
The IORails engine also returns a GenerationResponse when you pass options, but fields that depend on the Colang runtime remain unavailable.
output_vars and state raise ValueError.
The log.internal_events and log.colang_history options raise NotImplementedError.
For detailed options, refer to Generation Options.
Checking Messages Against Rails
The check_async() and check() methods validate messages against input and output rails without triggering full LLM generation:
By default, the methods automatically detect which rails to run based on message roles. You can override this with the rail_types parameter:
The LLMRails and IORails engines both implement these methods, and the Guardrails facade forwards the call to the engine it selected.
For detailed usage, method signatures, engine differences, and examples, refer to Check Messages.
Registering Custom Actions
You can register custom Python functions as actions:
For detailed action registration, refer to Custom Actions.
Registering Embedding Search Providers
For custom knowledge base search:
Complete Example
Related Resources
- Generation Options - Fine-grained control over generation
- Check Messages - Validate messages against rails
- Streaming - Real-time token streaming
- Event-Based API - Low-level event control
- Tools Integration - Integrating LangChain tools
- About Configuring Guardrails - Complete configuration reference