{
  "id": "message_history",
  "title": "LLM Message History",
  "url": "https://redis.io/docs/latest/develop/ai/redisvl/0.10.0/api/message_history/",
  "summary": "",
  "content": "\n\n## SemanticMessageHistory\n\n\u003ca id=\"semantic-message-history-api\"\u003e\u003c/a\u003e\n\n### `class SemanticMessageHistory(name, session_tag=None, prefix=None, vectorizer=None, distance_threshold=0.3, redis_client=None, redis_url='redis://localhost:6379', connection_kwargs={}, overwrite=False, **kwargs)`\n\nBases: `BaseMessageHistory`\n\nInitialize message history with index\n\nSemantic Message History stores the current and previous user text prompts\nand LLM responses to allow for enriching future prompts with session\ncontext. Message history is stored in individual user or LLM prompts and\nresponses.\n\n* **Parameters:**\n  * **name** (*str*) – The name of the message history index.\n  * **session_tag** (*Optional* *[* *str* *]*) – Tag to be added to entries to link to a specific\n    conversation session. Defaults to instance ULID.\n  * **prefix** (*Optional* *[* *str* *]*) – Prefix for the keys for this message data.\n    Defaults to None and will be replaced with the index name.\n  * **vectorizer** (*Optional* *[* *BaseVectorizer* *]*) – The vectorizer used to create embeddings.\n  * **distance_threshold** (*float*) – The maximum semantic distance to be\n    included in the context. Defaults to 0.3.\n  * **redis_client** (*Optional* *[* *Redis* *]*) – A Redis client instance. Defaults to\n    None.\n  * **redis_url** (*str* *,* *optional*) – The redis url. Defaults to redis://localhost:6379.\n  * **connection_kwargs** (*Dict* *[* *str* *,* *Any* *]*) – The connection arguments\n    for the redis client. Defaults to empty {}.\n  * **overwrite** (*bool*) – Whether or not to force overwrite the schema for\n    the semantic message index. Defaults to false.\n\nThe proposed schema will support a single vector embedding constructed\nfrom either the prompt or response in a single string.\n\n#### `add_message(message, session_tag=None)`\n\nInsert a single prompt or response into the message history.\nA timestamp is associated with it so that it can be later sorted\nin sequential ordering after retrieval.\n\n* **Parameters:**\n  * **message** (*Dict* *[* *str* *,**str* *]*) – The user prompt or LLM response.\n  * **session_tag** (*Optional* *[* *str* *]*) – Tag to be added to entry to link to a specific\n    conversation session. Defaults to instance ULID.\n* **Return type:**\n  None\n\n#### `add_messages(messages, session_tag=None)`\n\nInsert a list of prompts and responses into the session memory.\nA timestamp is associated with each so that they can be later sorted\nin sequential ordering after retrieval.\n\n* **Parameters:**\n  * **messages** (*List* *[* *Dict* *[* *str* *,* *str* *]* *]*) – The list of user prompts and LLM responses.\n  * **session_tag** (*Optional* *[* *str* *]*) – Tag to be added to entries to link to a specific\n    conversation session. Defaults to instance ULID.\n* **Return type:**\n  None\n\n#### `clear()`\n\nClears the message history.\n\n* **Return type:**\n  None\n\n#### `delete()`\n\nClear all message keys and remove the search index.\n\n* **Return type:**\n  None\n\n#### `drop(id=None)`\n\nRemove a specific exchange from the message history.\n\n* **Parameters:**\n  **id** (*Optional* *[* *str* *]*) – The id of the message entry to delete.\n  If None then the last entry is deleted.\n* **Return type:**\n  None\n\n#### `get_recent(top_k=5, as_text=False, raw=False, session_tag=None, role=None)`\n\nRetrieve the recent message history in sequential order.\n\n* **Parameters:**\n  * **top_k** (*int*) – The number of previous exchanges to return. Default is 5.\n  * **as_text** (*bool*) – Whether to return the conversation as a single string,\n    or list of alternating prompts and responses.\n  * **raw** (*bool*) – Whether to return the full Redis hash entry or just the\n    prompt and response\n  * **session_tag** (*Optional* *[* *str* *]*) – Tag of the entries linked to a specific\n    conversation session. Defaults to instance ULID.\n  * **role** (*Optional* *[* *Union* *[* *str* *,* *List* *[* *str* *]* *]* *]*) – Filter messages by role(s).\n    Can be a single role string (\"system\", \"user\", \"llm\", \"tool\") or\n    a list of roles. If None, all roles are returned.\n* **Returns:**\n  A single string transcription of the session\n  : or list of strings if as_text is false.\n* **Return type:**\n  Union[str, List[str]]\n* **Raises:**\n  **ValueError** – if top_k is not an integer greater than or equal to 0,\n      or if role contains invalid values.\n\n#### `get_relevant(prompt, as_text=False, top_k=5, fall_back=False, session_tag=None, raw=False, distance_threshold=None, role=None)`\n\nSearches the message history for information semantically related to\nthe specified prompt.\n\nThis method uses vector similarity search with a text prompt as input.\nIt checks for semantically similar prompts and responses and gets\nthe top k most relevant previous prompts or responses to include as\ncontext to the next LLM call.\n\n* **Parameters:**\n  * **prompt** (*str*) – The message text to search for in message history\n  * **as_text** (*bool*) – Whether to return the prompts and responses as text\n  * **JSON.** (*or as*)\n  * **top_k** (*int*) – The number of previous messages to return. Default is 5.\n  * **session_tag** (*Optional* *[* *str* *]*) – Tag of the entries linked to a specific\n    conversation session. Defaults to instance ULID.\n  * **distance_threshold** (*Optional* *[* *float* *]*) – The threshold for semantic\n    vector distance.\n  * **fall_back** (*bool*) – Whether to drop back to recent conversation history\n    if no relevant context is found.\n  * **raw** (*bool*) – Whether to return the full Redis hash entry or just the\n    message.\n  * **role** (*Optional* *[* *Union* *[* *str* *,* *List* *[* *str* *]* *]* *]*) – Filter messages by role(s).\n    Can be a single role string (\"system\", \"user\", \"llm\", \"tool\") or\n    a list of roles. If None, all roles are returned.\n* **Returns:**\n  Either a list of strings, or a\n  list of prompts and responses in JSON containing the most relevant.\n* **Return type:**\n  Union[List[str], List[Dict[str,str]]\n\nRaises ValueError: if top_k is not an integer greater or equal to 0,\n: or if role contains invalid values.\n\n#### `store(prompt, response, session_tag=None)`\n\nInsert a prompt:response pair into the message history. A timestamp\nis associated with each message so that they can be later sorted\nin sequential ordering after retrieval.\n\n* **Parameters:**\n  * **prompt** (*str*) – The user prompt to the LLM.\n  * **response** (*str*) – The corresponding LLM response.\n  * **session_tag** (*Optional* *[* *str* *]*) – Tag to be added to entries to link to a specific\n    conversation session. Defaults to instance ULID.\n* **Return type:**\n  None\n\n#### `property messages: List[str] | List[Dict[str, str]]`\n\nReturns the full message history.\n\n## MessageHistory\n\n\u003ca id=\"message-history-api\"\u003e\u003c/a\u003e\n\n### `class MessageHistory(name, session_tag=None, prefix=None, redis_client=None, redis_url='redis://localhost:6379', connection_kwargs={}, **kwargs)`\n\nBases: `BaseMessageHistory`\n\nInitialize message history\n\nMessage History stores the current and previous user text prompts and\nLLM responses to allow for enriching future prompts with session\ncontext. Message history is stored in individual user or LLM prompts and\nresponses.\n\n* **Parameters:**\n  * **name** (*str*) – The name of the message history index.\n  * **session_tag** (*Optional* *[* *str* *]*) – Tag to be added to entries to link to a specific\n    conversation session. Defaults to instance ULID.\n  * **prefix** (*Optional* *[* *str* *]*) – Prefix for the keys for this conversation data.\n    Defaults to None and will be replaced with the index name.\n  * **redis_client** (*Optional* *[* *Redis* *]*) – A Redis client instance. Defaults to\n    None.\n  * **redis_url** (*str* *,* *optional*) – The redis url. Defaults to redis://localhost:6379.\n  * **connection_kwargs** (*Dict* *[* *str* *,* *Any* *]*) – The connection arguments\n    for the redis client. Defaults to empty {}.\n\n#### `add_message(message, session_tag=None)`\n\nInsert a single prompt or response into the message history.\nA timestamp is associated with it so that it can be later sorted\nin sequential ordering after retrieval.\n\n* **Parameters:**\n  * **message** (*Dict* *[* *str* *,**str* *]*) – The user prompt or LLM response.\n  * **session_tag** (*Optional* *[* *str* *]*) – Tag to be added to entries to link to a specific\n    conversation session. Defaults to instance ULID.\n* **Return type:**\n  None\n\n#### `add_messages(messages, session_tag=None)`\n\nInsert a list of prompts and responses into the message history.\nA timestamp is associated with each so that they can be later sorted\nin sequential ordering after retrieval.\n\n* **Parameters:**\n  * **messages** (*List* *[* *Dict* *[* *str* *,* *str* *]* *]*) – The list of user prompts and LLM responses.\n  * **session_tag** (*Optional* *[* *str* *]*) – Tag to be added to entries to link to a specific\n    conversation session. Defaults to instance ULID.\n* **Return type:**\n  None\n\n#### `clear()`\n\nClears the conversation message history.\n\n* **Return type:**\n  None\n\n#### `delete()`\n\nClear all conversation keys and remove the search index.\n\n* **Return type:**\n  None\n\n#### `drop(id=None)`\n\nRemove a specific exchange from the conversation history.\n\n* **Parameters:**\n  **id** (*Optional* *[* *str* *]*) – The id of the message entry to delete.\n  If None then the last entry is deleted.\n* **Return type:**\n  None\n\n#### `get_recent(top_k=5, as_text=False, raw=False, session_tag=None, role=None)`\n\nRetrieve the recent message history in sequential order.\n\n* **Parameters:**\n  * **top_k** (*int*) – The number of previous messages to return. Default is 5.\n  * **as_text** (*bool*) – Whether to return the conversation as a single string,\n    or list of alternating prompts and responses.\n  * **raw** (*bool*) – Whether to return the full Redis hash entry or just the\n    prompt and response.\n  * **session_tag** (*Optional* *[* *str* *]*) – Tag of the entries linked to a specific\n    conversation session. Defaults to instance ULID.\n  * **role** (*Optional* *[* *Union* *[* *str* *,* *List* *[* *str* *]* *]* *]*) – Filter messages by role(s).\n    Can be a single role string (\"system\", \"user\", \"llm\", \"tool\") or\n    a list of roles. If None, all roles are returned.\n* **Returns:**\n  A single string transcription of the messages\n  : or list of strings if as_text is false.\n* **Return type:**\n  Union[str, List[str]]\n* **Raises:**\n  **ValueError** – if top_k is not an integer greater than or equal to 0,\n      or if role contains invalid values.\n\n#### `store(prompt, response, session_tag=None)`\n\nInsert a prompt:response pair into the message history. A timestamp\nis associated with each exchange so that they can be later sorted\nin sequential ordering after retrieval.\n\n* **Parameters:**\n  * **prompt** (*str*) – The user prompt to the LLM.\n  * **response** (*str*) – The corresponding LLM response.\n  * **session_tag** (*Optional* *[* *str* *]*) – Tag to be added to entries to link to a specific\n    conversation session. Defaults to instance ULID.\n* **Return type:**\n  None\n\n#### `property messages: List[str] | List[Dict[str, str]]`\n\nReturns the full message history.\n",
  "tags": [],
  "last_updated": "2026-04-01T08:10:08-05:00"
}

