Messages¶
Message classes for the streamlit_rich_message_history package.
This module defines the Message class and its derivatives (UserMessage, AssistantMessage, ErrorMessage) which represent chat messages with rich content components.
- class streamlit_rich_message_history.messages.AssistantMessage(avatar)[source]¶
Bases:
MessageConvenience class for assistant messages.
This class creates a Message with the ‘assistant’ role pre-configured, making it easier to create assistant/AI responses in a chat interface.
- user¶
Always set to ‘assistant’
- avatar¶
Avatar image for the assistant
- components¶
List of MessageComponent objects in this message
- class streamlit_rich_message_history.messages.ErrorMessage(avatar, error_text)[source]¶
Bases:
MessageConvenience class for error messages.
This class creates a Message with the ‘error’ role pre-configured and automatically adds an error component, making it easier to display errors in a chat interface.
- user¶
Always set to ‘error’
- avatar¶
Avatar image for error messages
- components¶
List of MessageComponent objects in this message
- class streamlit_rich_message_history.messages.Message(user, avatar)[source]¶
Bases:
MessageBuilderMixinClass representing a message with multiple components.
This is the core class for creating rich messages with various content types. A message represents a single chat bubble/entry that can contain multiple components (text, code, charts, tables, etc.).
- user¶
The sender of the message (‘user’, ‘assistant’, etc.)
- avatar¶
Avatar image for the message sender
- components¶
List of MessageComponent objects in this message
- __init__(user, avatar)[source]¶
Initialize a new message.
- Parameters:
user (
str) – The sender of the message (‘user’, ‘assistant’, etc.)avatar (
str) – Avatar image for the message sender (URL or emoji)
- classmethod register_component_method(method_name, component_type, method_func=None)[source]¶
Register a new component method for the Message class. This method dynamically adds a new add_* method to the Message class for a custom component type. If a method with this name already exists, returns the existing method with a warning instead of raising an exception.
- Parameters:
method_name (
str) – Name of the method to add (typically ‘add_xyz’)component_type (
ComponentType) – The component type to associate with this methodmethod_func (
Optional[Callable]) – Optional custom function for the method If None, a default implementation is created
- Returns:
The created or existing method function
- Return type:
Callable
Examples
>>> IMAGE_TYPE = ComponentRegistry.register_component_type("image") >>> Message.register_component_method("add_image", IMAGE_TYPE) >>> # Now message.add_image() is available >>> # Registering the same method again will return the existing method >>> Message.register_component_method("add_image", IMAGE_TYPE) >>> # A warning will be printed and the existing method will be returned
- class streamlit_rich_message_history.messages.UserMessage(avatar, text=None)[source]¶
Bases:
MessageConvenience class for user messages.
This class creates a Message with the ‘user’ role pre-configured, making it easier to create user messages in a chat interface.
- user¶
Always set to ‘user’
- avatar¶
Avatar image for the user
- components¶
List of MessageComponent objects in this message