pyrit.executor.attack.ConversationManager#

class ConversationManager(*, attack_identifier: Dict[str, str], prompt_normalizer: PromptNormalizer | None = None)[source]#

Bases: object

Manages conversations for attacks, handling message history, system prompts, and conversation state.

This class provides methods to: - Initialize attack context with prepended conversations - Retrieve conversation history - Set system prompts for chat targets

__init__(*, attack_identifier: Dict[str, str], prompt_normalizer: PromptNormalizer | None = None)[source]#

Initialize the conversation manager.

Parameters:
  • attack_identifier – The identifier of the attack this manager belongs to.

  • prompt_normalizer – Optional prompt normalizer for converting prompts. If not provided, a default PromptNormalizer instance will be created.

Methods

__init__(*, attack_identifier[, ...])

Initialize the conversation manager.

add_prepended_conversation_to_memory_async(*, ...)

Add prepended conversation messages to memory for a chat target.

get_conversation(conversation_id)

Retrieve a conversation by its ID.

get_last_message(*, conversation_id[, role])

Retrieve the most recent message from a conversation.

initialize_context_async(*, context, target, ...)

Initialize attack context with prepended conversation and merged labels.

set_system_prompt(*, target, ...[, labels])

Set or update the system prompt for a conversation.

async add_prepended_conversation_to_memory_async(*, prepended_conversation: List[Message], conversation_id: str, request_converters: List[PromptConverterConfiguration] | None = None, prepended_conversation_config: PrependedConversationConfig | None = None, max_turns: int | None = None) int[source]#

Add prepended conversation messages to memory for a chat target.

This is a lower-level method that handles adding messages to memory without modifying any attack context state. It can be called directly by attacks that manage their own state (like TAP nodes) or internally by initialize_context_async for standard attacks.

Messages are added with: - Duplicated message objects (preserves originals) - simulated_assistant role for assistant messages (for traceability) - Converters applied based on config

Parameters:
  • prepended_conversation – Messages to add to memory.

  • conversation_id – Conversation ID to assign to all messages.

  • request_converters – Optional converters to apply to messages.

  • prepended_conversation_config – Optional configuration for converter roles.

  • max_turns – If provided, validates that turn count doesn’t exceed this limit.

Returns:

The number of turns (assistant messages) added.

Raises:

ValueError – If max_turns is exceeded by the prepended conversation.

get_conversation(conversation_id: str) List[Message][source]#

Retrieve a conversation by its ID.

Parameters:

conversation_id – The ID of the conversation to retrieve.

Returns:

A list of messages in the conversation, ordered by creation time. Returns empty list if no messages exist.

get_last_message(*, conversation_id: str, role: Literal['system', 'user', 'assistant', 'simulated_assistant', 'tool', 'developer'] | None = None) MessagePiece | None[source]#

Retrieve the most recent message from a conversation.

Parameters:
  • conversation_id – The ID of the conversation to retrieve from.

  • role – If provided, return only the last message matching this role.

Returns:

The last message piece, or None if no messages exist.

async initialize_context_async(*, context: AttackContext[Any], target: PromptTarget, conversation_id: str, request_converters: List[PromptConverterConfiguration] | None = None, prepended_conversation_config: PrependedConversationConfig | None = None, max_turns: int | None = None, memory_labels: Dict[str, str] | None = None) ConversationState[source]#

Initialize attack context with prepended conversation and merged labels.

This is the primary method for setting up an attack context. It: 1. Merges memory_labels from attack strategy with context labels 2. Processes prepended_conversation based on target type and config 3. Updates context.executed_turns for multi-turn attacks 4. Sets context.next_message if there’s an unanswered user message

For PromptChatTarget:
  • Adds prepended messages to memory with simulated_assistant role

  • All messages get new UUIDs

For non-chat PromptTarget:
  • If config.non_chat_target_behavior=”normalize_first_turn”: normalizes conversation to string and prepends to context.next_message

  • If config.non_chat_target_behavior=”raise”: raises ValueError

Parameters:
  • context – The attack context to initialize.

  • target – The objective target for the conversation.

  • conversation_id – Unique identifier for the conversation.

  • request_converters – Converters to apply to messages.

  • prepended_conversation_config – Configuration for handling prepended conversation.

  • max_turns – Maximum turns allowed (for validation and state tracking).

  • memory_labels – Labels from the attack strategy to merge with context labels.

Returns:

ConversationState with turn_count and last_assistant_message_scores.

Raises:

ValueError – If conversation_id is empty, or if prepended_conversation requires a PromptChatTarget but target is not one.

set_system_prompt(*, target: PromptChatTarget, conversation_id: str, system_prompt: str, labels: Dict[str, str] | None = None) None[source]#

Set or update the system prompt for a conversation.

Parameters:
  • target – The chat target to set the system prompt on.

  • conversation_id – Unique identifier for the conversation.

  • system_prompt – The system prompt text.

  • labels – Optional labels to associate with the system prompt.