Module containing initialization PyRIT.
Functions¶
get_lazy_dir¶
get_lazy_dir(module_globals: dict[str, Any], exports: Mapping[str, LazyExport]) → list[str]Return package attributes and unresolved public exports.
| Parameter | Type | Description |
|---|---|---|
module_globals | dict[str, Any] | The package globals. |
exports | Mapping[str, LazyExport] | Public names and their implementation locations. |
Returns:
list[str]— list[str]: Sorted package attribute names.
initialize_from_config_async¶
initialize_from_config_async(config_path: str | pathlib.Path | None = None) → ConfigurationLoaderInitialize PyRIT from a configuration file.
This is a convenience function that loads a ConfigurationLoader from a YAML file and initializes PyRIT.
| Parameter | Type | Description |
|---|---|---|
config_path | str | pathlib.Path | None | Path to the configuration file. If None, uses the default path (~/.pyrit/.pyrit_conf). Can be a string or pathlib.Path. Defaults to None. |
Returns:
ConfigurationLoader— The loaded ConfigurationLoader instance.
Raises:
FileNotFoundError— If the configuration file does not exist.ValueError— If the configuration is invalid.
Examples:
initialize_pyrit_async¶
initialize_pyrit_async(memory_db_type: MemoryDatabaseType | str, initialization_scripts: Sequence[str | pathlib.Path] | None = None, initializers: Sequence[PyRITInitializer] | None = None, load_defaults: bool = True, env_files: Sequence[pathlib.Path] | None = None, env_akv_ref: Sequence[str] | None = None, env_akv_strict: bool = True, silent: bool = False, seed: int | None = None, raise_on_initializer_error: bool = True, memory_instance_kwargs: Any = {}) → NoneInitialize PyRIT with the provided memory instance and loads environment files.
| Parameter | Type | Description |
|---|---|---|
memory_db_type | MemoryDatabaseType | The MemoryDatabaseType string literal which indicates the memory instance to use for central memory. Options include “InMemory”, “SQLite”, and “AzureSQL”. |
initialization_scripts | Sequence[str | pathlib.Path] | None | Optional sequence of local Python script paths that define PyRITInitializer subclasses. Every initializer subclass defined in each file is loaded and executed. Loading is handled by the InitializerRegistry. Defaults to None. |
initializers | Sequence[PyRITInitializer] | None | Optional sequence of PyRITInitializer instances to execute directly. These provide type-safe, validated configuration with clear documentation. Defaults to None. |
load_defaults | bool | If True (default) AND the caller supplies neither initializers nor initialization_scripts, a default initializer set is run so a bare initialize_pyrit_async(...) yields a usable environment: the core attack-technique catalog (TechniqueInitializer, populating the AttackTechniqueRegistry) plus the available default targets (TargetInitializer, registering whatever endpoints are configured via env vars). Supplying any initializer or script means the caller owns setup, so the defaults are skipped; set this to False to also skip them on a bare call (e.g. to start from an empty state). Only the core techniques and default targets are loaded — extra / per-source technique groups and scorer target variants remain opt-in. |
env_files | Sequence[pathlib.Path] | None | Optional sequence of environment file paths to load in order. Ordinary files fill missing process values; files named .env.local override. If omitted, PyRIT auto-discovers supported .env and .env.local files. Defaults to None. |
env_akv_ref | Sequence[str] | None | Optional zero-or-one-item sequence containing an Azure Key Vault URL whose secret value is a bootstrap dotenv document. The document fills missing process values and supports complete-value references to scalar secrets. Requires azure-keyvault-secrets. Defaults to None. |
env_akv_strict | bool | If True, reject malformed bootstrap entries and Key Vault reference syntax. If False, warn and skip those entries. Operational Key Vault failures always raise. Defaults to True. |
silent | bool | If True, suppresses print statements about environment file loading and schema migration. Defaults to False. |
seed | int | None | Optional root seed for deterministic converter operations. Converters derive independent named child streams automatically. Initialize PyRIT before constructing components whose defaults are selected randomly. This does not control remote model output. Defaults to None. |
raise_on_initializer_error | bool | If True, raise when loading or executing an initializer fails. If False, log each failure and continue with the remaining initializers. Defaults to True. |
**memory_instance_kwargs | Any | None | Additional keyword arguments to pass to the memory instance. Azure SQL checks the schema without migrating by default. Pass skip_schema_migration=False to explicitly migrate a non-production database. SQLite retains automatic initialization. Defaults to {}. |
Raises:
TypeError— Ifenv_akv_strictis not a bool or seed is not an int or None.ValueError— If an unsupported memory_db_type is provided or env_files contains non-existent files.
Examples:
resolve_lazy_export¶
resolve_lazy_export(name: str, module_name: str, module_globals: dict[str, Any], exports: Mapping[str, LazyExport]) → AnyResolve and cache one package export.
| Parameter | Type | Description |
|---|---|---|
name | str | Public attribute requested from the package. |
module_name | str | Package name used in an AttributeError. |
module_globals | dict[str, Any] | Package globals where the resolved value is cached. |
exports | Mapping[str, LazyExport] | Public names and their implementation locations. |
Returns:
Any— The resolved public value.
Raises:
AttributeError— Ifnameis not a declared lazy export.
ConfigurationLoader¶
Bases: YamlLoadable
Loader for PyRIT configuration from YAML files.
This class loads configuration from a YAML file and provides methods to initialize PyRIT with the loaded configuration.
Attributes:
| Parameter | Type | Description |
|---|---|---|
memory_db_type | str | The type of memory database (in_memory, sqlite, azure_sql). |
initializers | list[str | dict[str, Any]] | List of initializer configurations (name + optional args). |
initialization_scripts | list[str] | None | List of paths to custom initialization scripts. None means “use defaults”, [] means “load nothing”. |
env_files | list[str] | None | List of environment file paths to load. None means auto-discover supported .env and .env.local; [] means “load nothing”. |
env_akv_ref | list[str] | None | List containing at most one Key Vault bootstrap secret URL. |
env_akv_strict | bool | Whether malformed or valueless entries in a Key Vault bootstrap document should fail initialization. |
custom_initializers_source | str | None | Local directory or Azure Blob container URI, optionally followed by a blob prefix, used to persist custom initializer Python scripts. |
scenario_presets_source | str | None | Local directory or Azure Blob container URI, optionally followed by a blob prefix, used to persist scenario presets. |
scenario_suites_source | str | None | Local directory or Azure Blob container URI, optionally followed by a blob prefix, used to persist scenario suites. |
registry_recipes_source | str | None | Local directory or Azure Blob container URI, optionally followed by a blob prefix, used to persist saved targets, converters, scorers, and techniques. |
silent | bool | Whether to suppress initialization messages. |
seed | int | None | Optional root seed for deterministic converter operations. |
operator | str | None | Name for the current operator, e.g. a team or username. |
operation | str | None | Name for the current operation. |
enable_live_reinitialization | bool | Whether administrators may replace the live single-process backend runtime from the GUI. |
target_upload_directory | str | None | Folder that targets created through the backend API may upload local files from. When unset, those targets cannot be created through the API. |
Examples:
Methods:
apply_prepared_reinitialization_async¶
apply_prepared_reinitialization_async(prepared: PreparedReinitialization) → NoneApply a preflighted replacement while preserving the current memory instance.
Raises:
RuntimeError— If no live memory instance exists.
from_dict¶
from_dict(data: dict[str, Any]) → ConfigurationLoaderCreate a ConfigurationLoader from a dictionary.
| Parameter | Type | Description |
|---|---|---|
data | dict[str, Any] | Dictionary containing configuration values. |
Returns:
ConfigurationLoader— A new ConfigurationLoader instance.
Raises:
_RemovedConfigurationOptionError— If the removedscenarioblock is present.ValueError— Ifextensionsis present but not a dict.
get_default_config_path¶
get_default_config_path() → pathlib.PathGet the default configuration file path.
Returns:
pathlib.Path— Path to the default config file in ~/.pyrit/.pyrit_conf
initialize_pyrit_async¶
initialize_pyrit_async(raise_on_initializer_error: bool = True) → NoneInitialize PyRIT with the loaded configuration.
Resolves the .pyrit_conf initializers to instances and calls the core
initialize_pyrit_async function.
| Parameter | Type | Description |
|---|---|---|
raise_on_initializer_error | bool | Whether initializer resolution, loading, validation, or execution failures should abort initialization. Defaults to True. |
Raises:
ValueError— If configuration is invalid or initializers cannot be resolved.
load_with_overrides¶
load_with_overrides(config_file: pathlib.Path | None = None, memory_db_type: str | None = None, initializers: Sequence[str | dict[str, Any]] | None = None, initialization_scripts: Sequence[str] | None = None, env_files: Sequence[str] | None = None, env_akv_ref: Sequence[str] | None = None, env_akv_strict: bool | None = None, strict: bool = False) → ConfigurationLoaderLoad configuration with optional overrides.
This factory method implements a 3-layer configuration precedence:
Default config file (~/.pyrit/.pyrit_conf) if it exists
Explicit config_file argument if provided
Individual override arguments (non-None values take precedence)
| Parameter | Type | Description |
|---|---|---|
config_file | pathlib.Path | None | Optional path to a YAML-formatted configuration file. Defaults to None. |
memory_db_type | str | None | Override for database type (in_memory, sqlite, azure_sql). Defaults to None. |
initializers | Sequence[str | dict[str, Any]] | None | Override for initializer list. Defaults to None. |
initialization_scripts | Sequence[str] | None | Override for initialization script paths. Defaults to None. |
env_files | Sequence[str] | None | Override for environment file paths. Defaults to None. |
env_akv_ref | Sequence[str] | None | Override containing at most one Azure Key Vault bootstrap secret URL. Defaults to None. |
env_akv_strict | bool | None | Override for strict Key Vault bootstrap validation. Defaults to None. |
strict | bool | Reject malformed default layers instead of ignoring them. Defaults to False. |
Returns:
ConfigurationLoader— A merged ConfigurationLoader instance.
Raises:
FileNotFoundError— If an explicitly specified config_file does not exist._RemovedConfigurationOptionError— If the removedscenarioblock is present.ValueError— If the configuration is invalid.
preflight_reinitialization_async¶
preflight_reinitialization_async(environment_values: dict[str, str]) → PreparedReinitializationValidate replacement scripts and initializer configuration without changing live PyRIT state.
Returns:
PreparedReinitialization— Immutable inputs ready for the mutation phase.
Raises:
ValueError— If an initializer is invalid, a required environment value is missing, more than oneFindingsInitializersubclass is configured, or the configured one returns invalid finding fields. Code that callsset_active_finding_fieldsdirectly isn’t checked.
resolve_env_akv_ref¶
resolve_env_akv_ref() → list[str] | NoneReturn the AKV bootstrap secret URLs, or None when not configured.
Returns:
list[str] | None— list[str] | None: The configured AKV bootstrap secret URLs, orNone.
resolve_env_files¶
resolve_env_files() → Sequence[pathlib.Path] | NoneResolve environment file paths.
Returns:
Sequence[pathlib.Path] | None— None if field is None (use defaults), empty list if field is [],Sequence[pathlib.Path] | None— or Sequence of resolved Path objects if paths are specified.
resolve_initialization_scripts¶
resolve_initialization_scripts() → Sequence[pathlib.Path] | NoneResolve initialization script paths.
Returns:
Sequence[pathlib.Path] | None— None if field is None (use defaults), empty list if field is [],Sequence[pathlib.Path] | None— or a sequence of resolved Path objects.
resolve_initializers¶
resolve_initializers(raise_on_initializer_error: bool = True, registry: InitializerRegistry | None = None) → Sequence[PyRITInitializer]Resolve initializer names to PyRITInitializer instances.
Uses the InitializerRegistry to look up initializer classes by name and instantiate them with optional arguments.
| Parameter | Type | Description |
|---|---|---|
raise_on_initializer_error | bool | Whether to raise when an initializer cannot be resolved. If False, log the failure and continue resolving the remaining initializers. Defaults to True. |
registry | InitializerRegistry | None | Isolated initializer registry to use instead of the live singleton. Defaults to None. |
Returns:
Sequence[PyRITInitializer]— Sequence of PyRITInitializer instances.
Raises:
ValueError— If an initializer name is not found in the registry.