Skip to main content
NeMo Guardrails integrates seamlessly with LangChain through the RunnableRails class, which implements the LangChain Runnable protocol. This allows you to add guardrails to any LangChain component.

Installation

Install with LangChain support:

RunnableRails

The RunnableRails class wraps a guardrails configuration and provides LangChain-compatible interfaces.

Basic Usage

Constructor Parameters

RailsConfig
required
The rails configuration to use.
BaseLanguageModel
default:"None"
Optional LLM to use with the rails. If not provided, uses the LLM from the config.
List[Tool]
default:"None"
Optional list of LangChain tools to register with the rails.
bool
default:"True"
Whether to pass through the original prompt or let rails modify it.
Runnable
default:"None"
Optional runnable to wrap with the rails.
str
default:"input"
The key to use for input when dealing with dict input.
str
default:"output"
The key to use for output when dealing with dict output.
bool
default:"False"
Whether to print verbose logs.

Integration Patterns

Wrapping an LLM

Add guardrails around a language model:

Wrapping a Chain

Add guardrails to an entire chain:

With LangChain Tools

Register LangChain tools with guardrails:

Chaining Multiple Guardrails

Create pipelines with multiple guardrail layers:

Async Support

RunnableRails fully supports async operations:

Streaming

Stream responses token-by-token:
Streaming with output rails requires enabling streaming support:
config.yml

Batch Processing

Process multiple inputs efficiently:

Input/Output Formats

RunnableRails supports various input and output formats:

String Input/Output

Dict Input/Output

Message Objects

Advanced Examples

RAG Chain with Guardrails

Agent with Guardrails

Context Variables

Pass context variables through the chain:

Error Handling

Best Practices

1

Use async for production

Async methods provide better performance and scalability:
2

Enable streaming when needed

For better user experience with long responses:
3

Configure concurrency for batch

Control resource usage in batch operations:
4

Use appropriate input/output keys

Match your chain’s expected format:

Troubleshooting

Input Format Errors

If you get input format errors, verify the expected format:

Streaming Not Working

Enable streaming in both the LLM and config:
config.yml

Tool Registration Issues

Ensure tools are registered before use:

Next Steps

Python API

Core Python API reference

Server Guide

Deploy as a REST API server

Configuration

Configure your guardrails

Examples

More integration examples