SQLServerChatMessageHistory(
self,
session_id: str,
*,
connection: Optional[Connection] = None| Name | Type | Description |
|---|---|---|
session_id* | str | Identifier for the chat session. All messages added
through this instance are associated with this |
connection | Optional[Connection] | Default: NoneOptional pre-existing SQLAlchemy |
connection_string | str | Default: '' |
table_name | str | Default: DEFAULT_TABLE_NAME |
db_schema | Optional[str] | Default: None |
Return all messages stored for this session, in insertion order.
Chat message history stored in a SQL Server table.
Each instance is scoped to a single session_id; multiple sessions can
share the same underlying table.
The table is created on first use if it does not already exist. Messages
are stored serialized as JSON in an NVARCHAR(MAX) column and ordered
by an auto-incrementing id column on read, so insertion order is
preserved.
The class supports both username/password connections and Entra ID
authentication using the same connection-string conventions as
:class:langchain_sqlserver.SQLServer_VectorStore.
Example:
from langchain_sqlserver import SQLServerChatMessageHistory
history = SQLServerChatMessageHistory(
session_id="user-123",
connection_string=(
"Driver={ODBC Driver 18 for SQL Server};Server=tcp:host,1433;"
"Database=mydb;Uid=user;Pwd=pwd;TrustServerCertificate=yes;"
),
)
history.add_user_message("hi!")
history.add_ai_message("hello")
print(history.messages)SQL Server ODBC connection string. Required if
connection is not provided. If the string contains no
username/password and no Trusted_Connection=yes, Entra ID
authentication is used.
Name of the table that stores messages. Defaults to
sqlserver_chat_history.
Optional schema in which the table lives. The schema must already exist.