nemoguardrails.guardrails.guardrails
Top-level Guardrails interface module.
This module provides a simplified, user-friendly interface for interacting with NeMo Guardrails. The Guardrails class wraps either IORails or LLMRails (chosen automatically based on config) and provides a streamlined API for generating LLM responses with programmable guardrails.
Module Contents
Classes
Data
API
Bases: BaseGuardrails
Top-level interface for NeMo Guardrails functionality.
Per-session events history cache. Only supported for LLMRails.
Used by the server to persist conversation state across requests. Stored by reference; assigning replaces the dict object, not its contents.
Deprecated. Use explain() instead.
Direct access can return None for an uninitialized accumulator;
explain() guarantees a non-None ExplainInfo. Only supported for LLMRails.
The main LLM in use. Only supported for LLMRails.
Read-only; use update_llm() to replace it.
The optional passthrough function that bypasses LLM generation.
Only supported for LLMRails. When set, the rails pipeline calls this function instead of the main LLM for generating responses.
Get immutable LLMRails object
The Colang runtime backing the rails engine. Only supported for LLMRails.
Async context manager entry.
Async context manager exit.
Pickle support: preserve config, verbose, and use_iorails so the rebuilt instance lands on the same engine. The llm is dropped (matches LLMRails).
Pickle support: rebuild from config + verbose + use_iorails. Older pickles missing these keys default to False/True respectively for backwards compatibility.
Return messages in standard format, converting a prompt string if needed.
If messages is provided, returns it as-is. If prompt is provided, wraps it as [{“role”: “user”, “content”: prompt}].
Lazy initialization: call startup() on first use if not already started.
Synchronous version of check_async. Only supported for LLMRails.
Run rails on messages based on their content (asynchronous). Only supported for LLMRails.
Get the latest ExplainInfo object for debugging. Only supported for LLMRails
Generate an LLM response synchronously with guardrails applied. Supported in both IORails and LLMRails
Generate an LLM response asynchronously with guardrails applied. Supported by both LLMRails and IORails
Synchronous version of generate_events_async. Only supported for LLMRails.
Generate the next events based on the provided history. Only supported for LLMRails.
Synchronous version of process_events_async. Only supported for LLMRails.
Process a sequence of events in a given state. Only supported for LLMRails.
Register a custom action for the rails configuration. Only supported for LLMRails. Returns self so calls can be chained.
Register a custom action parameter. Only supported for LLMRails. Returns self so calls can be chained.
Register a custom embedding provider. Only supported for LLMRails. Returns self so calls can be chained.
Register a new embedding search provider. Only supported for LLMRails. Returns self so calls can be chained.
Register a custom filter for the rails configuration. Only supported for LLMRails. Returns self so calls can be chained.
Register a custom output parser for the rails configuration. Only supported for LLMRails. Returns self so calls can be chained.
Register a value to be included in the prompt context. Only supported for LLMRails. Returns self so calls can be chained.
Lifecycle method to stop the rails engine.
Idempotent: safe to call multiple times.
Lifecycle method to start the rails engine.
Idempotent: safe to call multiple times. Also called automatically
on first generate_async() if not called explicitly, so callers
are not required to manage the lifecycle.
The non-streaming admission queue is owned by IORails and is
started/stopped as part of IORails.start() / stop().
Generate an LLM response asynchronously with streaming support.
Replace the main LLM with a new one. Only supported for LLMRails, since IORails doesn’t take LLM as argument