pyrit.scenario.AtomicAttack#
- class AtomicAttack(*, atomic_attack_name: str, attack: AttackStrategy, seed_groups: List[SeedGroup], memory_labels: Dict[str, str] | None = None, **attack_execute_params: Any)[source]#
Bases:
objectRepresents a single atomic attack test combining an attack strategy and dataset.
An AtomicAttack is an executable unit that executes a configured attack against all objectives in a dataset. Multiple AtomicAttacks can be grouped together into larger test scenarios for comprehensive security testing and evaluation.
The AtomicAttack uses SeedGroups as the single source of truth for objectives, prepended conversations, and next messages. Each SeedGroup must have an objective set.
Example
>>> from pyrit.scenario import AtomicAttack >>> from pyrit.attacks import PromptAttack >>> from pyrit.prompt_target import OpenAIChatTarget >>> from pyrit.models import SeedGroup >>> >>> target = OpenAIChatTarget() >>> attack = PromptAttack(objective_target=target) >>> >>> # Create seed groups with objectives >>> seed_groups = SeedGroup.from_yaml_file("seeds.yaml") >>> for sg in seed_groups: ... sg.set_objective("your objective here") >>> >>> atomic_attack = AtomicAttack( ... atomic_attack_name="test_attack", ... attack=attack, ... seed_groups=seed_groups, ... memory_labels={"test": "run1"} ... ) >>> results = await atomic_attack.run_async(max_concurrency=5)
- __init__(*, atomic_attack_name: str, attack: AttackStrategy, seed_groups: List[SeedGroup], memory_labels: Dict[str, str] | None = None, **attack_execute_params: Any) None[source]#
Initialize an atomic attack with an attack strategy and seed groups.
- Parameters:
atomic_attack_name (str) – Used to group an AtomicAttack with related attacks for a strategy.
attack (AttackStrategy) – The configured attack strategy to execute.
seed_groups (List[SeedGroup]) – List of seed groups. Each seed group must have an objective set. The seed groups serve as the single source of truth for objectives, prepended conversations, and next messages.
memory_labels (Optional[Dict[str, str]]) – Additional labels to apply to prompts. These labels help track and categorize the atomic attack in memory.
**attack_execute_params (Any) – Additional parameters to pass to the attack execution method (e.g., batch_size).
- Raises:
ValueError – If seed_groups list is empty or any seed group is missing an objective.
Methods
__init__(*, atomic_attack_name, attack, ...)Initialize an atomic attack with an attack strategy and seed groups.
filter_seed_groups_by_objectives(*, ...)Filter seed groups to only those with objectives in the remaining list.
run_async(*[, max_concurrency, ...])Execute the atomic attack against all seed groups.
Attributes
Get the objectives from the seed groups.
Get a copy of the seed groups list for this atomic attack.
- filter_seed_groups_by_objectives(*, remaining_objectives: List[str]) None[source]#
Filter seed groups to only those with objectives in the remaining list.
This is used for scenario resumption to skip already completed objectives.
- Parameters:
remaining_objectives (List[str]) – List of objectives that still need to be executed.
- property objectives: List[str]#
Get the objectives from the seed groups.
- Returns:
List of objectives from all seed groups.
- Return type:
List[str]
- async run_async(*, max_concurrency: int = 1, return_partial_on_failure: bool = True, **attack_params) AttackExecutorResult[AttackResult][source]#
Execute the atomic attack against all seed groups.
This method uses AttackExecutor to run the configured attack against all seed groups.
When return_partial_on_failure=True (default), this method will return an AttackExecutorResult containing both completed results and incomplete objectives (those that didn’t finish execution due to exceptions). This allows scenarios to save progress and retry only the incomplete objectives.
Note: “completed” means the execution finished, not that the attack objective was achieved. “incomplete” means execution didn’t finish (threw an exception).
- Parameters:
max_concurrency (int) – Maximum number of concurrent attack executions. Defaults to 1 for sequential execution.
return_partial_on_failure (bool) – If True, returns partial results even when some objectives don’t complete execution. If False, raises an exception on any execution failure. Defaults to True.
**attack_params – Additional parameters to pass to the attack strategy.
- Returns:
- Result containing completed attack results and
incomplete objectives (those that didn’t finish execution).
- Return type:
- Raises:
ValueError – If the attack execution fails completely and return_partial_on_failure=False.