Exceptions and Error Handling
NeMo Guardrails supports raising exceptions from within flows.
An exception is an event whose name ends with Exception, e.g., InputRailException.
When an exception is raised, the final output is a message with the role set to exception and the content
set to additional information about the exception. For example:
Guardrails Library Exception
By default, all the guardrails included in the Guardrail Catalog return a predefined message
when a rail is triggered. You can change this behavior by setting the enable_rails_exceptions key to True in your
config.yml file:
When this setting is enabled, the rails are triggered, they will return an exception message.
To understand better what is happening under the hood, here’s how the self check input rail is implemented:
In Colang 2.x, you must change $config.enable_rails_exceptions to $system.config.enable_rails_exceptions and create event to send.
When the self check input rail is triggered, the following exception is returned.
Exception Types
The NeMo Guardrails library includes additional exception types for specific integrations:
LlamaGuardInputRailException/LlamaGuardOutputRailExceptionJailbreakDetectionRailExceptionContentSafetyCheckInputException/ContentSafetyCheckOutputExceptionFactCheckRailExceptionSelfCheckHallucinationRailExceptionInjectionDetectionRailException
Each library rail raises its own exception type when enable_rails_exceptions is enabled.
Custom Exception Handling
You can create custom exception types by following the naming convention of ending with Exception:
Exception Response Format
All exceptions follow a consistent JSON format:
Field Descriptions
- type: The exception type (e.g.,
InputRailException) - uid: A unique identifier for the exception instance
- event_created_at: Timestamp when the exception was created
- source_uid: Identifier for the source that created the exception
- message: Human-readable description of what went wrong
Handling Exceptions in Applications
When integrating NeMo Guardrails with your application, you should handle exceptions appropriately:
Best Practices
-
Use Descriptive Messages: Provide clear, actionable error messages in your exceptions.
-
Log Exceptions: Always log exceptions for debugging and monitoring purposes.
-
Graceful Degradation: Provide fallback responses when exceptions occur.
-
User-Friendly Messages: Translate technical exception messages into user-friendly responses.
-
Exception Categories: Use appropriate exception types to categorize different kinds of errors.
-
Configuration Control: Use the
enable_rails_exceptionssetting to control whether rails return exceptions or predefined messages.