pyrit.message_normalizer.ChatMessageNormalizer#
- class ChatMessageNormalizer(*, use_developer_role: bool = False, system_message_behavior: Literal['keep', 'squash', 'ignore'] = 'keep')[source]#
Bases:
MessageListNormalizer[ChatMessage],MessageStringNormalizerNormalizer that converts a list of Messages to a list of ChatMessages.
This normalizer handles both single-part and multipart messages: - Single piece messages: content is a simple string - Multiple piece messages: content is a list of dicts with type/text or type/image_url
- Parameters:
use_developer_role – If True, translates “system” role to “developer” role for compatibility with newer OpenAI models (o1, o3, gpt-4.1+). Defaults to False for backward compatibility.
system_message_behavior – How to handle system messages before conversion. - “keep”: Keep system messages as-is (default) - “squash”: Merge system message into first user message - “ignore”: Drop system messages entirely
- __init__(*, use_developer_role: bool = False, system_message_behavior: Literal['keep', 'squash', 'ignore'] = 'keep') None[source]#
Initialize the ChatMessageNormalizer.
- Parameters:
use_developer_role – If True, translates “system” role to “developer” role.
system_message_behavior – How to handle system messages. Defaults to “keep”.
Methods
__init__(*[, use_developer_role, ...])Initialize the ChatMessageNormalizer.
normalize_async(messages)Convert a list of Messages to a list of ChatMessages.
normalize_string_async(messages)Convert a list of Messages to a JSON string representation.
normalize_to_dicts_async(messages)Normalize the list of messages into a list of dictionaries.
- async normalize_async(messages: List[Message]) List[ChatMessage][source]#
Convert a list of Messages to a list of ChatMessages.
For single-piece text messages, content is a string. For multi-piece or non-text messages, content is a list of content dicts.
- Parameters:
messages – The list of Message objects to normalize.
- Returns:
A list of ChatMessage objects.
- Raises:
ValueError – If the messages list is empty.
- async normalize_string_async(messages: List[Message]) str[source]#
Convert a list of Messages to a JSON string representation.
This serializes the list of ChatMessages to JSON format.
- Parameters:
messages – The list of Message objects to normalize.
- Returns:
A JSON string representation of the ChatMessages.