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: Message

Convenience 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

__init__(avatar)[source]

Initialize a new assistant message.

Parameters:

avatar (str) – Avatar image for the assistant (URL or emoji)

class streamlit_rich_message_history.messages.ErrorMessage(avatar, error_text)[source]

Bases: Message

Convenience 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

__init__(avatar, error_text)[source]

Initialize a new error message.

Parameters:
  • avatar (str) – Avatar image for the error message (URL or emoji)

  • error_text (str) – The error message to display

class streamlit_rich_message_history.messages.Message(user, avatar)[source]

Bases: MessageBuilderMixin

Class 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 method

  • method_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
render()[source]

Render the message with all its components.

This method displays the message in a Streamlit app using st.chat_message and renders all components within it.

Raises:

Displays an error message in the UI if rendering fails

class streamlit_rich_message_history.messages.UserMessage(avatar, text=None)[source]

Bases: Message

Convenience 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

__init__(avatar, text=None)[source]

Initialize a new user message.

Parameters:
  • avatar (str) – Avatar image for the user (URL or emoji)

  • text (Optional[str]) – Optional initial text for the message. If provided, adds a text component automatically.