Skip to content

feat(sqlserver): add SQLServerChatMessageHistory#629

Open
DevNinja (0xDevNinja) wants to merge 1 commit into
langchain-ai:mainfrom
0xDevNinja:feat/sqlserver-chat-history
Open

feat(sqlserver): add SQLServerChatMessageHistory#629
DevNinja (0xDevNinja) wants to merge 1 commit into
langchain-ai:mainfrom
0xDevNinja:feat/sqlserver-chat-history

Conversation

@0xDevNinja

Copy link
Copy Markdown

Summary

Adds SQLServerChatMessageHistory, an implementation of langchain_core.chat_history.BaseChatMessageHistory backed by a SQL Server / Azure SQL table. This is the chat_message_histories integration tracked in #14.

Messages are JSON-serialized via message_to_dict and stored in an NVARCHAR(MAX) column, keyed by session_id, with an auto-incrementing id column that preserves insertion order on read. The class supports the same connection-string conventions as SQLServer_VectorStore — including Entra ID authentication when the connection string carries no credentials — so users can reuse existing config.

Closes #14

Public API

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)

Works out of the box with RunnableWithMessageHistory.

Files

  • langchain_sqlserver/chat_message_histories.py — new module with SQLServerChatMessageHistory.
  • langchain_sqlserver/__init__.py — export the new class.
  • tests/unit_tests/test_chat_message_histories.py — 6 unit tests covering validation, Entra-ID-vs-uid/pwd routing, serialization and the empty-input fast path. Does not require a live DB.
  • tests/unit_tests/test_imports.py — extend the __all__ snapshot.
  • tests/integration_tests/test_chat_message_histories.py — 6 integration tests (round-trip persistence, helper methods, session isolation, scoped clear, empty-batch noop, empty-session_id rejection). Uses the existing TEST_AZURESQLSERVER_TRUSTED_CONNECTION / TEST_PYODBC_CONNECTION_STRING env vars.

Test plan

  • ruff check langchain_sqlserver tests — clean
  • mypy langchain_sqlserver tests — clean (14 source files)
  • pytest tests/unit_tests/ — 7 passed (6 new + import snapshot), 1 module skipped (pre-existing)
  • pytest tests/integration_tests/test_chat_message_histories.py against a live SQL Server / Azure SQL instance.

Notes

  • The connection-string parsing and Entra-ID token-provider helpers intentionally mirror those on SQLServer_VectorStore so users get identical auth semantics across both classes. Extracting the helpers into a shared internal module is left as a follow-up so this PR stays additive.

Adds `SQLServerChatMessageHistory`, an implementation of
`langchain_core.chat_history.BaseChatMessageHistory` backed by a SQL
Server / Azure SQL table. Messages are JSON-serialized via
`message_to_dict` and stored in an `NVARCHAR(MAX)` column, keyed by
`session_id`, with an auto-incrementing `id` column that preserves
insertion order on read.

The class supports the same connection-string conventions as
`SQLServer_VectorStore`, including Entra ID authentication when the
connection string carries no credentials.

Closes langchain-ai#14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[langchain-sqlserver] Add support for storing chat history

1 participant