Skip to main content

LLMRails

The LLMRails class is the primary interface for creating and managing guardrails in NeMo Guardrails. It provides methods for generating responses with guardrails applied.

Constructor

RailsConfig
required
A RailsConfig object containing the guardrails configuration.
Optional[Union[BaseLLM, BaseChatModel]]
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.

Methods

generate_async

Generate a completion or next message asynchronously.
Optional[str]
The prompt to be used for completion.
Optional[List[dict]]
The history of messages to be used to generate the next message. Messages have the format:
Optional[Union[dict, GenerationOptions]]
Options specific for the generation.
Optional[Union[dict, State]]
The state object that should be used as the starting point.
Optional[StreamingHandler]
If specified, and the config supports streaming, the provided handler will be used for streaming.
Union[str, dict, GenerationResponse, Tuple[dict, dict]]
The completion (when a prompt is provided) or the next message.

generate

Synchronous version of generate_async.

stream_async

Stream the response tokens asynchronously.
bool
default:"False"
Whether to include metadata in the streamed chunks.
Optional[AsyncIterator[str]]
If provided, uses this external generator for streaming.
AsyncIterator[Union[str, dict]]
An async iterator that yields token chunks as strings, or dicts if include_metadata=True.

check_async

Run rails on messages to check for policy violations.
List[dict]
required
List of message dicts with ‘role’ and ‘content’ fields.
Optional[List[RailType]]
Optional list of rail types to run (e.g., [RailType.INPUT] or [RailType.OUTPUT]). When not provided, automatically determines which rails to run based on message roles.
RailsResult
Contains:
  • status: PASSED, MODIFIED, or BLOCKED
  • content: The final content after rails processing
  • rail: Name of the rail that blocked (if blocked)

check

Synchronous version of check_async.

register_action

Register a custom action for the rails configuration.
Callable
required
The action function to register.
Optional[str]
The name to use for the action. If not provided, uses the function name.

register_filter

Register a custom filter for the rails configuration.

register_embedding_provider

Register a custom embedding provider.
Type[EmbeddingModel]
required
The embedding model class.
Optional[str]
The name of the embedding engine.

explain

Returns detailed information about the latest generation.
ExplainInfo
An object containing detailed explanation information including LLM calls, activated rails, and Colang history.

Attributes

RailsConfig
The rails configuration object.
Optional[Union[BaseLLM, BaseChatModel]]
The main LLM engine being used.
Runtime
The Colang runtime instance.
bool
Whether verbose logging is enabled.