Message History

class streamlit_rich_message_history.history.MessageHistory[source]

Bases: object

Class to store and manage a history of messages in a Streamlit application.

The MessageHistory class provides a convenient way to maintain a conversation-like interface in Streamlit apps, with support for different types of messages (user, assistant, error) and rich content components.

It manages the addition, storage, and rendering of messages, as well as the registration of custom component types, detectors, and renderers.

messages

A list of Message objects that comprise the conversation history

__init__()[source]

Initialize an empty message history.

add_assistant_message(message)[source]

Add a pre-created assistant message to the history.

Parameters:

message (AssistantMessage) – The AssistantMessage object to add to the history

Return type:

None

add_assistant_message_create(avatar)[source]

Create and add a new empty assistant message.

This creates an assistant message that can be populated with components after creation.

Parameters:

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

Returns:

The created assistant message

Return type:

AssistantMessage

add_error_message(avatar, error_text)[source]

Create and add an error message to the history.

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

  • error_text (str) – The error text to display

Returns:

The created error message

Return type:

ErrorMessage

add_message(message)[source]

Add a message to the history.

Parameters:

message (Message) – The Message object to add to the history

Returns:

The added message, allowing for method chaining

Return type:

Message

add_user_message(message)[source]

Add a pre-created user message to the history.

Parameters:

message (UserMessage) – The UserMessage object to add to the history

Return type:

None

add_user_message_create(avatar, text)[source]

Create and add a new user message with text.

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

  • text (str) – The text content of the user message

Returns:

The created user message

Return type:

UserMessage

clear()[source]

Clear all messages from the history, resetting it to empty.

static register_component_detector(component_type, detector)[source]

Register a detector function for a component type.

The detector function determines whether a given content should be treated as the specified component type.

Parameters:
  • component_type (ComponentType) – The component type to register a detector for

  • detector (Callable[[Any, dict], bool]) – A function that takes (content, kwargs) and returns True if the content should be treated as this component type

Return type:

None

static register_component_method(method_name, component_type, method_func=None)[source]

Register a new component method to the Message class.

This allows adding custom methods to Message objects for adding specific types of components with a convenient API.

Parameters:
  • method_name (str) – The name of the method to add (e.g., ‘add_chart’)

  • component_type (ComponentType) – The component type this method will create

  • method_func (Optional[Callable]) – Optional custom function to use for the method (if None, a default implementation will be used)

Return type:

None

static register_component_renderer(component_type, renderer)[source]

Register a renderer function for a component type.

The renderer function handles displaying the component in the Streamlit UI.

Parameters:
  • component_type (ComponentType) – The component type to register a renderer for

  • renderer (Callable[[Any, dict], None]) – A function that takes (content, kwargs) and renders the component in the Streamlit app

Return type:

None

static register_component_type(name)[source]

Register a new component type for use in messages.

This allows extending the library with custom component types that can be detected and rendered appropriately.

Parameters:

name (str) – The name of the new component type

Returns:

The created component type enum value

Return type:

ComponentType

render_all()[source]

Render all messages in the history to the Streamlit UI.

This renders each message in sequence, from first to last.

render_last(n=1)[source]

Render only the last n messages in the history.

Parameters:

n (int) – Number of most recent messages to render (default: 1)