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

Deprecation shim — pyrit.identifiers was renamed to pyrit.models.identifiers in 0.14.

This module emits a DeprecationWarning (one per name per process) on first access of each public symbol and returns the symbol from its new location. The shim will be removed in 0.16.0.

Functions

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], own_rule: ChildEvalRule | None = None, root_unwrap_child: str | None = None) → 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. own_rule extends this to the root entity itself, which is required for leaf components (e.g., a target) whose own params need filtering and which have no relevant children to delegate to. 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 both child_eval_rules is empty and own_rule is None (and no root unwrap applies), 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.
own_rule`ChildEvalRuleNone`
root_unwrap_child`strNone`

Returns:

Raises:

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:

print_deprecation_message

print_deprecation_message(old_item: type | Callable[..., Any] | str, new_item: type | Callable[..., Any] | str, removed_in: str) → None

Emit a deprecation warning.

ParameterTypeDescription
old_item`typeCallable[..., Any]
new_item`typeCallable[..., Any]
removed_instrThe version in which the deprecated item will be removed

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:

validate_registry_name

validate_registry_name(name: str) → None

Validate that name is a legal registry name.

ParameterTypeDescription
namestrThe name to validate.

Raises:

AtomicAttackEvaluationIdentifier

Bases: EvaluationIdentifier

Evaluation identity for atomic attacks.

Rules are derived from AtomicAttackIdentifier’s field markers, which propagate down the technique/attack subtree. The behavioral projection of targets, the objective_target restriction to temperature, and the exclusion of objective_scorer / seed_identifiers all live on the typed identifier fields.

ChildEvalRule

Bases: BaseModel

Per-child configuration for eval-hash computation.

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

ComponentIdentifier

Bases: BaseModel

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.

Typed projections: subclasses (TargetIdentifier, ConverterIdentifier, …) may promote well-known params and children to ordinary typed fields. Promotion is automatic and keyed off the field’s annotation: a scalar field maps to a params entry; a field annotated as a ComponentIdentifier subclass (or a list thereof) maps to a children slot of the same name. The promoted value is mirrored back into params / children before hashing, so a typed subclass serializes and hashes identically to a plain ComponentIdentifier built with the same params/children. Non-promoted members simply stay in params / children.

Serialization: model_dump() returns a flat dict where reserved keys (class_name, class_module, hash, pyrit_version, eval_hash, children) sit at the top level alongside the inlined param values. This shape is also the storage / REST format. Param values are stored in full (no truncation). model_validate() accepts the same flat shape (plus a structured form with an explicit params dict); the content hash is always recomputed on validation, so any stored hash is ignored.

Mutability: the model is frozen, but params and children are dicts whose contents are not deep-frozen — mutating them after construction creates an identifier whose stored hash no longer matches its content. Treat every identifier as a fully immutable value.

Methods:

from_component_identifier

from_component_identifier(identifier: ComponentIdentifier) → Self

Return identifier as an instance of this typed subclass.

Pass-through when identifier is already an instance of cls; otherwise revalidate its flat dump into cls (e.g. a base identifier loaded from the DB), rehydrating promoted typed fields. The content hash is recomputed identically across the round-trip.

ParameterTypeDescription
identifierComponentIdentifierA ComponentIdentifier (possibly the base type).

Returns:

from_dict

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

Reconstruct from a flat dict (deprecated; use model_validate instead).

ParameterTypeDescription
datadict[str, Any]The flat storage dict.

Returns:

get_child

get_child(key: str) → ComponentIdentifier | None

Get a single child by key.

ParameterTypeDescription
keystrChild name.

Returns:

Raises:

get_child_list

get_child_list(key: str) → list[ComponentIdentifier]

Get a list of children by key. Wraps singletons; [] if missing.

ParameterTypeDescription
keystrChild name.

Returns:

of

of(obj: object, params: dict[str, Any] | None = None, children: dict[str, ComponentIdentifier | list[ComponentIdentifier]] | None = None, promoted: Any = {}) → Self

Build a ComponentIdentifier from a live object instance.

Extracts class_name and class_module from the object’s type automatically. None-valued params and children are filtered out to keep schemas backward-compatible.

ParameterTypeDescription
objobjectThe live object whose class metadata will populate the identifier.
params`dict[str, Any]None`
children`dict[str, ComponentIdentifierlist[ComponentIdentifier]]
**promotedAnyOptional promoted typed fields (for subclasses). Passed by name; None values are dropped. These are mirrored back into params / children automatically. Defaults to {}.

Returns:

to_dict

to_dict() → dict[str, Any]

Return the flat storage dict (deprecated; use model_dump instead).

Returns:

with_eval_hash

with_eval_hash(eval_hash: str) → ComponentIdentifier

Return a new identifier with eval_hash set.

This is the single supported way to set eval_hash: it is not computed by the base model, so callers attach it here rather than via the constructor. The content hash is recomputed from the (unchanged) params and children, so it is identical to this identifier’s hash.

ParameterTypeDescription
eval_hashstrThe evaluation hash to attach.

Returns:

EvaluationIdentifier

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

Concrete subclasses name their root typed-identifier type via EVAL_ROOT; the per-child rules (CHILD_EVAL_RULES), the root OWN_RULE, and the root-unwrap slot (ROOT_UNWRAP_CHILD) are then derived from the Evaluate.* field markers on that type’s class graph (see derive_eval_config). The typed identifier fields are the single source of truth for what feeds the eval hash.

Subclasses may instead set CHILD_EVAL_RULES (and optionally OWN_RULE / ROOT_UNWRAP_CHILD) directly to bypass derivation.

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:

IdentifierFilter

Bases: BaseModel

Immutable filter definition for matching JSON-backed identifier properties.

IdentifierType

Bases: Enum

Enumeration of supported identifier types for filtering.

ObjectiveTargetEvaluationIdentifier

Bases: EvaluationIdentifier

Evaluation identity for an objective target.

Rules are derived from TargetIdentifier’s field markers: the target’s own params are filtered to the behavioral set (underlying_model_name, temperature, top_p) via the derived OWN_RULE, and wrapper targets (e.g. RoundRobinTarget) are unwrapped at the root, so the same logical target produces the same eval hash whether bare or wrapped.

ScorerEvaluationIdentifier

Bases: EvaluationIdentifier

Evaluation identity for scorers.

Rules are derived from ScorerIdentifier’s field markers. The prompt_target child is projected to behavioral target params only (underlying_model_name, temperature, top_p) and wrapper targets are unwrapped, so the same scorer configuration on different deployments produces the same eval hash.

ScorerIdentifier

Bases: ComponentIdentifier

Strongly-typed projection of a Scorer’s ComponentIdentifier.

Promotes the scorer_type discriminator, the score_aggregator name, and the scorer’s own child slots — prompt_target (an LLM target) and sub_scorers (nested scorers).