pyrit.identifiers.ComponentIdentifier#
- class ComponentIdentifier(class_name: str, class_module: str, params: dict[str, ~typing.Any] = <factory>, children: dict[str, ~pyrit.identifiers.component_identifier.ComponentIdentifier | list[~pyrit.identifiers.component_identifier.ComponentIdentifier]] = <factory>, pyrit_version: str = <factory>)[source]#
Bases:
objectImmutable 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.
- __init__(class_name: str, class_module: str, params: dict[str, ~typing.Any] = <factory>, children: dict[str, ~pyrit.identifiers.component_identifier.ComponentIdentifier | list[~pyrit.identifiers.component_identifier.ComponentIdentifier]] = <factory>, pyrit_version: str = <factory>) None#
Methods
__init__(class_name, class_module[, params, ...])from_dict(data)Deserialize from a stored dictionary.
get_child(key)Get a single child by key.
get_child_list(key)Get a list of children by key.
normalize(value)Normalize a value to a ComponentIdentifier instance.
of(obj, *[, params, children])Build a ComponentIdentifier from a live object instance.
to_dict(*[, max_value_length])Serialize to a JSON-compatible dictionary for DB/JSONL storage.
Attributes
Return the first 8 characters of the hash for display and logging.
class_name::short_hash.Python class name (e.g., "SelfAskScaleScorer").
Full module path (e.g., "pyrit.score.self_ask_scale_scorer").
Behavioral parameters that affect output.
Named child identifiers for compositional identity (e.g., a scorer's target).
Content-addressed SHA256 hash computed from class, params, and children.
Version tag for storage.
- children: dict[str, ComponentIdentifier | list[ComponentIdentifier]]#
Named child identifiers for compositional identity (e.g., a scorer’s target).
- classmethod from_dict(data: dict[str, Any]) ComponentIdentifier[source]#
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.Note
This reconstruction is lossy. If
to_dict()was called with amax_value_lengthlimit, param values may have been truncated before storage. The original untruncated values cannot be recovered. To preserve correct identity, the stored hash (computed from the original untruncated data) is kept as-is rather than recomputed from the potentially truncated params.- Parameters:
data (Dict[str, Any]) – Dictionary from DB/JSONL storage. The original dict is not mutated; a copy is made internally.
- Returns:
- Reconstructed identifier with the stored hash
preserved (if available) to maintain correct identity despite potential param truncation.
- Return type:
- get_child(key: str) ComponentIdentifier | None[source]#
Get a single child by key.
- Parameters:
key (str) – The child key.
- Returns:
The child, or None if not found.
- Return type:
Optional[ComponentIdentifier]
- Raises:
ValueError – If the child is a list (use get_child_list instead).
- get_child_list(key: str) list[ComponentIdentifier][source]#
Get a list of children by key.
- Parameters:
key (str) – The child key.
- Returns:
- The children. Returns empty list if
not found, wraps single child in a list.
- Return type:
List[ComponentIdentifier]
- classmethod normalize(value: ComponentIdentifier | dict[str, Any]) ComponentIdentifier[source]#
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.
- Parameters:
value (Union[ComponentIdentifier, Dict[str, Any]]) – A ComponentIdentifier or a dictionary representation.
- Returns:
The normalized identifier instance.
- Return type:
- Raises:
TypeError – If value is neither a ComponentIdentifier nor a dict.
- classmethod of(obj: object, *, params: dict[str, Any] | None = None, children: dict[str, ComponentIdentifier | list[ComponentIdentifier]] | None = None) ComponentIdentifier[source]#
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.
- Parameters:
obj (object) – The live component instance whose type info will be captured.
params (Optional[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.
children (Optional[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.
- Returns:
The frozen identity snapshot with computed hash.
- Return type:
- property short_hash: str#
Return the first 8 characters of the hash for display and logging.
- Returns:
First 8 hex characters of the SHA256 hash.
- Return type:
- to_dict(*, max_value_length: int | None = None) dict[str, Any][source]#
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.
- Parameters:
max_value_length (Optional[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).
- Returns:
- JSON-serializable dictionary suitable for database storage
or JSONL export.
- Return type:
Dict[str, Any]