Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

pyrit.identifiers

Identifiers module for PyRIT components.

Functions

build_atomic_attack_identifier

build_atomic_attack_identifier(attack_identifier: ComponentIdentifier, seed_group: Optional[SeedGroup] = None) → ComponentIdentifier

Build a composite ComponentIdentifier for an atomic attack.

Combines the attack strategy’s identity with identifiers for all seeds from the seed group. Every seed in the group is included in the identity; each seed’s is_general_technique flag is captured as a param so that downstream consumers (e.g., evaluation identity) can filter as needed.

When no seed_group is provided, the resulting identifier has an empty seeds children list, but still has the standard AtomicAttack shape for consistent querying.

ParameterTypeDescription
attack_identifierComponentIdentifierThe attack strategy’s identifier (from attack.get_identifier()).
seed_groupOptional[SeedGroup]The seed group to extract seeds from. If None, the identifier has an empty seeds list. Defaults to None.

Returns:

build_seed_identifier

build_seed_identifier(seed: Seed) → ComponentIdentifier

Build a ComponentIdentifier from a seed’s behavioral properties.

Captures the seed’s content hash, dataset name, and class type so that different seeds produce different identifiers while the same seed content always produces the same identifier.

ParameterTypeDescription
seedSeedThe seed to build an identifier for.

Returns:

class_name_to_snake_case

class_name_to_snake_case(class_name: str, suffix: str = '') → str

Convert a PascalCase class name to snake_case, optionally stripping a suffix.

ParameterTypeDescription
class_namestrThe class name to convert (e.g., “SelfAskRefusalScorer”).
suffixstrOptional explicit suffix to strip before conversion (e.g., “Scorer”). Defaults to ''.

Returns:

compute_eval_hash

compute_eval_hash(identifier: ComponentIdentifier, child_eval_rules: dict[str, ChildEvalRule]) → str

Compute a behavioral equivalence hash for evaluation grouping.

Unlike ComponentIdentifier.hash (which includes all params of self and children), the eval hash applies per-child rules to strip operational params (like endpoint, max_requests_per_minute), exclude children entirely, or filter list items. This ensures the same logical configuration on different deployments produces the same eval hash.

Children not listed in child_eval_rules receive full recursive treatment.

When child_eval_rules is empty, no filtering occurs and the result equals identifier.hash.

ParameterTypeDescription
identifierComponentIdentifierThe component identity to compute the hash for.
child_eval_rulesdict[str, ChildEvalRule]Per-child eval rules.

Returns:

config_hash

config_hash(config_dict: dict[str, Any]) → str

Compute a deterministic SHA256 hash from a config dictionary.

This is the single source of truth for identity hashing across the entire system. The dict is serialized with sorted keys and compact separators to ensure determinism.

ParameterTypeDescription
config_dictDict[str, Any]A JSON-serializable dictionary.

Returns:

Raises:

snake_case_to_class_name

snake_case_to_class_name(snake_case_name: str, suffix: str = '') → str

Convert a snake_case name to a PascalCase class name.

ParameterTypeDescription
snake_case_namestrThe snake_case name to convert (e.g., “my_custom”).
suffixstrOptional suffix to append to the class name (e.g., “Scenario” would convert “my_custom” to “MyCustomScenario”). Defaults to ''.

Returns:

AtomicAttackEvaluationIdentifier

Bases: EvaluationIdentifier

Evaluation identity for atomic attacks.

Per-child rules:

Non-target children (e.g., request_converters, response_converters) receive full recursive eval treatment, meaning they fully contribute to the hash.

ChildEvalRule

Per-child configuration for eval-hash computation.

Controls how a specific named child is treated when building the evaluation hash:

ComponentIdentifier

Immutable snapshot of a component’s behavioral configuration.

A single type for all component identity — scorers, targets, converters, and any future component types all produce a ComponentIdentifier with their relevant params and children.

The hash is content-addressed: two ComponentIdentifiers with the same class, params, and children produce the same hash. This enables deterministic metrics lookup, DB deduplication, and registry keying.

Methods:

from_dict

from_dict(data: dict[str, Any]) → ComponentIdentifier

Deserialize from a stored dictionary.

Reconstructs a ComponentIdentifier from data previously saved via to_dict(). Handles both the current format (class_name/class_module) and legacy format (__type__/__module__) for backward compatibility with older database records.

ParameterTypeDescription
dataDict[str, Any]Dictionary from DB/JSONL storage. The original dict is not mutated; a copy is made internally.

Returns:

get_child

get_child(key: str) → Optional[ComponentIdentifier]

Get a single child by key.

ParameterTypeDescription
keystrThe child key.

Returns:

Raises:

get_child_list

get_child_list(key: str) → list[ComponentIdentifier]

Get a list of children by key.

ParameterTypeDescription
keystrThe child key.

Returns:

normalize

normalize(value: Union[ComponentIdentifier, dict[str, Any]]) → ComponentIdentifier

Normalize a value to a ComponentIdentifier instance.

Accepts either an existing ComponentIdentifier (returned as-is) or a dict (reconstructed via from_dict). This supports code paths that may receive either typed identifiers or raw dicts from database storage.

ParameterTypeDescription
valueUnion[ComponentIdentifier, Dict[str, Any]]A ComponentIdentifier or a dictionary representation.

Returns:

Raises:

of

of(obj: object, params: Optional[dict[str, Any]] = None, children: Optional[dict[str, Union[ComponentIdentifier, list[ComponentIdentifier]]]] = None) → ComponentIdentifier

Build a ComponentIdentifier from a live object instance.

This factory method extracts class_name and class_module from the object’s type automatically, making it the preferred way to create identifiers in component implementations. None-valued params and children are filtered out to ensure backward-compatible hashing.

ParameterTypeDescription
objobjectThe live component instance whose type info will be captured.
paramsOptional[Dict[str, Any]]Behavioral parameters that affect the component’s output. Only include params that change behavior — exclude operational settings like rate limits, retry counts, or logging config. Defaults to None.
childrenOptional[Dict[str, Union[ComponentIdentifier, List[ComponentIdentifier]]]]Named child component identifiers. Use for compositional components like scorers that wrap other scorers or targets that chain converters. Defaults to None.

Returns:

to_dict

to_dict(max_value_length: Optional[int] = None) → dict[str, Any]

Serialize to a JSON-compatible dictionary for DB/JSONL storage.

Produces a flat structure where params are inlined at the top level alongside class_name, class_module, hash, and pyrit_version.

Children are recursively serialized into a nested “children” key.

ParameterTypeDescription
max_value_lengthOptional[int]If provided, string param values longer than this limit are truncated and suffixed with “...”. Useful for DB storage where column sizes may be limited. The truncation applies only to param values, not to structural keys like class_name or hash. The limit is propagated to children. Defaults to None (no truncation). Defaults to None.

Returns:

with_eval_hash

with_eval_hash(eval_hash: str) → ComponentIdentifier

Return a new frozen ComponentIdentifier with eval_hash set.

The original hash is preserved (important for identifiers reconstructed from truncated DB data where recomputation would produce a wrong hash).

ParameterTypeDescription
eval_hashstrThe evaluation hash to attach.

Returns:

EvaluationIdentifier

Bases: ABC

Wraps a ComponentIdentifier with domain-specific eval-hash configuration.

Subclasses set CHILD_EVAL_RULES — a mapping of child names to ChildEvalRule instances that control how each child is treated during eval-hash computation. Children not listed receive full recursive treatment.

The concrete eval_hash property delegates to the module-level compute_eval_hash free function.

Identifiable

Bases: ABC

Abstract base class for components that provide a behavioral identity.

Components implement _build_identifier() to return a frozen ComponentIdentifier snapshot. The identifier is built lazily on first access and cached for the component’s lifetime.

Methods:

get_identifier

get_identifier() → ComponentIdentifier

Get the component’s identifier, building it lazily on first access.

The identifier is computed once via _build_identifier() and then cached for subsequent calls. This ensures consistent identity throughout the component’s lifetime while deferring computation until actually needed.

Returns:

ScorerEvaluationIdentifier

Bases: EvaluationIdentifier

Evaluation identity for scorers.

The prompt_target child is filtered to behavioral params only (model_name, temperature, top_p), so the same scorer configuration on different deployments produces the same eval hash.