Attacks API

The attacks module provides the framework for creating and applying adversarial attacks to prompts.

Base Classes

BaseAttack

class hivetracered.attacks.base_attack.BaseAttack[source]

Bases: ABC

Abstract base class for all attack implementations. Defines the standard interface for applying attacks to prompts in both synchronous and asynchronous contexts.

DETERMINISTIC = False
__or__(other)[source]

Allows using the | operator to compose attacks: attack1 | attack2 means attack1(attack2(prompt)).

Parameters:

other – Another BaseAttack instance to compose with this one

Returns:

A ComposedAttack instance that applies attacks in sequence

abstractmethod apply(prompt)[source]

Apply the attack to the given prompt.

Parameters:

prompt (str | list[dict[str, str]]) – A string or list of messages to apply the attack to

Return type:

str | list[dict[str, str]]

Returns:

The transformed prompt with the attack applied

abstractmethod get_description()[source]

Get the description of the attack.

Return type:

str

Returns:

A description of what the attack does

abstractmethod get_name()[source]

Get the name of the attack.

Return type:

str

Returns:

The name of the attack

get_params()[source]

Get the parameters of the attack.

Returns:

A dictionary containing the attack’s parameters

abstractmethod async stream_abatch(prompts)[source]

Apply the attack asynchronously to a batch of prompts.

Parameters:

prompts (list[str | list[dict[str, str]]]) – A list of prompts to apply the attack to

Return type:

AsyncGenerator[list[str | list[dict[str, str]]], None]

Returns:

An async generator yielding transformed prompts as they are processed

TemplateAttack

class hivetracered.attacks.template_attack.TemplateAttack(template='{prompt}', name=None, description=None)[source]

Bases: BaseAttack

A base class for template-based attacks. Allows creating new attacks by defining a template string with a ‘{prompt}’ placeholder where the original prompt will be inserted.

DETERMINISTIC = True
__init__(template='{prompt}', name=None, description=None)[source]

Initialize the template attack with a specific template string.

Parameters:
  • template (str) – A format string with a ‘{prompt}’ placeholder

  • name (str | None) – Optional name for the attack (defaults to class name)

  • description (str | None) – Optional description for the attack

apply(prompt)[source]

Apply the template attack to the given prompt.

Parameters:

prompt (str | list[dict[str, str]]) – A string or list of messages to apply the attack to. If the prompt is a list, the template will be applied to the last message.

Return type:

str | list[dict[str, str]]

Returns:

The transformed prompt with the template applied

Raises:

ValueError – If the prompt is invalid or the last message is not a human message

get_description()[source]

Get the description of the attack.

Return type:

str

Returns:

The custom description if provided, otherwise a default description based on the template

get_name()[source]

Get the name of the attack.

Return type:

str

Returns:

The custom name if provided, otherwise the class name

get_params()[source]

Get the parameters of the attack.

Returns:

A dictionary containing the attack’s parameters

async stream_abatch(prompts)[source]

Apply the template attack to a batch of prompts asynchronously.

Parameters:

prompts (list[str | list[dict[str, str]]]) – A list of prompts to apply the attack to

Return type:

AsyncGenerator[list[str | list[dict[str, str]]], None]

Returns:

An async generator yielding transformed prompts

AlgoAttack

ModelAttack

IterativeAttack

ComposedAttack

Single-Turn Attack Types

Iterative Attacks

Iterative attacks (PAIR, TAP) that optimise a single attack prompt across an internal refinement loop:

Roleplay Attacks

Persuasion Attacks

Token Smuggling Attacks

Context Switching Attacks

In-Context Learning Attacks

Task Deflection Attacks

Text Structure Modification Attacks

Output Formatting Attacks

Irrelevant Information Attacks

Simple Instructions Attacks

Multi-Turn Attack Types

Conversational Attacks

Conversational attacks (Crescendo) that drive a multi-turn dialogue with the target across many rounds within a single invocation:

See Also