Skip to content

fix: defer PostgresChatMessageHistory import to keep psycopg lazy#311

Open
Humphrey (HumphreySun98) wants to merge 1 commit into
langchain-ai:mainfrom
HumphreySun98:fix/lazy-load-chat-message-histories-to-defer-psycopg
Open

fix: defer PostgresChatMessageHistory import to keep psycopg lazy#311
Humphrey (HumphreySun98) wants to merge 1 commit into
langchain-ai:mainfrom
HumphreySun98:fix/lazy-load-chat-message-histories-to-defer-psycopg

Conversation

@HumphreySun98

Copy link
Copy Markdown

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

  • `langchain_postgres/init.py`: replace the eager `from langchain_postgres.chat_message_histories import PostgresChatMessageHistory` with a module-level `getattr` that performs the import on first access. `all` is unchanged so `PostgresChatMessageHistory` remains a documented public symbol. A `TYPE_CHECKING` import keeps static analyzers happy.
  • `tests/unit_tests/test_lazy_chat_message_histories.py`: four tests
    • `PostgresChatMessageHistory` accessed via the package attribute path resolves to the same class as the direct submodule import (no backward-compat regression);
    • unknown attributes raise `AttributeError` (the `getattr` fallback works);
    • a fresh subprocess that imports only `PGEngine` / `PGVectorStore` has no `psycopg` entry in `sys.modules`;
    • the same subprocess does pick up `psycopg` once the lazy attribute is touched (so the v1 chat-history API keeps working when needed).

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.

`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
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.

psycopg (LGPL-3.0) is eagerly imported at package load, making it impossible to use only the v2 driver-agnostic API

1 participant