BaseChatMessageHistory#

class langchain_core.chat_history.BaseChatMessageHistory[source]#

Abstract base class for storing chat message history.

Implementations guidelines:

Implementations are expected to over-ride all or some of the following methods:

  • add_messages: sync variant for bulk addition of messages

  • aadd_messages: async variant for bulk addition of messages

  • messages: sync variant for getting messages

  • aget_messages: async variant for getting messages

  • clear: sync variant for clearing messages

  • aclear: async variant for clearing messages

add_messages contains a default implementation that calls add_message for each message in the sequence. This is provided for backwards compatibility with existing implementations which only had add_message.

Async variants all have default implementations that call the sync variants. Implementers can choose to over-ride the async implementations to provide truly async implementations.

Usage guidelines:

When used for updating history, users should favor usage of add_messages over add_message or other variants like add_user_message and add_ai_message to avoid unnecessary round-trips to the underlying persistence layer.

Example: Shows a default implementation.

class FileChatMessageHistory(BaseChatMessageHistory):
    storage_path:  str
    session_id: str

   @property
   def messages(self):
       with open(os.path.join(storage_path, session_id), 'r:utf-8') as f:
           messages = json.loads(f.read())
        return messages_from_dict(messages)

   def add_messages(self, messages: Sequence[BaseMessage]) -> None:
       all_messages = list(self.messages) # Existing messages
       all_messages.extend(messages) # Add new messages

       serialized = [message_to_dict(message) for message in all_messages]
       # Can be further optimized by only writing new messages
       # using append mode.
       with open(os.path.join(storage_path, session_id), 'w') as f:
           json.dump(f, messages)

   def clear(self):
       with open(os.path.join(storage_path, session_id), 'w') as f:
           f.write("[]")

Attributes

Methods

__init__()

aadd_messages(messages)

Async add a list of messages.

aclear()

Async remove all messages from the store

add_ai_message(message)

Convenience method for adding an AI message string to the store.

add_message(message)

Add a Message object to the store.

add_messages(messages)

Add a list of messages.

add_user_message(message)

Convenience method for adding a human message string to the store.

aget_messages()

Async version of getting messages.

clear()

Remove all messages from the store

__init__()#
async aadd_messages(messages: Sequence[BaseMessage]) None[source]#

Async add a list of messages.

Parameters:

messages (Sequence[BaseMessage]) – A sequence of BaseMessage objects to store.

Return type:

None

async aclear() None[source]#

Async remove all messages from the store

Return type:

None

add_ai_message(message: AIMessage | str) None[source]#

Convenience method for adding an AI message string to the store.

Please note that this is a convenience method. Code should favor the bulk add_messages interface instead to save on round-trips to the underlying persistence layer.

This method may be deprecated in a future release.

Parameters:

message (AIMessage | str) – The AI message to add.

Return type:

None

add_message(message: BaseMessage) None[source]#

Add a Message object to the store.

Parameters:

message (BaseMessage) – A BaseMessage object to store.

Raises:

NotImplementedError – If the sub-class has not implemented an efficient add_messages method.

Return type:

None

add_messages(messages: Sequence[BaseMessage]) None[source]#

Add a list of messages.

Implementations should over-ride this method to handle bulk addition of messages in an efficient manner to avoid unnecessary round-trips to the underlying store.

Parameters:

messages (Sequence[BaseMessage]) – A sequence of BaseMessage objects to store.

Return type:

None

add_user_message(message: HumanMessage | str) None[source]#

Convenience method for adding a human message string to the store.

Please note that this is a convenience method. Code should favor the bulk add_messages interface instead to save on round-trips to the underlying persistence layer.

This method may be deprecated in a future release.

Parameters:

message (HumanMessage | str) – The human message to add to the store.

Return type:

None

async aget_messages() List[BaseMessage][source]#

Async version of getting messages.

Can over-ride this method to provide an efficient async implementation.

In general, fetching messages may involve IO to the underlying persistence layer.

Return type:

List[BaseMessage]

abstract clear() None[source]#

Remove all messages from the store

Return type:

None

Examples using BaseChatMessageHistory