Output Rails
This guide describes how to add output rails to a guardrails configuration. This guide builds on the previous guide, Input Rails, developing further the demo ABC Bot.
Prerequisites
- Install the
openaipackage:
- Set the
OPENAI_API_KEYenvironment variable:
- If you’re running this inside a notebook, patch the AsyncIO loop.
Output Moderation
NeMo Guardrails comes with a built-in output self-checking rail. This rail uses a separate LLM call to make sure that the bot’s response should be allowed.
Activating the self check output rail is similar to the self check input rail:
- Activate the
self check outputrail inconfig.yml. - Add a
self_check_outputprompt inprompts.yml.
Activate the Rail
To activate the rail, include the self check output flow name in the output rails section of the config.yml file:
For reference, update the full rails section in config.yml to look like the following:
The self check output flow is similar to the input one:
Add a Prompt
The self-check output rail needs a prompt to perform the check.
Using the Output Checking Rail
Load the configuration and see it in action. Try tricking the LLM to respond with the phrase “you are an idiot”.
Inspect what happened behind the scenes:
As we can see, the LLM did generate the message containing the word “idiot”, however, the output was blocked by the output rail.
The following figure depicts the process:

Streaming Output
By default, the output from the rail is synchronous. You can enable streaming to provide asynchronous responses and reduce the time to the first response.
-
Modify the
railsfield in theconfig.ymlfile and add thestreamingfield to enable streaming:
The enabled: True field is required to enable streaming output rails.
-
Call the
stream_asyncmethod and handle the chunked response:Partial Output
For reference information about the related config.yaml file fields,
refer to the Configuration Reference.
Custom Output Rail
Build a custom output rail with a list of proprietary words that we want to make sure do not appear in the output.
- Create a
config/actions.pyfile with the following content, which defines an action:
The check_blocked_terms action fetches the bot_message context variable, which contains the message that was generated by the LLM, and checks whether it contains any of the blocked terms.
- Add a flow that calls the action. Let’s create an
config/rails/blocked_terms.cofile:
- Add the
check blocked termsto the list of output flows:
- Test whether the output rail is working:
As expected, the bot refuses to respond with the right message.
- List the LLM calls:
As we can see, the generated message did contain the word “proprietary” and it was blocked by the check blocked terms output rail.
Let’s check that the message was not blocked by the self-check output rail:
Similarly, you can add any number of custom output rails.
Test
Test this configuration in an interactive mode using the NeMo Guardrails CLI Chat:
Next
The next guide, Topical Rails, adds a topical rails to the ABC bot, to make sure it only responds to questions related to the employment situation.