feat(sqlserver): add SQLServerChatMessageHistory - #629
Merged
Facundo Santiago (santiagxf) merged 8 commits intoJul 6, 2026
Merged
Conversation
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
Collaborator
|
DevNinja (@0xDevNinja) please let us know if you can address the issues with this PR. |
Contributor
Author
|
Facundo Santiago (@santiagxf) sorted the lint/formatting issues and pushed, CI is passing now. Ready for another look. |
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a new SQLServerChatMessageHistory implementation to the langchain_sqlserver package, enabling LangChain chat message history persistence in SQL Server / Azure SQL using a simple table-backed storage model.
Changes:
- Introduces
SQLServerChatMessageHistorywith table creation, message insert/retrieve, and session-scopedclear(). - Exports the new class from
langchain_sqlserver.__init__and updates the__all__snapshot test. - Adds unit tests (no live DB) and integration tests (requires SQL Server/Azure SQL env vars) for persistence and session isolation.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| libs/sqlserver/langchain_sqlserver/chat_message_histories.py | New SQL Server-backed BaseChatMessageHistory implementation, including connection/auth helpers. |
| libs/sqlserver/langchain_sqlserver/init.py | Exports SQLServerChatMessageHistory in the public package API. |
| libs/sqlserver/tests/unit_tests/test_chat_message_histories.py | Adds unit tests for validation and behavior without requiring a live DB. |
| libs/sqlserver/tests/integration_tests/test_chat_message_histories.py | Adds integration coverage for round-trip persistence and session isolation. |
| libs/sqlserver/tests/unit_tests/test_imports.py | Updates expected __all__ list to include the new public symbol. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Comment on lines
+95
to
+96
| assert {m["data"]["content"] for m in decoded} == {"hi", "yo"} | ||
| def _make_history_without_db() -> SQLServerChatMessageHistory: |
Comment on lines
+148
to
+150
| except ProgrammingError as e: | ||
| logging.error(f"Create table {self.table_name} failed.") | ||
| raise Exception(e.__cause__) from None |
Comment on lines
+162
to
+164
| except DBAPIError as e: | ||
| logging.error(f"Fetch messages failed:\n {e.__cause__}\n") | ||
| raise Exception(e.__cause__) from None |
Comment on lines
+190
to
+192
| except DBAPIError as e: | ||
| logging.error(f"Add messages failed:\n {e.__cause__}\n") | ||
| raise Exception(e.__cause__) from None |
Comment on lines
+204
to
+206
| except DBAPIError as e: | ||
| logging.error(f"Clear messages failed:\n {e.__cause__}\n") | ||
| raise Exception(e.__cause__) from None |
Comment on lines
+256
to
+259
| except KeyError as k: | ||
| raise Exception( | ||
| f"Server, DB details should be provided in connection string. {k}" | ||
| ) |
Comment on lines
+34
to
+36
| # Best-effort cleanup: clear messages for this session, then drop the | ||
| # underlying table if no other session still has rows in it. | ||
| history.clear() |
Resolve __init__ and test_imports conflicts from the SQLServerVectorStore rename (langchain-ai#798); keep chat history export alongside the renamed class and deprecated alias. Apply ruff format.
Facundo Santiago (santiagxf)
merged commit Jul 6, 2026
9be6796
into
langchain-ai:main
8 checks passed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
SQLServerChatMessageHistory, an implementation oflangchain_core.chat_history.BaseChatMessageHistorybacked by a SQL Server / Azure SQL table. This is thechat_message_historiesintegration tracked in #14.Messages are JSON-serialized via
message_to_dictand stored in anNVARCHAR(MAX)column, keyed bysession_id, with an auto-incrementingidcolumn that preserves insertion order on read. The class supports the same connection-string conventions asSQLServer_VectorStore— including Entra ID authentication when the connection string carries no credentials — so users can reuse existing config.Closes #14
Public API
Works out of the box with
RunnableWithMessageHistory.Files
langchain_sqlserver/chat_message_histories.py— new module withSQLServerChatMessageHistory.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, scopedclear, empty-batch noop, empty-session_idrejection). Uses the existingTEST_AZURESQLSERVER_TRUSTED_CONNECTION/TEST_PYODBC_CONNECTION_STRINGenv vars.Test plan
ruff check langchain_sqlserver tests— cleanmypy 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.pyagainst a live SQL Server / Azure SQL instance.Notes
SQLServer_VectorStoreso 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.