Checking Messages Against Rails
The check_async() and check() methods validate messages against input and output rails without triggering full LLM generation. Use these methods instead of generation options when you only need to run rails without generating a response. If you need to run generation while selectively disabling rail categories, see Generation Options: Disabling Rails.
Method Signatures
Both methods accept the same parameters and return a RailsResult object.
check_async()
The primary asynchronous method for checking messages against rails.
check()
Synchronous wrapper around check_async().
Parameters:
Returns: RailsResult object containing validation results.
Rail Type Selection
The methods determine which rails to execute based on the message roles or an explicit rail_types parameter.
Automatic Detection (Default)
When rail_types is not provided, the methods automatically determine which rails to run based on the message roles:
The methods ignore other message roles such as system, context, tool when determining which rails to run but still include them in the validation context.
Explicit Rail Types
You can override automatic detection by passing a list of RailType values:
RailsResult
The RailsResult object contains the outcome of the rails check.
RailStatus Enum
The RailStatus enum represents the three possible outcomes of a rails check.
Usage Examples
The following examples demonstrate common patterns for validating messages with check_async().
Validating User Input
Check a single user message against input rails and handle each possible status.
Validating a Full Conversation
Pass both user and assistant messages to run input and output rails together.
Including Context
Pass context variables alongside user or assistant messages to provide additional information for rail evaluation.
Context messages use the context role with a dictionary value for content.
For more information about context variables, refer to Core Classes: Passing Context.
Related Resources
- Core Classes -
LLMRailsandRailsConfigclass reference - Generation Options - Fine-grained control over generation with the
optionsparameter