Skip to content

Topic

Topic safety configuration.

This module contains the builder for the topic safety configuration.

TopicSafetyConfigBuilder(topic_safety_mode=None, allowlist_topic=None, denylist_topic=None, allowed_topic_items=None, denied_topic_items=None)

Bases: BaseSafetyConfigBuilder

Topic safety configuration builder for guardrail prompts.

Initialize the topic safety configuration.

If allowed_topic_items or denied_topic_items are None, they 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
topic_safety_mode TopicSafetyMode | None

The mode for topic safety. Options: "allowlist", "denylist", "hybrid". Defaults to TopicSafetyMode.HYBRID.

None
allowlist_topic list[str] | None

List of topics to be allowed. The list should be based on the topics in allowed_topic_items. If None, all topics from allowed_topic_items are used. Defaults to None.

None
denylist_topic list[str] | None

List of topics to be denied. The list should be based on the topics in denied_topic_items. If None, all topics from denied_topic_items are used. Defaults to None.

None
allowed_topic_items list[TopicSafetyItem] | None

List of custom allowed topic items. Defaults to None.

None
denied_topic_items list[TopicSafetyItem] | None

List of custom denied topic items. Defaults to None.

None

Raises:

Type Description
ValueError

If topic_safety_mode is invalid, or if allowed_topic_items or denied_topic_items is an empty list.

get_string()

Get the string representation of the topic safety configuration.

Returns:

Name Type Description
str str

Generated topic 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 allowed_topic_items and denied_topic_items.

with_defaults(**kwargs) classmethod

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

If allowed_topic_items or denied_topic_items are provided in kwargs, they will be added to the default items.

Parameters:

Name Type Description Default
**kwargs Any

Optional overrides for the configuration attributes.

{}

Returns:

Name Type Description
TopicSafetyConfigBuilder TopicSafetyConfigBuilder

An instance of the configuration.

TopicSafetyItem

Bases: BaseModel

Topic safety item.

Attributes:

Name Type Description
topic str

The specific topic to be checked.

description str | None

The description of the specific topic. Defaults to None.

examples list[str] | None

The examples of the specific topic. Defaults to None.