Conversation.add_message()

Append a message with the given role to the conversation.

Usage

Source

Conversation.add_message(
    content,
    role,
    metadata=None,
)

Creates a new Message object, assigns a unique ID and timestamp, and appends it to the internal message list. This is the low-level method underlying the role-specific convenience methods (add_user_message, add_assistant_message, add_system_message).

Parameters

content: str

The text content of the message.

role: str

The role of the sender (e.g., "user", "assistant", "system").

metadata: Optional[dict[str, Any]] = None
Optional dictionary of extra data to attach to the message.

Returns

Message
The newly created Message instance.

Examples

from talk_box.conversation import Conversation

convo = Conversation()
msg = convo.add_message("Hello!", "user")
msg.role
'user'