pyrit.scenario.AtomicAttack#
- class AtomicAttack(*, atomic_attack_name: str, attack: AttackStrategy, objectives: List[str], prepended_conversations: List[List[Message]] | None = None, seed_groups: List[SeedGroup] | None = None, custom_prompts: List[str] | None = None, 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 automatically detects whether the attack is single-turn or multi-turn and calls the appropriate executor method. For single-turn attacks, you can provide seed_groups. For multi-turn attacks, you can provide custom_prompts.
Example
>>> from pyrit.scenario import AtomicAttack >>> from pyrit.attacks import PromptAttack >>> from pyrit.prompt_target import OpenAIChatTarget >>> >>> target = OpenAIChatTarget() >>> attack = PromptAttack(objective_target=target) >>> objectives = ["how to make a bomb", "how to hack a system"] >>> >>> atomic_attack = AtomicAttack( ... attack=attack, ... objectives=objectives, ... memory_labels={"test": "run1"} ... ) >>> results = await atomic_attack.run_async(max_concurrency=5) >>> >>> # With prepended conversations >>> from pyrit.models import Message >>> conversation = [Message(...)] >>> atomic_attack = AtomicAttack( ... attack=attack, ... objectives=objectives, ... prepended_conversations=[conversation] ... ) >>> results = await atomic_attack.run_async(max_concurrency=5) >>> >>> # Single-turn attack with seeds >>> from pyrit.models import SeedGroup >>> seeds = [SeedGroup(...), SeedGroup(...)] >>> atomic_attack = AtomicAttack( ... attack=single_turn_attack, ... objectives=objectives, ... seed_groups=seeds ... ) >>> results = await atomic_attack.run_async(max_concurrency=3) >>> >>> # Multi-turn attack with custom prompts >>> custom_prompts = ["Tell me about chemistry", "Explain system administration"] >>> atomic_attack = AtomicAttack( ... attack=multi_turn_attack, ... objectives=objectives, ... custom_prompts=custom_prompts ... ) >>> results = await atomic_attack.run_async(max_concurrency=3)
- __init__(*, atomic_attack_name: str, attack: AttackStrategy, objectives: List[str], prepended_conversations: List[List[Message]] | None = None, seed_groups: List[SeedGroup] | None = None, custom_prompts: List[str] | None = None, memory_labels: Dict[str, str] | None = None, **attack_execute_params: Any) None[source]#
Initialize an atomic attack with an attack strategy and dataset parameters.
- Parameters:
atomic_attack_name (str) – Used to group an AtomicAttack with related attacks for a strategy.
attack (AttackStrategy) – The configured attack strategy to execute.
objectives (List[str]) – List of attack objectives to test against.
prepended_conversations (Optional[List[List[Message]]]) – Optional list of conversation histories to prepend to each attack execution. This will be used for all objectives.
seed_groups (Optional[List[SeedGroup]]) – List of seed groups for single-turn attacks. Only valid for single-turn attacks.
custom_prompts (Optional[List[str]]) – List of custom prompts for multi-turn attacks. Only valid for multi-turn attacks.
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 objectives list is empty.
TypeError – If seed_groups is provided for multi-turn attacks or custom_prompts is provided for single-turn attacks.
Methods
__init__(*, atomic_attack_name, attack, ...)Initialize an atomic attack with an attack strategy and dataset parameters.
run_async(*[, max_concurrency, ...])Execute the atomic attack against all objectives in the dataset.
Attributes
Get a copy of the objectives list for this atomic attack.
- property objectives: List[str]#
Get a copy of the objectives list for this atomic attack.
This property is read-only. To use different objectives, create a new AtomicAttack instance.
- Returns:
A copy of the objectives list.
- 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 objectives in the dataset.
This method uses AttackExecutor to run the configured attack against all objectives from the dataset. It automatically detects whether to use single-turn or multi-turn execution based on the attack’s context type.
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:
AttackExecutorResult[AttackResult]
- Raises:
ValueError – If the attack execution fails completely and return_partial_on_failure=False.