fix: defer PostgresChatMessageHistory import to keep psycopg lazy#311
Open
Humphrey (HumphreySun98) wants to merge 1 commit into
Conversation
`langchain_postgres/__init__.py` eagerly imported `chat_message_histories.PostgresChatMessageHistory`, which top-level imports `psycopg`. That meant `from langchain_postgres import PGEngine` (or any other driver-agnostic v2 symbol) transitively loaded the LGPL-3.0 licensed `psycopg` even for downstreams that only use the v2 API on a non-LGPL driver such as `asyncpg`. Move `PostgresChatMessageHistory` behind a module-level `__getattr__` so it's only imported when actually accessed. Backward compat for `from langchain_postgres import PostgresChatMessageHistory` is preserved (the lookup triggers the lazy import); `__all__` is unchanged. Regression tests cover: - `PostgresChatMessageHistory` still resolves to the same class via the package attribute path, - a fresh subprocess that only imports `PGEngine`/`PGVectorStore` does NOT have `psycopg` in `sys.modules`, - once the lazy attribute is touched, `psycopg` does load. Fixes langchain-ai#296
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.
Description
`langchain_postgres/init.py` eagerly imports `chat_message_histories.PostgresChatMessageHistory`, which top-level imports `psycopg` (and `psycopg.sql`). So `from langchain_postgres import PGEngine` — or anything else from the driver-agnostic v2 API — transitively loads the LGPL-3.0-licensed `psycopg`, even for downstreams that only use `PGEngine` / `AsyncPGVectorStore` / `Column` on an Apache-2.0 driver like `asyncpg`. As reported in #296, this currently forces those users into a fork or post-install patching step.
This PR moves `PostgresChatMessageHistory` behind a module-level `getattr` so it's only imported when actually accessed. `PGVector` (which doesn't directly depend on `psycopg`) is unchanged.
This is the minimal of the three options the issue suggested — "lazy-load the v1 surfaces". The other two (declaring `psycopg` as an optional extra, or splitting the package) are larger and require maintainer direction; this change is a low-risk first step that solves the reported import-side-effect without changing public API.
Relevant issues
Fixes #296
Changes
Testing
```
$ python -m pytest tests/unit_tests/test_lazy_chat_message_histories.py -v
4 passed in 2.82s
```
Reverting the `init.py` change while keeping the new tests makes the subprocess test `test_psycopg_not_imported_when_only_v2_symbols_used` fail (`psycopg` shows up in `sys.modules`), confirming the regression coverage pins the buggy behavior. `ruff check` and `ruff format --check` pass.
Note
Happy to follow up with the optional-extra approach (option 2 from #296) or a docs note if you'd prefer either alongside this change — let me know.
Disclaimer: this PR was prepared with the assistance of an AI agent (Claude Code). All code and test changes were reviewed by the author before submission.