Core Classes
LLMRails
TheLLMRails class is the main entry point for using guardrails programmatically.
Initialization
RailsConfig
required
A rails configuration loaded from a directory or created programmatically.
BaseLLM | BaseChatModel
default:"None"
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.
RailsConfig
TheRailsConfig class represents a guardrails configuration.
Loading Configuration
classmethod
Loads a
RailsConfig from the specified path. The path should contain:config.ymlorconfig.yaml- Main configuration file*.co- Colang files defining rails and flowsconfig.py- Optional initialization codeactions.py- Optional custom actions
Generation Methods
generate_async()
The primary async method for generating responses with guardrails applied.str
default:"None"
The prompt to be used for completion. Cannot be used with
messages.List[dict]
default:"None"
The history of messages to generate the next message. Cannot be used with
prompt.GenerationOptions | dict
default:"None"
Options specific for the generation (e.g., output variables, logging).
State | dict
default:"None"
The state object that should be used as the starting point.
StreamingHandler
default:"None"
If specified, and the config supports streaming, the provided handler will be used for streaming.
- When using
prompt: Returns a string with the completion - When using
messages: Returns a dict with the assistant’s message - When using
options: Returns aGenerationResponseobject with additional metadata
Message Format
Messages follow the OpenAI Chat Completions API format:user- User messagesassistant- Assistant/bot messagescontext- Context variables (must be a dict)event- Custom eventssystem- System messagestool- Tool/function call results
generate()
Synchronous wrapper aroundgenerate_async().
The synchronous method is provided for convenience but internally uses the async API. For best performance, use
generate_async() in async contexts.Streaming
stream_async()
Streams the response token-by-token with guardrails applied.str
default:"None"
The prompt to be used for completion.
List[dict]
default:"None"
The history of messages.
GenerationOptions | dict
default:"None"
Generation options.
State | dict
default:"None"
The state object to use as starting point.
bool
default:"False"
If True, yields dicts with
text and metadata keys. If False, yields strings.Complete Examples
Basic Usage
Using Async API
With Generation Options
Streaming Example
With Context Variables
With Custom LLM
Conversation History
Advanced Features
Updating the LLM
You can update the LLM used by the rails instance:Registering Custom Actions
Register custom Python functions as actions:Error Handling
Next Steps
Server API
Deploy guardrails as a REST API server
CLI Tools
Use command-line tools for testing and development
LangChain Integration
Integrate with LangChain chains and agents
Configuration Guide
Learn how to configure guardrails