Exception Handling Guidelines#
In our PyRIT framework, proper exception handling is crucial for maintaining robustness and reliability. We have centralized exceptions in a dedicated module to streamline this process. When working with orchestrators, targets, converters, and scorers, and handle exceptions please adhere to the following guidelines:
General Guidelines#
Centralized Exceptions: Use the exceptions defined in our centralized exceptions module or create new ones as necessary in the same module.
Inherit
PyritException
: Ensure any new exceptions inherit fromPyritException
to maintain consistency.Exception Processing: Utilize the
process_exception
method to handle exceptions appropriately.Add Response Entries to Memory: After handling an exception using the
process_exception
method. While adding response entries to memory, ensure to set theresponse_type
anderror
parameter toerror
to help identify the responses in the database for easy filtering. Theoriginal_value
andconverted_value
or thePromptRequestPiece
object should contain the error message.
Specific Scenarios#
Response is None, JSON Parsing Failure in Scorer, RateLimitError
Action: In the endpoint code (scoring, target, or converter) there should be a retry mechanism to attempt the operation a few times. For example, use the
@pyrit_target_retry
decoratorIf Still Failing:
The endpoint raises the exception (it should be unhandled)
The top-level orchestrators can handle the exception as needed
BadRequestError Caused by Content Filter
Action: Do not retry the operation.
If Occurring: At the endpoint code (target) use the
process_exception
method to handle the failure and log the error. Do not re-raise.
Unknown Exception
Action: Raise the exception itself to allow for proper propagation. Do not retry.
Future Learning: Monitor these exceptions to learn and identify patterns for future enhancements and more specific exception handling.
By following these guidelines, we ensure a consistent and robust approach to exception handling across the framework.