pyrit.executor.attack.AttackStrategy#

class AttackStrategy(*, objective_target: ~pyrit.prompt_target.common.prompt_target.PromptTarget, context_type: type[~pyrit.executor.attack.core.attack_strategy.AttackStrategyContextT], params_type: ~typing.Type[~pyrit.executor.attack.core.attack_parameters.AttackParamsT] = <class 'pyrit.executor.attack.core.attack_parameters.AttackParameters'>, logger: ~logging.Logger = <Logger ai-red-team (INFO)>)[source]#

Bases: Strategy[AttackStrategyContextT, AttackStrategyResultT], ABC

Abstract base class for attack strategies. Defines the interface for executing attacks and handling results.

__init__(*, objective_target: ~pyrit.prompt_target.common.prompt_target.PromptTarget, context_type: type[~pyrit.executor.attack.core.attack_strategy.AttackStrategyContextT], params_type: ~typing.Type[~pyrit.executor.attack.core.attack_parameters.AttackParamsT] = <class 'pyrit.executor.attack.core.attack_parameters.AttackParameters'>, logger: ~logging.Logger = <Logger ai-red-team (INFO)>)[source]#

Initialize the attack strategy with a specific context type and logger.

Parameters:
  • objective_target (PromptTarget) – The target system to attack.

  • context_type (type[AttackStrategyContextT]) – The type of context this strategy operates on.

  • params_type (Type[AttackParamsT]) – The type of parameters this strategy accepts. Defaults to AttackParameters. Use AttackParameters.excluding() to create a params type that rejects certain fields.

  • logger (logging.Logger) – Logger instance for logging events.

Methods

__init__(*, objective_target, context_type)

Initialize the attack strategy with a specific context type and logger.

execute_async()

Execute the attack strategy asynchronously with the provided parameters.

execute_with_context_async(*, context)

Execute strategy with complete lifecycle management.

get_attack_scoring_config()

Get the attack scoring configuration used by this strategy.

get_identifier()

Get a serializable identifier for the strategy instance.

get_objective_target()

Get the objective target for this attack strategy.

Attributes

params_type

Get the parameters type for this attack strategy.

async execute_async(*, objective: str, next_message: Message | None = None, prepended_conversation: List[Message] | None = None, memory_labels: dict[str, str] | None = None, **kwargs) AttackStrategyResultT[source]#
async execute_async(**kwargs) AttackStrategyResultT

Execute the attack strategy asynchronously with the provided parameters.

This method provides a stable contract for all attacks. The signature includes all standard parameters (objective, next_message, prepended_conversation, memory_labels). Attacks that don’t accept certain parameters will raise ValueError if those parameters are provided.

Parameters:
  • objective (str) – The objective of the attack.

  • next_message (Optional[Message]) – Message to send to the target.

  • prepended_conversation (Optional[List[Message]]) – Conversation to prepend.

  • memory_labels (Optional[Dict[str, str]]) – Memory labels for the attack context.

  • **kwargs – Additional context-specific parameters (conversation_id, system_prompt, etc.).

Returns:

The result of the attack execution.

Return type:

AttackStrategyResultT

Raises:

ValueError – If required parameters are missing or if unsupported parameters are provided.

get_attack_scoring_config() AttackScoringConfig | None[source]#

Get the attack scoring configuration used by this strategy.

Returns:

The scoring configuration, or None if not applicable.

Return type:

Optional[AttackScoringConfig]

Note

Subclasses that use scoring should override this method to return their scoring configuration. The default implementation returns None.

get_objective_target() PromptTarget[source]#

Get the objective target for this attack strategy.

Returns:

The target system being attacked.

Return type:

PromptTarget

property params_type: Type[AttackParameters]#

Get the parameters type for this attack strategy.

Returns:

The parameters type this strategy accepts.

Return type:

Type[AttackParameters]