Skip to content

Threads

In LangMem, a Thread represents a conversation between users and your chatbot. Each Thread consists of:

  • id: A unique identifier for the thread
  • messages: The list of messages exchanged in the conversation
  • metadata (optional): Additional data about the thread

Messages

Messages can follow OpenAI's format, with additional metadata for LangMem to instruct it on how to persist memories.

  • role: The role of the message sender (e.g., "user", "assistant")
  • content: The text content of the message
  • name (optional): The name of the user, if available
  • metadata (optional): Additional data about the message or user. The metadata can include:
    • an optional user_id field that contains a user you wish to retain memories about
    • an optional timestamp field that adds in deduplication if you wish to post more messages to the service.

Bases: TypedDict

OpenAI Message.

Represents an OpenAI message, with optional user metadata to direct LangMem to process memories for the user.

Attributes:

Name Type Description
content str | List[dict]

The content of the message.

role str

The role of the message sender.

name Optional[str]

The name of the message sender.

metadata Optional[Union[Metadata, Dict[str, Any]]]

Additional metadata for the message.

Source code in langmem/schemas.py
class OpenAIMessage(TypedDict, total=False):
    """OpenAI Message.

    Represents an OpenAI message, with optional user metadata to direct
    LangMem to process memories for the user.

    Attributes:
        content (str | List[dict]): The content of the message.
        role (str): The role of the message sender.
        name (Optional[str]): The name of the message sender.
        metadata (Optional[Union[Metadata, Dict[str, Any]]]): Additional metadata for the message.
    """

    content: str | List[dict]
    role: str
    name: Optional[str]
    metadata: Optional[Union[Metadata, Dict[str, Any]]]

Bases: TypedDict

LangMem Message.

Represents a message.

Attributes:

Name Type Description
id UUID

The unique identifier of the message.

content str | List[Dict[str, Any]]

The content of the message.

timestamp datetime

The timestamp of the message.

user UserForMessage

The user associated with the message.

Source code in langmem/schemas.py
class Message(TypedDict, total=False):
    """LangMem Message.

    Represents a message.

    Attributes:
        id (uuid.UUID): The unique identifier of the message.
        content (str | List[Dict[str, Any]]): The content of the message.
        timestamp (datetime): The timestamp of the message.
        user (UserForMessage): The user associated with the message.
    """

    id: uuid.UUID
    content: str | List[Dict[str, Any]]
    timestamp: datetime
    user: UserForMessage

Represents a message-like object, which can be either an OpenAIMessage or a Message.

Creating Threads

The easiest way to create a new thread is implicitly via the add_messages() method in the client. You can also explicitly create one using the create_thread() method.

Processing memories

LangMem processes messages and associated memories asynchronously, so there's no need to wait for a response before sending additional messages or continuing your chat bot's conversation.

As messages are added to a thread, LangMem periodically extracts and stores relevant memories based on your configured memory types (User State, Semantic Memory, Append-only State). You can eagerly trigger the formation of memories using the trigger_all_for_thread() method.

Whichever user_id's are present in the messages will have their memories updated.

If you have configured any thread_summary memory types, these will be processed as well.