pyrit.prompt_converter.SelectiveTextConverter#

class SelectiveTextConverter(*, converter: PromptConverter, selection_strategy: TextSelectionStrategy, preserve_tokens: bool = False, start_token: str = '⟪', end_token: str = '⟫', word_separator: str = ' ')[source]#

Bases: PromptConverter

A wrapper converter that applies another converter to selected portions of text.

This converter supports multiple selection strategies: - Character-level: Selects a contiguous character range (e.g., IndexSelectionStrategy, RegexSelectionStrategy) - Word-level: Selects specific words (e.g., WordIndexSelectionStrategy, WordPositionSelectionStrategy) - Token-based: Auto-detects and converts text between ⟪⟫ tokens (TokenSelectionStrategy)

Most use cases will use word-level strategies for more intuitive selection.

Example

>>> from pyrit.prompt_converter import Base64Converter, SelectiveTextConverter
>>> from pyrit.prompt_converter.text_selection_strategy import WordRegexSelectionStrategy
>>>
>>> # Convert only words matching a pattern
>>> strategy = WordRegexSelectionStrategy(pattern=r"\d+")
>>> converter = SelectiveTextConverter(
...     converter=Base64Converter(),
...     selection_strategy=strategy,
...     preserve_tokens=True
... )
>>> result = await converter.convert_async(
...     prompt="The code is 12345 here"
... )
>>> # Result: "The code is ⟪MTIzNDU=⟫ here"
__init__(*, converter: PromptConverter, selection_strategy: TextSelectionStrategy, preserve_tokens: bool = False, start_token: str = '⟪', end_token: str = '⟫', word_separator: str = ' ') None[source]#

Initializes the selective text converter.

Parameters:
  • converter (PromptConverter) – The converter to apply to the selected text.

  • selection_strategy (TextSelectionStrategy) – The strategy for selecting which text to convert. Can be character-level or word-level strategy.

  • preserve_tokens (bool) – If True, wraps converted text with start/end tokens. This allows subsequent converters in a chain to target different regions. Defaults to False.

  • start_token (str) – The token to place before converted text when preserve_tokens=True. Defaults to “⟪”.

  • end_token (str) – The token to place after converted text when preserve_tokens=True. Defaults to “⟫”.

  • word_separator (str) – The separator to use when working with word-level strategies. Defaults to “ “.

Raises:
  • ValueError – If the wrapped converter does not support text input/output.

  • ValueError – If a word-level selection_strategy is used with a WordLevelConverter that has a non-default word_selection_strategy. When SelectiveTextConverter uses a WordSelectionStrategy, it passes individual words to the wrapped converter, making the wrapped converter’s word selection strategy meaningless.

Methods

__init__(*, converter, selection_strategy[, ...])

Initializes the selective text converter.

convert_async(*, prompt[, input_type])

Converts selected portions of the prompt using the wrapped converter.

convert_tokens_async(*, prompt[, ...])

Converts substrings within a prompt that are enclosed by specified start and end tokens.

get_identifier()

Returns an identifier dictionary for the converter.

input_supported(input_type)

Checks if the input type is supported.

output_supported(output_type)

Checks if the output type is supported.

Attributes

supported_input_types

Returns a list of supported input types for the converter.

supported_output_types

Returns a list of supported output types for the converter.

async convert_async(*, prompt: str, input_type: Literal['text', 'image_path', 'audio_path', 'video_path', 'url', 'reasoning', 'error', 'function_call', 'tool_call', 'function_call_output'] = 'text') ConverterResult[source]#

Converts selected portions of the prompt using the wrapped converter.

Parameters:
  • prompt (str) – The prompt to be converted.

  • input_type (PromptDataType) – The type of input data. Must be “text”.

Returns:

The result containing the converted output and its type.

Return type:

ConverterResult

Raises:

ValueError – If the input type is not “text”.

input_supported(input_type: Literal['text', 'image_path', 'audio_path', 'video_path', 'url', 'reasoning', 'error', 'function_call', 'tool_call', 'function_call_output']) bool[source]#

Checks if the input type is supported.

Parameters:

input_type (PromptDataType) – The input type to check.

Returns:

True if the input type is “text”, False otherwise.

Return type:

bool

output_supported(output_type: Literal['text', 'image_path', 'audio_path', 'video_path', 'url', 'reasoning', 'error', 'function_call', 'tool_call', 'function_call_output']) bool[source]#

Checks if the output type is supported.

Parameters:

output_type (PromptDataType) – The output type to check.

Returns:

True if the output type is “text”, False otherwise.

Return type:

bool