Skip to content

Content

Content safety configuration.

This module contains the builder for the content safety configuration.

ContentSafetyConfigBuilder(enabled_categories=None, safety_items=None)

Bases: BaseSafetyConfigBuilder

Content safety configuration builder for guardrail prompts.

Initialize the content safety configuration.

If safety_items is None, it will default to the standard items from the default YAML file. If an empty list is provided, it will raise a ValueError. The safety strings are pre-formatted during initialization for better performance.

Parameters:

Name Type Description Default
enabled_categories list[str] | None

List of categories to be enabled. If None, all categories from safety_items are used. Defaults to None.

None
safety_items list[ContentSafetyItem] | None

List of custom content safety items. If None, loads defaults from DEFAULT_CONTENT_SAFETY_FILE. Defaults to None.

None

Raises:

Type Description
ValueError

If safety_items is an empty list.

get_string()

Get the string representation of the content safety configuration.

Returns:

Name Type Description
str str

Generated content safety configuration string.

to_dict()

Returns a dictionary containing only the final filtered safety items.

Returns:

Type Description
dict[str, Any]

dict[str, Any]: dictionary with safety_items.

with_defaults(**kwargs) classmethod

Factory method to create a config instance with default values and optional overrides.

If safety_items is provided in kwargs, it will be added to the default items.

Parameters:

Name Type Description Default
**kwargs Any

Optional overrides for the configuration attributes.

{}

Returns:

Name Type Description
ContentSafetyConfigBuilder ContentSafetyConfigBuilder

An instance of the configuration.

ContentSafetyItem

Bases: BaseModel

Content safety item.

Attributes:

Name Type Description
category str

The specific category to be checked.

description str | None

The description of the specific category. Defaults to None.

examples list[str] | None

The examples of the specific category. Defaults to None.