pyrit.models.Message#
- class Message(message_pieces: Sequence[MessagePiece], *, skip_validation: bool | None = False)[source]#
Bases:
objectRepresents a message in a conversation, for example a prompt or a response to a prompt.
This is a single request to a target. It can contain multiple message pieces.
- Parameters:
message_pieces (Sequence[MessagePiece]) – The list of message pieces.
Methods
__init__(message_pieces, *[, skip_validation])Create a deep copy of this message with new IDs and timestamp for all message pieces.
flatten_to_message_pieces(messages)from_prompt(*, prompt, role[, prompt_metadata])from_system_prompt(system_prompt)get_all_values(messages)Return all converted values across the provided messages.
get_piece([n])Return the nth message piece.
get_value([n])Return the converted value of the nth message piece.
Return the converted values of all message pieces.
is_error()Returns True if any of the message pieces have an error response.
Set that the prompt is not in the database.
to_dict()Convert the message to a dictionary representation.
validate()Validates the request response.
Attributes
Return the API-compatible role of the first message piece.
Return the conversation ID of the first request piece (they should all be the same).
Check if this is a simulated assistant response.
Use api_role for comparisons or _role for internal storage.
Return the sequence of the first request piece (they should all be the same).
- property api_role: Literal['system', 'user', 'assistant', 'simulated_assistant', 'tool', 'developer']#
Return the API-compatible role of the first message piece.
Maps simulated_assistant to assistant for API compatibility. All message pieces in a Message should have the same role.
- property conversation_id: str#
Return the conversation ID of the first request piece (they should all be the same).
- duplicate_message() Message[source]#
Create a deep copy of this message with new IDs and timestamp for all message pieces.
This is useful when you need to reuse a message template but want fresh IDs to avoid database conflicts (e.g., during retry attempts).
The original_prompt_id is intentionally kept the same to track the origin. Generates a new timestamp to reflect when the duplicate is created.
- Returns:
A new Message with deep-copied message pieces, new IDs, and fresh timestamp.
- Return type:
- static flatten_to_message_pieces(messages: Sequence[Message]) MutableSequence[MessagePiece][source]#
- classmethod from_prompt(*, prompt: str, role: Literal['system', 'user', 'assistant', 'simulated_assistant', 'tool', 'developer'], prompt_metadata: Dict[str, str | int] | None = None) Message[source]#
- static get_all_values(messages: Sequence[Message]) list[str][source]#
Return all converted values across the provided messages.
- get_piece(n: int = 0) MessagePiece[source]#
Return the nth message piece.
- property is_simulated: bool#
Check if this is a simulated assistant response.
Simulated responses come from prepended conversations or generated simulated conversations, not from actual target responses.
- property role: Literal['system', 'user', 'assistant', 'simulated_assistant', 'tool', 'developer']#
Use api_role for comparisons or _role for internal storage.
This property is deprecated and will be removed in a future version. Returns api_role for backward compatibility.
- Type:
Deprecated
- property sequence: int#
Return the sequence of the first request piece (they should all be the same).
- set_response_not_in_database()[source]#
Set that the prompt is not in the database.
This is needed when we’re scoring prompts or other things that have not been sent by PyRIT