Skip to content

Exceptions: Python

Exception classes used throughout Guardrails for SDK and model errors.

GuardrailException

Bases: Exception

Base class for exceptions thrown by :mod:guardrails.

Source code in src/guardrails/exceptions.py
6
7
class GuardrailException(Exception):
    """Base class for exceptions thrown by :mod:`guardrails`."""

UserError

Bases: GuardrailException

Error raised when the user misuses the SDK.

Source code in src/guardrails/exceptions.py
10
11
12
13
14
15
16
17
18
class UserError(GuardrailException):
    """Error raised when the user misuses the SDK."""

    message: str

    def __init__(self, message: str):
        """Initialize the exception with a human readable message."""
        super().__init__(message)
        self.message = message

__init__

__init__(message: str)

Initialize the exception with a human readable message.

Source code in src/guardrails/exceptions.py
15
16
17
18
def __init__(self, message: str):
    """Initialize the exception with a human readable message."""
    super().__init__(message)
    self.message = message

ModelBehaviorError

Bases: GuardrailException

Error raised when the model returns malformed or invalid data.

Source code in src/guardrails/exceptions.py
21
22
23
24
25
26
27
28
29
class ModelBehaviorError(GuardrailException):
    """Error raised when the model returns malformed or invalid data."""

    message: str

    def __init__(self, message: str):
        """Initialize with information on the misbehaviour."""
        super().__init__(message)
        self.message = message

__init__

__init__(message: str)

Initialize with information on the misbehaviour.

Source code in src/guardrails/exceptions.py
26
27
28
29
def __init__(self, message: str):
    """Initialize with information on the misbehaviour."""
    super().__init__(message)
    self.message = message

GuardrailTripwireTriggered

Bases: GuardrailException

Raised when a guardrail triggers a configured tripwire.

Source code in src/guardrails/exceptions.py
32
33
34
35
36
37
38
39
40
41
42
43
class GuardrailTripwireTriggered(GuardrailException):
    """Raised when a guardrail triggers a configured tripwire."""

    guardrail_result: "GuardrailResult"
    """The result data from the triggering guardrail."""

    def __init__(self, guardrail_result: "GuardrailResult"):
        """Initialize storing the result which caused the tripwire."""
        self.guardrail_result = guardrail_result
        super().__init__(
            f"Guardrail {guardrail_result.__class__.__name__} triggered tripwire",
        )

guardrail_result instance-attribute

guardrail_result: GuardrailResult = guardrail_result

The result data from the triggering guardrail.

__init__

__init__(guardrail_result: GuardrailResult)

Initialize storing the result which caused the tripwire.

Source code in src/guardrails/exceptions.py
38
39
40
41
42
43
def __init__(self, guardrail_result: "GuardrailResult"):
    """Initialize storing the result which caused the tripwire."""
    self.guardrail_result = guardrail_result
    super().__init__(
        f"Guardrail {guardrail_result.__class__.__name__} triggered tripwire",
    )

ConfigError

Bases: GuardrailException

Configuration bundle could not be loaded or validated.

Source code in src/guardrails/exceptions.py
46
47
48
49
50
51
52
class ConfigError(GuardrailException):
    """Configuration bundle could not be loaded or validated."""

    def __init__(self, message: str):
        """Initialize with a short description of the failure."""
        super().__init__(message)
        self.message = message

__init__

__init__(message: str)

Initialize with a short description of the failure.

Source code in src/guardrails/exceptions.py
49
50
51
52
def __init__(self, message: str):
    """Initialize with a short description of the failure."""
    super().__init__(message)
    self.message = message

ContextValidationError

Bases: GuardrailException

Raised when CLI context fails to match guardrail specification.

Source code in src/guardrails/exceptions.py
55
56
57
58
59
60
61
class ContextValidationError(GuardrailException):
    """Raised when CLI context fails to match guardrail specification."""

    def __init__(self, message: str):
        """Initialize with details about the schema mismatch."""
        super().__init__(message)
        self.message = message

__init__

__init__(message: str)

Initialize with details about the schema mismatch.

Source code in src/guardrails/exceptions.py
58
59
60
61
def __init__(self, message: str):
    """Initialize with details about the schema mismatch."""
    super().__init__(message)
    self.message = message