Skip to main content

RailsConfig

The RailsConfig class represents the configuration for a guardrails application. It defines models, rails, prompts, and other settings.

Loading Configuration

from_path

Load a configuration from a directory or file path.
@classmethod
def from_path(
    config_path: str,
    test_set_percentage: float = 0.0
) -> RailsConfig
config_path
str
required
Path to a directory containing configuration files (config.yml, .co files) or a single .co file.
test_set_percentage
float
default:"0.0"
The percentage of examples to use for testing.
config
RailsConfig
A RailsConfig instance loaded from the specified path.
from nemoguardrails import RailsConfig

config = RailsConfig.from_path("path/to/config")

from_content

Create a configuration from YAML and Colang content strings.
@classmethod
def from_content(
    colang_content: Optional[str] = None,
    yaml_content: Optional[str] = None
) -> RailsConfig
colang_content
Optional[str]
Colang (.co) file content as a string.
yaml_content
Optional[str]
YAML configuration content as a string.
from nemoguardrails import RailsConfig

yaml_content = """
models:
  - type: main
    engine: openai
    model: gpt-4o
"""

colang_content = """
define user express greeting
  "hello"
  "hi"

define bot express greeting
  "Hello! How can I help you?"
"""

config = RailsConfig.from_content(
    colang_content=colang_content,
    yaml_content=yaml_content
)

Configuration Fields

models
List[Model]
default:"[]"
List of LLM model configurations. Each model has:
  • type: “main”, “embeddings”, “content_safety”, etc.
  • engine: Provider name (“openai”, “nvidia_ai_endpoints”, etc.)
  • model: Model name (“gpt-4o”, “llama-3.1-8b”, etc.)
  • parameters: Additional parameters for the model
  • cache: Cache configuration for the model
rails
Rails
Configuration for different types of rails:
  • input: Input rails configuration
  • output: Output rails configuration
  • retrieval: Retrieval rails configuration
  • dialog: Dialog rails configuration
  • tool_input: Tool input rails configuration
  • tool_output: Tool output rails configuration
  • config: Additional rail-specific configuration (fact checking, jailbreak detection, etc.)
prompts
List[TaskPrompt]
default:"[]"
List of custom prompts for specific tasks. Each prompt has:
  • task: The task ID
  • content: The prompt content (for text completion)
  • messages: List of messages (for chat completion)
  • models: Optional list of models this prompt applies to
user_messages
Dict[str, List[str]]
default:"{}"
Mapping of canonical user message forms to example utterances.
bot_messages
Dict[str, List[str]]
default:"{}"
Mapping of canonical bot message forms to example responses.
flows
List[dict]
default:"[]"
List of Colang flow definitions.
instructions
List[Instruction]
default:"[]"
Natural language instructions for the LLM.
docs
List[Document]
default:"[]"
Documents for knowledge base/RAG.
colang_version
str
default:"1.0"
The Colang version to use (“1.0” or “2.x”).
passthrough
bool
default:"False"
Whether to enable passthrough mode (direct LLM access without rails).
streaming
bool
default:"False"
Whether streaming is enabled by default.
custom_data
dict
default:"{}"
Custom configuration data for user-defined extensions.
knowledge_base
KnowledgeBaseConfig
Configuration for the knowledge base.
tracing
TracingConfig
Configuration for tracing/telemetry.

Model Configuration

models:
  - type: main
    engine: openai
    model: gpt-4o
    parameters:
      temperature: 0.7
      max_tokens: 1000
  
  - type: embeddings
    engine: openai
    model: text-embedding-3-small
  
  - type: content_safety
    engine: nvidia_ai_endpoints
    model: nemoguard-guardrailsmoderation-8b
    cache:
      enabled: true
      maxsize: 10000

Rails Configuration

rails:
  input:
    flows:
      - check jailbreak
      - check sensitive data
  
  output:
    flows:
      - check hallucination
    streaming:
      enabled: true
      chunk_size: 200
  
  config:
    jailbreak_detection:
      nim_base_url: "http://localhost:8000/v1"
    
    sensitive_data_detection:
      input:
        entities:
          - EMAIL_ADDRESS
          - PHONE_NUMBER
          - SSN

Prompts Configuration

prompts:
  - task: self_check_input
    content: |
      Your task is to check if the user message below complies with the policy.
      
      User message: {{ user_message }}
      
      Policy: {{ policy }}
      
      Answer with 'yes' if compliant or 'no' if not.
  
  - task: generate_user_intent
    messages:
      - role: system
        content: You are a helpful assistant that identifies user intent.
      - role: user
        content: "{{ user_message }}"

Methods

model_dump

Export the configuration as a dictionary.
def model_dump() -> dict

model_dump_json

Export the configuration as a JSON string.
def model_dump_json() -> str

Combining Configurations

You can combine multiple configurations using the + operator:
base_config = RailsConfig.from_path("base")
additional_config = RailsConfig.from_path("additional")

combined_config = base_config + additional_config

Build docs developers (and LLMs) love