PyRIT Shell - Interactive REPL for PyRIT.
This module provides an interactive shell where PyRIT modules are loaded once at startup, making subsequent commands instant.
Functions¶
main¶
main() → intEntry point for pyrit_shell.
Returns:
int— Exit code.
merge_config_scenario_args¶
merge_config_scenario_args(config_scenario: Optional[ScenarioConfig], effective_scenario_name: str, cli_args: dict[str, Any]) → dict[str, Any]Merge config-file scenario args with CLI scenario args (CLI wins per-key).
When config_scenario.name does not match effective_scenario_name, the
config args are skipped with a warning so users are not silently surprised.
Mutable values are deep-copied so they don’t leak across runs.
| Parameter | Type | Description |
|---|---|---|
config_scenario | Optional[ScenarioConfig] | The scenario: block from the layered config, or None when not configured. |
effective_scenario_name | str | The scenario about to run (CLI wins). |
cli_args | dict[str, Any] | Scenario args supplied on the CLI. |
Returns:
dict[str, Any]— dict[str, Any]: The merged scenario-args dict to pass toset_params_from_args.
print_deprecation_message¶
print_deprecation_message(old_item: type | Callable[..., Any] | str, new_item: type | Callable[..., Any] | str, removed_in: str) → NoneEmit a deprecation warning.
| Parameter | Type | Description |
|---|---|---|
old_item | `type | Callable[..., Any] |
new_item | `type | Callable[..., Any] |
removed_in | str | The version in which the deprecated item will be removed |
PyRITShell¶
Bases: cmd.Cmd
Interactive shell for PyRIT.
Constructor Parameters:
| Parameter | Type | Description |
|---|---|---|
no_animation | bool | If True, skip the animated startup banner. Defaults to False. |
config_file | Optional[Path] | Path to a YAML configuration file. Defaults to None. |
database | Optional[str] | Database type (InMemory, SQLite, or AzureSQL). Defaults to None. |
initialization_scripts | Optional[list[Path]] | Initialization script paths. Defaults to None. |
initializer_names | Optional[list[Any]] | Initializer entries (names or dicts). Defaults to None. |
env_files | Optional[list[Path]] | Environment file paths to load in order. Defaults to None. |
log_level | Optional[int] | Logging level constant (e.g., logging.WARNING). Defaults to None. |
context | Optional[frontend_core.FrontendCore] | Deprecated. Pre-created FrontendCore context. Use the individual keyword arguments instead. Defaults to None. |
Methods:
cmdloop¶
cmdloop(intro: Optional[str] = None) → NoneOverride cmdloop to play animated banner before starting the REPL.
default¶
default(line: str) → NoneHandle unknown commands and convert hyphens to underscores.
do_clear¶
do_clear(arg: str) → NoneClear the screen.
do_exit¶
do_exit(arg: str) → boolExit the shell. Aliases: quit, q.
Returns:
bool— True to exit the shell.
do_help¶
do_help(arg: str) → NoneShow help. Usage: help [command].
do_list_initializers¶
do_list_initializers(arg: str) → NoneList all available initializers.
Raises:
RuntimeError— If initialization has not completed.
do_list_scenarios¶
do_list_scenarios(arg: str) → NoneList all available scenarios.
Raises:
RuntimeError— If initialization has not completed.
do_list_targets¶
do_list_targets(arg: str) → NoneList all available targets from the TargetRegistry.
Raises:
RuntimeError— If initialization has not completed.
do_print_scenario¶
do_print_scenario(arg: str) → NonePrint detailed results for scenario runs.
do_run¶
do_run(line: str) → NoneRun a scenario.
Raises:
RuntimeError— If initialization has not completed.
do_scenario_history¶
do_scenario_history(arg: str) → NoneShows a numbered list of all scenario runs with the commands used.
emptyline¶
emptyline() → boolDon’t repeat last command on empty line.
Returns:
bool— False to prevent repeating the last command.
ScenarioRegistry¶
Bases: BaseClassRegistry['Scenario', ScenarioMetadata]
Registry for discovering and managing available scenario classes.
This class discovers all Scenario subclasses from:
Built-in scenarios in pyrit.scenario.scenarios module
User-defined scenarios from initialization scripts (set via globals)
Scenarios are identified by their dotted name (e.g., “garak.encoding”, “foundry.red_team_agent”).
Constructor Parameters:
| Parameter | Type | Description |
|---|---|---|
lazy_discovery | bool | If True, discovery is deferred until first access. Defaults to True for performance. Defaults to True. |
Methods:
discover_user_scenarios¶
discover_user_scenarios() → NoneDiscover user-defined scenarios from global variables.
After initialization scripts are executed, they may define Scenario subclasses and store them in globals. This method searches for such classes.
User scenarios will override built-in scenarios with the same name.
ScenarioResult¶
Scenario result class for aggregating scenario results.
Constructor Parameters:
| Parameter | Type | Description |
|---|---|---|
scenario_identifier | ScenarioIdentifier | Identifier for the executed scenario. |
objective_target_identifier | ComponentIdentifier | Target identifier. |
attack_results | dict[str, List[AttackResult]] | Results grouped by atomic attack name. |
objective_scorer_identifier | ComponentIdentifier | Objective scorer identifier. |
scenario_run_state | ScenarioRunState | Current scenario run state. Defaults to 'CREATED'. |
labels | Optional[dict[str, str]] | Optional labels. Defaults to None. |
completion_time | Optional[datetime] | Optional completion timestamp. Defaults to None. |
number_tries | int | Number of run attempts. Defaults to 0. |
id | Optional[uuid.UUID] | Optional scenario result ID. Defaults to None. |
display_group_map | Optional[dict[str, str]] | Optional mapping of atomic_attack_name → display group label. Used by the console printer to aggregate results for user-facing output. Defaults to None. |
Methods:
get_display_groups¶
get_display_groups() → dict[str, list[AttackResult]]Aggregate attack results by display group.
When a display_group_map was provided, results from multiple
atomic_attack_name keys that share the same display group are
merged into a single list. When no map was provided, this returns
the same structure as attack_results (identity mapping).
Returns:
dict[str, list[AttackResult]]— dict[str, list[AttackResult]]: Results grouped by display label.
get_objectives¶
get_objectives(atomic_attack_name: Optional[str] = None) → list[str]Get the list of unique objectives for this scenario.
| Parameter | Type | Description |
|---|---|---|
atomic_attack_name | Optional[str] | Name of specific atomic attack to include. If None, includes objectives from all atomic attacks. Defaults to None. Defaults to None. |
Returns:
list[str]— List[str]: Deduplicated list of objectives.
get_scorer_evaluation_metrics¶
get_scorer_evaluation_metrics() → Optional[ScorerMetrics]Get the evaluation metrics for the scenario’s scorer from the scorer evaluation registry.
Returns:
Optional[ScorerMetrics]— The evaluation metrics object, or None if not found.
get_strategies_used¶
get_strategies_used() → list[str]Get the list of strategies used in this scenario.
Returns:
list[str]— List[str]: Atomic attack strategy names present in the results.
normalize_scenario_name¶
normalize_scenario_name(scenario_name: str) → strNormalize a scenario name to match the stored class name format.
Converts CLI-style snake_case names (e.g., “foundry” or “content_harms”) to PascalCase class names (e.g., “Foundry” or “ContentHarms”) for database queries. If the input is already in PascalCase or doesn’t match the snake_case pattern, it is returned unchanged.
This is the inverse of ScenarioRegistry._class_name_to_scenario_name().
| Parameter | Type | Description |
|---|---|---|
scenario_name | str | The scenario name to normalize. |
Returns:
str— The normalized scenario name suitable for database queries.
objective_achieved_rate¶
objective_achieved_rate(atomic_attack_name: Optional[str] = None) → intGet the success rate of this scenario.
| Parameter | Type | Description |
|---|---|---|
atomic_attack_name | Optional[str] | Name of specific atomic attack to calculate rate for. If None, calculates rate across all atomic attacks. Defaults to None. Defaults to None. |
Returns:
int— Success rate as a percentage (0-100).