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.common

Common utilities and helpers for PyRIT.

Functions

apply_defaults_to_method

apply_defaults_to_method(method: Callable[..., T]) → Callable[..., T]

Apply default values to a method’s parameters.

This decorator looks up default values for the method’s class and applies them to parameters that are None or not provided.

ParameterTypeDescription
methodCallable[..., T]The method to decorate (typically init).

Returns:

combine_dict

combine_dict(existing_dict: Optional[dict[str, Any]] = None, new_dict: Optional[dict[str, Any]] = None) → dict[str, Any]

Combine two dictionaries containing string keys and values into one.

ParameterTypeDescription
existing_dictOptional[dict[str, Any]]Dictionary with existing values Defaults to None.
new_dictOptional[dict[str, Any]]Dictionary with new values to be added to the existing dictionary. Note if there’s a key clash, the value in new_dict will be used. Defaults to None.

Returns:

combine_list

combine_list(list1: Union[str, list[str]], list2: Union[str, list[str]]) → list[str]

Combine two lists or strings into a single list with unique values.

ParameterTypeDescription
list1Union[str, List[str]]First list or string to combine.
list2Union[str, List[str]]Second list or string to combine.

Returns:

convert_local_image_to_data_url

convert_local_image_to_data_url(image_path: str) → str

Convert a local image file to a data URL encoded in base64.

ParameterTypeDescription
image_pathstrThe file system path to the image file.

Returns:

Raises:

display_image_response

display_image_response(response_piece: MessagePiece) → None

Display response images if running in notebook environment.

ParameterTypeDescription
response_pieceMessagePieceThe response piece to display.

download_chunk

download_chunk(url: str, headers: dict[str, str], start: int, end: int, client: httpx.AsyncClient) → bytes

Download a chunk of the file with a specified byte range.

Returns:

download_file

download_file(url: str, token: str, download_dir: Path, num_splits: int) → None

Download a file in multiple segments (splits) using byte-range requests.

download_files

download_files(urls: list[str], token: str, download_dir: Path, num_splits: int = 3, parallel_downloads: int = 4) → None

Download multiple files with parallel downloads and segmented downloading.

download_specific_files

download_specific_files(model_id: str, file_patterns: list[str] | None, token: str, cache_dir: Path) → None

Download specific files from a Hugging Face model repository. If file_patterns is None, downloads all files.

get_available_files

get_available_files(model_id: str, token: str) → list[str]

Fetch available files for a model from the Hugging Face repository.

Returns:

Raises:

get_global_default_values

get_global_default_values() → GlobalDefaultValues

Get the global default values registry.

Returns:

get_httpx_client

get_httpx_client(use_async: bool = False, debug: bool = False, httpx_client_kwargs: Optional[Any] = {}) → httpx.Client | httpx.AsyncClient

Get the httpx client for making requests.

Returns:

get_kwarg_param

get_kwarg_param(kwargs: dict[str, Any], param_name: str, expected_type: type[_T], required: bool = True, default_value: Optional[_T] = None) → Optional[_T]

Validate and extract a parameter from kwargs.

ParameterTypeDescription
kwargsDict[str, Any]The dictionary containing parameters.
param_namestrThe name of the parameter to validate.
expected_typeType[_T]The expected type of the parameter.
requiredboolWhether the parameter is required. If True, raises ValueError if missing. Defaults to True.
default_valueOptional[_T]Default value to return if the parameter is not required and not present. Defaults to None.
kwargsDict[str, Any]The dictionary containing parameters.
param_namestrThe name of the parameter to validate.
expected_typeType[_T]The expected type of the parameter.
requiredboolWhether the parameter is required. If True, raises ValueError if missing. Defaults to True.
default_valueOptional[_T]Default value to return if the parameter is not required and not present. Defaults to None.

Returns:

Raises:

get_non_required_value

get_non_required_value(env_var_name: str, passed_value: Optional[str] = None) → str

Get a non-required value from an environment variable or a passed value, preferring the passed value.

ParameterTypeDescription
env_var_namestrThe name of the environment variable to check.
passed_valuestrThe value passed to the function. Defaults to None.

Returns:

get_random_indices

get_random_indices(start: int, size: int, proportion: float) → list[int]

Generate a list of random indices based on the specified proportion of a given size. The indices are selected from the range [start, start + size).

ParameterTypeDescription
startintStarting index (inclusive). It’s the first index that could possibly be selected.
sizeintSize of the collection to select from. This is the total number of indices available. For example, if start is 0 and size is 10, the available indices are [0, 1, 2, ..., 9].
proportionfloatThe proportion of indices to select from the total size. Must be between 0 and 1. For example, if proportion is 0.5 and size is 10, 5 randomly selected indices will be returned.

Returns:

Raises:

get_required_value

get_required_value(env_var_name: str, passed_value: Any) → Any

Get a required value from an environment variable or a passed value, preferring the passed value.

If no value is found, raises a KeyError

ParameterTypeDescription
env_var_namestrThe name of the environment variable to check
passed_valueAnyThe value passed to the function. Can be a string or a callable that returns a string.

Returns:

Raises:

is_in_ipython_session

is_in_ipython_session() → bool

Determine if the code is running in an IPython session.

This may be useful if the behavior of the code should change when running in an IPython session. For example, the code may display additional information or plots when running in an IPython session.

Returns:

make_request_and_raise_if_error_async

make_request_and_raise_if_error_async(endpoint_uri: str, method: str, post_type: PostType = 'json', debug: bool = False, extra_url_parameters: Optional[dict[str, str]] = None, request_body: Optional[dict[str, object]] = None, files: Optional[dict[str, tuple[str, bytes, str]]] = None, headers: Optional[dict[str, str]] = None, httpx_client_kwargs: Optional[Any] = {}) → httpx.Response

Make a request and raise an exception if it fails.

Query parameters can be specified either:

  1. In the endpoint_uri (e.g., “https://api.com/endpoint?api-version=2024-10-21”)

  2. Via the extra_url_parameters dict

  3. Both (extra_url_parameters will be merged with URL query parameters, with extra_url_parameters taking precedence)

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

reset_default_values

reset_default_values() → None

Reset all default values in the global registry.

set_default_value

set_default_value(class_type: type[object], parameter_name: str, value: Any, include_subclasses: bool = True) → None

Set a default value for a specific class and parameter.

This is a convenience function that delegates to the global default values registry.

ParameterTypeDescription
class_typetype[object]The class type for which to set the default.
parameter_namestrThe name of the parameter to set the default for.
valueAnyThe default value to set.
include_subclassesboolWhether this default should apply to subclasses as well. Defaults to True.

verify_and_resolve_path

verify_and_resolve_path(path: Union[str, Path]) → Path

Verify that a path is valid and resolve it to an absolute path.

This utility function can be used anywhere path validation is needed, such as in scorers, converters, or other components that accept file paths.

ParameterTypeDescription
pathUnion[str, Path]A path as a string or Path object.

Returns:

Raises:

warn_if_set

warn_if_set(config: Any, unused_fields: list[str], log: Union[logging.Logger, logging.LoggerAdapter[logging.Logger]] = logger) → None

Warn about unused parameters in configurations.

This method checks if specified fields in a configuration object are set (not None and not empty for collections) and logs a warning message for each field that will be ignored by the current attack strategy.

ParameterTypeDescription
configAnyThe configuration object to check for unused fields.
unused_fieldsList[str]List of field names to check in the config object.
logUnion[logging.Logger, logging.LoggerAdapter]Logger to use for warning messages. Defaults to logger.

DefaultValueScope

Represents a scope for default values with class type, parameter name, and inheritance rules.

This class defines the scope where a default value applies, including whether it should be inherited by subclasses.

Singleton

Bases: abc.ABCMeta

A metaclass for creating singleton classes. A singleton class can only have one instance. If an instance of the class exists, it returns that instance; if not, it creates and returns a new one.

YamlLoadable

Bases: abc.ABC

Abstract base class for objects that can be loaded from YAML files.

Methods:

from_yaml_file

from_yaml_file(file: Union[Path | str]) → T

Create a new object from a YAML file.

ParameterTypeDescription
file`Union[Pathstr]`

Returns:

Raises: