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.cli.pyrit_shell

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() → int

Entry point for pyrit_shell.

Returns:

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.

ParameterTypeDescription
config_scenarioOptional[ScenarioConfig]The scenario: block from the layered config, or None when not configured.
effective_scenario_namestrThe scenario about to run (CLI wins).
cli_argsdict[str, Any]Scenario args supplied on the CLI.

Returns:

print_deprecation_message(old_item: type | Callable[..., Any] | str, new_item: type | Callable[..., Any] | str, removed_in: str) → None

Emit a deprecation warning.

ParameterTypeDescription
old_item`typeCallable[..., Any]
new_item`typeCallable[..., Any]
removed_instrThe version in which the deprecated item will be removed

PyRITShell

Bases: cmd.Cmd

Interactive shell for PyRIT.

Constructor Parameters:

ParameterTypeDescription
no_animationboolIf True, skip the animated startup banner. Defaults to False.
config_fileOptional[Path]Path to a YAML configuration file. Defaults to None.
databaseOptional[str]Database type (InMemory, SQLite, or AzureSQL). Defaults to None.
initialization_scriptsOptional[list[Path]]Initialization script paths. Defaults to None.
initializer_namesOptional[list[Any]]Initializer entries (names or dicts). Defaults to None.
env_filesOptional[list[Path]]Environment file paths to load in order. Defaults to None.
log_levelOptional[int]Logging level constant (e.g., logging.WARNING). Defaults to None.
contextOptional[frontend_core.FrontendCore]Deprecated. Pre-created FrontendCore context. Use the individual keyword arguments instead. Defaults to None.

Methods:

cmdloop

cmdloop(intro: Optional[str] = None) → None

Override cmdloop to play animated banner before starting the REPL.

default

default(line: str) → None

Handle unknown commands and convert hyphens to underscores.

do_clear

do_clear(arg: str) → None

Clear the screen.

do_exit

do_exit(arg: str) → bool

Exit the shell. Aliases: quit, q.

Returns:

do_help

do_help(arg: str) → None

Show help. Usage: help [command].

do_list_initializers

do_list_initializers(arg: str) → None

List all available initializers.

Raises:

do_list_scenarios

do_list_scenarios(arg: str) → None

List all available scenarios.

Raises:

do_list_targets

do_list_targets(arg: str) → None

List all available targets from the TargetRegistry.

Raises:

do_print_scenario

do_print_scenario(arg: str) → None

Print detailed results for scenario runs.

do_run

do_run(line: str) → None

Run a scenario.

Raises:

do_scenario_history

do_scenario_history(arg: str) → None

Shows a numbered list of all scenario runs with the commands used.

emptyline

emptyline() → bool

Don’t repeat last command on empty line.

Returns:

ScenarioRegistry

Bases: BaseClassRegistry['Scenario', ScenarioMetadata]

Registry for discovering and managing available scenario classes.

This class discovers all Scenario subclasses from:

  1. Built-in scenarios in pyrit.scenario.scenarios module

  2. 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:

ParameterTypeDescription
lazy_discoveryboolIf True, discovery is deferred until first access. Defaults to True for performance. Defaults to True.

Methods:

discover_user_scenarios

discover_user_scenarios() → None

Discover 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:

ParameterTypeDescription
scenario_identifierScenarioIdentifierIdentifier for the executed scenario.
objective_target_identifierComponentIdentifierTarget identifier.
attack_resultsdict[str, List[AttackResult]]Results grouped by atomic attack name.
objective_scorer_identifierComponentIdentifierObjective scorer identifier.
scenario_run_stateScenarioRunStateCurrent scenario run state. Defaults to 'CREATED'.
labelsOptional[dict[str, str]]Optional labels. Defaults to None.
completion_timeOptional[datetime]Optional completion timestamp. Defaults to None.
number_triesintNumber of run attempts. Defaults to 0.
idOptional[uuid.UUID]Optional scenario result ID. Defaults to None.
display_group_mapOptional[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:

get_objectives

get_objectives(atomic_attack_name: Optional[str] = None) → list[str]

Get the list of unique objectives for this scenario.

ParameterTypeDescription
atomic_attack_nameOptional[str]Name of specific atomic attack to include. If None, includes objectives from all atomic attacks. Defaults to None. Defaults to None.

Returns:

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:

get_strategies_used

get_strategies_used() → list[str]

Get the list of strategies used in this scenario.

Returns:

normalize_scenario_name

normalize_scenario_name(scenario_name: str) → str

Normalize 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().

ParameterTypeDescription
scenario_namestrThe scenario name to normalize.

Returns:

objective_achieved_rate

objective_achieved_rate(atomic_attack_name: Optional[str] = None) → int

Get the success rate of this scenario.

ParameterTypeDescription
atomic_attack_nameOptional[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: