Implementation:Guardrails ai Guardrails RailTypes
| Knowledge Sources | |
|---|---|
| Domains | Types, RAIL |
| Last Updated | 2026-02-14 00:00 GMT |
Overview
Defines the RailTypes enumeration representing all built-in data types supported by the RAIL XML specification.
Description
The RailTypes class is a string enumeration (str, Enum) that maps human-readable type names to their RAIL XML tag values. It supports thirteen data types that cover primitive values (strings, integers, floats, booleans), temporal values (dates, times, datetimes), structural values (lists, objects), and special types (percentages, enums, discriminated unions via choice/case).
Because RailTypes inherits from both str and Enum, each member can be used directly as a string wherever a RAIL type name is expected. The class also provides a get classmethod for safe lookup that returns None instead of raising an exception when a key is not found.
Usage
Use RailTypes when parsing or constructing RAIL XML documents to validate that a given type string corresponds to a recognized built-in type. The get classmethod is particularly useful for safely checking if a tag name is a known RAIL type without exception handling.
Code Reference
Source Location
- Repository: Guardrails
- File:
guardrails/types/rail.py
Signature
class RailTypes(str, Enum):
STRING = "string"
INTEGER = "integer"
FLOAT = "float"
BOOL = "bool"
DATE = "date"
TIME = "time"
DATETIME = "date-time"
PERCENTAGE = "percentage"
ENUM = "enum"
LIST = "list"
OBJECT = "object"
CHOICE = "choice"
CASE = "case"
@classmethod
def get(cls, key: str) -> Optional["RailTypes"]: ...
Import
from guardrails.types.rail import RailTypes
I/O Contract
Enum Members
| Member | Value | Description |
|---|---|---|
STRING |
"string" |
A string value |
INTEGER |
"integer" |
An integer value |
FLOAT |
"float" |
A float value |
BOOL |
"bool" |
A boolean value |
DATE |
"date" |
A date value |
TIME |
"time" |
A time value |
DATETIME |
"date-time" |
A datetime value |
PERCENTAGE |
"percentage" |
A percentage value represented as a string (e.g. "20.5%") |
ENUM |
"enum" |
An enum value |
LIST |
"list" |
A list/array value |
OBJECT |
"object" |
An object/dictionary value |
CHOICE |
"choice" |
The options for a discriminated union |
CASE |
"case" |
A dictionary that contains a discriminated union |
get Classmethod
| Parameter | Type | Description |
|---|---|---|
key |
str |
The RAIL type string to look up |
Returns: Optional[RailTypes] -- The matching enum member, or None if no match is found.
Usage Examples
from guardrails.types.rail import RailTypes
# Direct access
rail_type = RailTypes.STRING
print(rail_type) # "string"
print(rail_type.value) # "string"
# Safe lookup
result = RailTypes.get("integer")
print(result) # RailTypes.INTEGER
# Safe lookup with unknown type
result = RailTypes.get("unknown_type")
print(result) # None
# Use as string
if RailTypes.get("date-time") is not None:
print("date-time is a valid RAIL type")
Related Pages
- Guardrails_ai_Guardrails_RAIL_Schema -- Uses RailTypes for parsing RAIL XML documents
- Guardrails_ai_Guardrails_Types -- Parent types module