Safety
Safety configuration module.
This module contains the safety configuration builders.
BaseSafetyConfigBuilder
Bases: ABC
Base abstract class for all safety configuration builders.
Tip
To minimize runtime latency, ensure that all computationally expensive operations
(e.g., string formatting, data processing, configuration loading) are performed
during the __init__ phase. The get_string and to_dict methods should ideally
return pre-computed values.
get_string()
abstractmethod
Returns the formatted prompt string for LLM context.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The formatted prompt string. |
to_dict()
abstractmethod
Returns a structured dictionary representation of the configuration.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dict[str, Any]: The structured dictionary. |
with_defaults(**kwargs)
abstractmethod
classmethod
Factory method to create a config instance with default values and optional overrides.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Optional overrides for the configuration attributes. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
T |
T
|
An instance of the 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 |
None
|
safety_items
|
list[ContentSafetyItem] | None
|
List of custom content safety items.
If None, loads defaults from |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
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 |
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. |
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 |
None
|
denylist_topic
|
list[str] | None
|
List of topics to be denied.
The list should be based on the topics in |
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 |
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 |
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. |
TopicSafetyMode
Bases: StrEnum
Topic safety mode enumeration for guardrail configuration.