DynamoDBChatMessageHistory#

class langchain_community.chat_message_histories.dynamodb.DynamoDBChatMessageHistory(table_name: str, session_id: str, endpoint_url: str | None = None, primary_key_name: str = 'SessionId', key: Dict[str, str] | None = None, boto3_session: Session | None = None, kms_key_id: str | None = None, ttl: int | None = None, ttl_key_name: str = 'expireAt', history_size: int | None = None, history_messages_key: str | None = 'History')[source]#

Chat message history that stores history in AWS DynamoDB.

This class expects that a DynamoDB table exists with name table_name

Parameters:
  • table_name (str) – name of the DynamoDB table

  • session_id (str) – arbitrary key that is used to store the messages of a single chat session.

  • endpoint_url (Optional[str]) – URL of the AWS endpoint to connect to. This argument is optional and useful for test purposes, like using Localstack. If you plan to use AWS cloud service, you normally don’t have to worry about setting the endpoint_url.

  • primary_key_name (str) – name of the primary key of the DynamoDB table. This argument is optional, defaulting to β€œSessionId”.

  • key (Optional[Dict[str, str]]) – an optional dictionary with a custom primary and secondary key. This argument is optional, but useful when using composite dynamodb keys, or isolating records based off of application details such as a user id. This may also contain global and local secondary index keys.

  • kms_key_id (Optional[str]) – an optional AWS KMS Key ID, AWS KMS Key ARN, or AWS KMS Alias for client-side encryption

  • ttl (Optional[int]) – Optional Time-to-live (TTL) in seconds. Allows you to define a per-item expiration timestamp that indicates when an item can be deleted from the table. DynamoDB handles deletion of expired items without consuming write throughput. To enable this feature on the table, follow the [AWS DynamoDB documentation](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/time-to-live-ttl-how-to.html)

  • history_size (Optional[int]) – Maximum number of messages to store. If None then there is no limit. If not None then only the latest history_size messages are stored.

  • history_messages_key (Optional[str]) – Key for the chat history where the messages are stored and updated

  • boto3_session (Optional[Session]) –

  • ttl_key_name (str) –

Attributes

messages

Retrieve the messages from DynamoDB

Methods

__init__(table_name,Β session_id[,Β ...])

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)

Append the message to the record in DynamoDB

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()

Clear session memory from DynamoDB

__init__(table_name: str, session_id: str, endpoint_url: str | None = None, primary_key_name: str = 'SessionId', key: Dict[str, str] | None = None, boto3_session: Session | None = None, kms_key_id: str | None = None, ttl: int | None = None, ttl_key_name: str = 'expireAt', history_size: int | None = None, history_messages_key: str | None = 'History')[source]#
Parameters:
  • table_name (str) –

  • session_id (str) –

  • endpoint_url (Optional[str]) –

  • primary_key_name (str) –

  • key (Optional[Dict[str, str]]) –

  • boto3_session (Optional[Session]) –

  • kms_key_id (Optional[str]) –

  • ttl (Optional[int]) –

  • ttl_key_name (str) –

  • history_size (Optional[int]) –

  • history_messages_key (Optional[str]) –

async aadd_messages(messages: Sequence[BaseMessage]) β†’ None#

Async add a list of messages.

Parameters:

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

Return type:

None

async aclear() β†’ None#

Async remove all messages from the store

Return type:

None

add_ai_message(message: AIMessage | str) β†’ None#

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]#

Append the message to the record in DynamoDB

Parameters:

message (BaseMessage) –

Return type:

None

add_messages(messages: Sequence[BaseMessage]) β†’ None#

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#

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]#

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]

clear() β†’ None[source]#

Clear session memory from DynamoDB

Return type:

None

Examples using DynamoDBChatMessageHistory