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.executor.core

Core executor module.

Strategy

Bases: ABC, Generic[StrategyContextT, StrategyResultT]

Abstract base class for strategies with enforced lifecycle management.

Ensures a consistent execution flow: validate -> setup -> execute -> teardown. The teardown phase is guaranteed to run even if exceptions occur.

Subclasses must implement: _validate_context(): Validate context _setup_async(): Initialize resources _perform_async(): Execute the logic _teardown_async(): Clean up resources

Constructor Parameters:

ParameterTypeDescription
context_typetype[StrategyContextT]The type of context this strategy will use.
event_handlerOptional[StrategyEventHandler[StrategyContextT, StrategyResultT]]An optional event handler for strategy events. Defaults to None.
loggerlogging.LoggerThe logger to use for this strategy. Defaults to logger.

Methods:

execute_async

execute_async(kwargs: Any = {}) → StrategyResultT

Execute the strategy asynchronously with the given keyword arguments.

Returns:

execute_with_context_async

execute_with_context_async(context: StrategyContextT) → StrategyResultT

Execute strategy with complete lifecycle management.

Enforces: validate -> setup -> execute -> teardown.

ParameterTypeDescription
contextStrategyContextTThe context for the strategy, containing configuration and state.

Returns:

Raises:

StrategyContext

Bases: ABC

Base class for all strategy contexts.

Methods:

duplicate

duplicate() → StrategyContextT

Create a deep copy of the context.

Returns:

StrategyConverterConfig

Configuration for prompt converters used in strategies.

This class defines the converter configurations that transform prompts during the strategy process, both for requests and responses.

StrategyEvent

Bases: Enum

Enumeration of all strategy lifecycle events.

StrategyEventData

Bases: Generic[StrategyContextT, StrategyResultT]

Data passed to event observers.

StrategyEventHandler

Bases: ABC, Generic[StrategyContextT, StrategyResultT]

Abstract base class for strategy event handlers.

Methods:

on_event

on_event(event_data: StrategyEventData[StrategyContextT, StrategyResultT]) → None

Handle a strategy event.

ParameterTypeDescription
event_dataStrategyEventData[StrategyContextT, StrategyResultT]Data about the event that occurred.