pyrit.models.HarmDefinition#

class HarmDefinition(version: str, category: str, scale_descriptions: ~typing.List[~pyrit.models.harm_definition.ScaleDescription] = <factory>, *, source_path: str | None = None)[source]#

Bases: object

A harm definition loaded from a YAML file.

This class represents the structured content of a harm definition YAML file, which includes the version, category name, and scale descriptions that define how to score content for this harm category.

Parameters:
  • version – The version of the harm definition (e.g., “1.0”).

  • category – The harm category name (e.g., “violence”, “hate_speech”).

  • scale_descriptions – List of scale descriptions defining score levels.

  • source_path – The path to the YAML file this was loaded from.

__init__(version: str, category: str, scale_descriptions: ~typing.List[~pyrit.models.harm_definition.ScaleDescription] = <factory>, *, source_path: str | None = None) None#

Methods

__init__(version, category[, ...])

from_yaml(harm_definition_path)

Load and validate a harm definition from a YAML file.

get_scale_description(score_value)

Get the description for a specific score value.

validate_category(category, *[, check_exists])

Validate a harm category name.

Attributes

category: str#
classmethod from_yaml(harm_definition_path: str | Path) HarmDefinition[source]#

Load and validate a harm definition from a YAML file.

The function first checks if the path is a simple filename (e.g., “violence.yaml”) and if so, looks for it in the standard HARM_DEFINITION_PATH directory. Otherwise, it treats the path as a full or relative path.

Parameters:

harm_definition_path – Path to the harm definition YAML file. Can be a simple filename like “violence.yaml” which will be resolved relative to the standard harm_definition directory, or a full path.

Returns:

The loaded harm definition.

Return type:

HarmDefinition

Raises:
  • FileNotFoundError – If the harm definition file does not exist.

  • ValueError – If the YAML file is invalid or missing required fields.

get_scale_description(score_value: str) str | None[source]#

Get the description for a specific score value.

Parameters:

score_value – The score value to look up (e.g., “1”, “2”).

Returns:

The description for the score value, or None if not found.

scale_descriptions: List[ScaleDescription]#
source_path: str | None = None#
static validate_category(category: str, *, check_exists: bool = False) bool[source]#

Validate a harm category name.

Validates that the category name follows the naming convention (lowercase letters and underscores only) and optionally checks if it exists in the standard harm definitions.

Parameters:
  • category – The category name to validate.

  • check_exists – If True, also verify the category exists in get_all_harm_definitions(). Defaults to False.

Returns:

True if the category is valid (and exists if check_exists is True), False otherwise.

version: str#