You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
langchain/libs/community/langchain_community/chat_message_histories
Philippe PRADOS 8250c177de
community[minor]: Add native async support to SQLChatMessageHistory (#22065)
# package community: Fix SQLChatMessageHistory

## Description
Here is a rewrite of `SQLChatMessageHistory` to properly implement the
asynchronous approach. The code circumvents [issue
22021](https://github.com/langchain-ai/langchain/issues/22021) by
accepting a synchronous call to `def add_messages()` in an asynchronous
scenario. This bypasses the bug.

For the same reasons as in [PR
22](https://github.com/langchain-ai/langchain-postgres/pull/32) of
`langchain-postgres`, we use a lazy strategy for table creation. Indeed,
the promise of the constructor cannot be fulfilled without this. It is
not possible to invoke a synchronous call in a constructor. We
compensate for this by waiting for the next asynchronous method call to
create the table.

The goal of the `PostgresChatMessageHistory` class (in
`langchain-postgres`) is, among other things, to be able to recycle
database connections. The implementation of the class is problematic, as
we have demonstrated in [issue
22021](https://github.com/langchain-ai/langchain/issues/22021).

Our new implementation of `SQLChatMessageHistory` achieves this by using
a singleton of type (`Async`)`Engine` for the database connection. The
connection pool is managed by this singleton, and the code is then
reentrant.

We also accept the type `str` (optionally complemented by `async_mode`.
I know you don't like this much, but it's the only way to allow an
asynchronous connection string).

In order to unify the different classes handling database connections,
we have renamed `connection_string` to `connection`, and `Session` to
`session_maker`.

Now, a single transaction is used to add a list of messages. Thus, a
crash during this write operation will not leave the database in an
unstable state with a partially added message list. This makes the code
resilient.

We believe that the `PostgresChatMessageHistory` class is no longer
necessary and can be replaced by:
```
PostgresChatMessageHistory = SQLChatMessageHistory
```
This also fixes the bug.


## Issue
- [issue 22021](https://github.com/langchain-ai/langchain/issues/22021)
  - Bug in _exit_history()
  - Bugs in PostgresChatMessageHistory and sync usage
  - Bugs in PostgresChatMessageHistory and async usage
- [issue
36](https://github.com/langchain-ai/langchain-postgres/issues/36)
 ## Twitter handle:
pprados

## Tests
- libs/community/tests/unit_tests/chat_message_histories/test_sql.py
(add async test)

@baskaryan, @eyurtsev or @hwchase17 can you check this PR ?
And, I've been waiting a long time for validation from other PRs. Can
you take a look?
- [PR 32](https://github.com/langchain-ai/langchain-postgres/pull/32)
- [PR 15575](https://github.com/langchain-ai/langchain/pull/15575)
- [PR 13200](https://github.com/langchain-ai/langchain/pull/13200)

---------

Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
3 months ago
..
__init__.py community[minor]: Add Zep Cloud components + docs + examples (#21671) 3 months ago
astradb.py (all): update removal in deprecation warnings from 0.2 to 0.3 (#21265) 4 months ago
cassandra.py community[minor]: Add async methods to CassandraChatMessageHistory (#21975) 4 months ago
cosmos_db.py infra: rm unused # noqa violations (#22049) 4 months ago
dynamodb.py community[patch]: history size support for DynamoDBChatMessageHistory (#16794) 5 months ago
elasticsearch.py community[patch]: mypy ignore fix (#18483) 5 months ago
file.py core[patch],community[patch]: Move file chat history back to community (#20834) 5 months ago
firestore.py community[major], core[patch], langchain[patch], experimental[patch]: Create langchain-community (#14463) 9 months ago
in_memory.py core[minor],langchain[patch],community[patch]: Move InMemory and File implementations of Chat History to core (#20752) 5 months ago
momento.py community[major], core[patch], langchain[patch], experimental[patch]: Create langchain-community (#14463) 9 months ago
mongodb.py (all): update removal in deprecation warnings from 0.2 to 0.3 (#21265) 4 months ago
neo4j.py community[patch]: chat message history mypy fixes #17048 (#20114) 4 months ago
postgres.py community[patch]: Add deprecation warnings to postgres implementation (#20222) 5 months ago
redis.py community[patch]: chat message history mypy fixes #17048 (#20114) 4 months ago
rocksetdb.py infra: add print rule to ruff (#16221) 7 months ago
singlestoredb.py community[major], core[patch], langchain[patch], experimental[patch]: Create langchain-community (#14463) 9 months ago
sql.py community[minor]: Add native async support to SQLChatMessageHistory (#22065) 3 months ago
streamlit.py community: Fix bug with StreamlitChatMessageHistory (#18834) 6 months ago
tidb.py community[patch]: chat message history mypy fixes (#17059) 7 months ago
upstash_redis.py community[major], core[patch], langchain[patch], experimental[patch]: Create langchain-community (#14463) 9 months ago
xata.py multiple: Remove unnecessary Ruff suppression comments (#21050) 4 months ago
zep.py docs: community docstring updates (#21040) 4 months ago
zep_cloud.py community[minor]: Add Zep Cloud components + docs + examples (#21671) 3 months ago