Skip to content

Guardrail Manager

Manage content guardrails using pluggable guardrail engines.

GuardrailManager(engine, empty_content_safe=None, error_conservative=None, raise_on_exceptions=None)

Bases: Component

Manage content guardrails using pluggable engines.

This component provides content filtering and safety checks and delegates the provider-specific logic to a GuardrailEngine.

Attributes:

Name Type Description
engines list[GuardrailEngine]

Required pluggable engines for guardrail operations. Provide a custom engine or a list of engines to handle guardrail logic. If a list is provided, engines are executed sequentially in the order they appear.

empty_content_safe bool | None

Whether empty content should be considered safe. Defaults to None.

error_conservative bool | None

Whether to mark content as unsafe on errors. Defaults to None.

raise_on_exceptions tuple[type[Exception], ...]

Tuple of exception types that should propagate to the caller instead of being handled as safe/unsafe. Defaults to ().

Initialize the GuardrailManager class.

Parameters:

Name Type Description Default
engine GuardrailEngine | list[GuardrailEngine]

Required pluggable engine for guardrail operations. Provide a custom engine or a list of engines to handle guardrail logic. If a list is provided, engines are executed sequentially in the order they appear.

required
empty_content_safe bool | None

Whether empty content should be considered safe. Defaults to True.

None
error_conservative bool | None

Whether to mark content as unsafe on errors. Defaults to True.

None
raise_on_exceptions tuple[type[Exception], ...] | None

Exception types to propagate to the caller when raised by an engine. Defaults to ().

None

Raises:

Type Description
TypeError

If raise_on_exceptions is not a tuple or contains non-Exception subclasses.

check_content(content, engine_kwargs=None) async

Check the content for safety using configured engines.

This method iterates through the configured engines sequentially and returns the first unsafe result found. This "fail-fast" approach ensures efficient processing while maintaining strict safety guarantees.

Engines with DISABLED mode are skipped entirely. For active engines, it checks input content if the engine's guardrail_mode includes input checking, then checks output content if the input passed and the engine's mode includes output checking.

Parameters:

Name Type Description Default
content str | GuardrailInput

The primary content to check. If str, treated as input-only content.

required
engine_kwargs dict[str, Any] | None

Arguments to pass to each engine. Defaults to None.

None

Returns:

Name Type Description
GuardrailResult GuardrailResult

The result of the content safety check. Returns safe result only if all engines pass all applicable checks.

Raises:

Type Description
BaseInvokerError

If an LM invoker used by an engine fails.

ValueError

If content is neither str nor GuardrailInput.

Exception

The original engine exception, if its type matches any entry in raise_on_exceptions. The exception propagates unmodified after logger.error fires.