Toggle Menu
Prev Up Next

# `langchain_community.chat_message_histories.dynamodb`.DynamoDBChatMessageHistory

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

  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.

    - boto3_session (Optional[Session]) –

    - ttl_key_name (str) –

  Attributes

  `messages` | Retrieve the messages from DynamoDB

  Methods

  `__init__`(table_name, session_id[, ...]) |
  `aadd_messages`(messages) | Add a list of messages.
  `aclear`() | 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: Optional[str] = None, primary_key_name: str = 'SessionId', key: Optional[Dict[str, str]] = None, boto3_session: Optional[Session] = None, kms_key_id: Optional[str] = None, ttl: Optional[int] = None, ttl_key_name: str = 'expireAt', history_size: Optional[int] = None)
    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]) –

  async aadd_messages(messages: Sequence[BaseMessage]) → None

    Add a list of messages.

    Parameters

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

    Return type

      None

  async aclear() → None

    Remove all messages from the store

    Return type

      None

  add_ai_message(message: Union[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 (Union[AIMessage, str]) – The AI message to add.

    Return type

      None

  add_message(message: BaseMessage) → None

    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 list of BaseMessage objects to store.

    Return type

      None

  add_user_message(message: Union[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 (Union[HumanMessage, str]) – The human message to add

    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

    Clear session memory from DynamoDB

    Return type

      None

# Examples using DynamoDBChatMessageHistory

- AWS

- os.environ[“LANGCHAIN_TRACING_V2”] = “true”