Skip to content

The agent engineering platform.

langchain[patch]: Upgrade storage to treat langchain community as optional (#21105)

Eugene Yurtsev2fcab9acd936
diff --git a/libs/langchain/langchain/storage/__init__.py b/libs/langchain/langchain/storage/__init__.py
index 0ad76ba1e0..fb4134e6e0 100644
--- a/libs/langchain/langchain/storage/__init__.py
+++ b/libs/langchain/langchain/storage/__init__.py
@@ -5,48 +5,52 @@ to a simple key-value interface.
The primary goal of these storages is to support implementation of caching.
"""
-import warnings
-from typing import Any
+from typing import TYPE_CHECKING, Any
-from langchain_core._api import LangChainDeprecationWarning
from langchain_core.stores import (
InMemoryByteStore,
InMemoryStore,
InvalidKeyException,
)
+from langchain._api import create_importer
from langchain.storage._lc_store import create_kv_docstore, create_lc_store
from langchain.storage.encoder_backed import EncoderBackedStore
from langchain.storage.file_system import LocalFileStore
-from langchain.utils.interactive_env import is_interactive_env
+if TYPE_CHECKING:
+ from langchain_community.storage import (
+ RedisStore,
+ UpstashRedisByteStore,
+ UpstashRedisStore,
+ )
-def __getattr__(name: str) -> Any:
- from langchain_community import storage
+# Create a way to dynamically look up deprecated imports.
+# Used to consolidate logic for raising deprecation warnings and
+# handling optional imports.
+DEPRECATED_LOOKUP = {
+ "RedisStore": "langchain_community.storage",
+ "UpstashRedisByteStore": "langchain_community.storage",
+ "UpstashRedisStore": "langchain_community.storage",
+}
+
+_import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP)
- # If not in interactive env, raise warning.
- if not is_interactive_env():
- warnings.warn(
- "Importing stores from langchain is deprecated. Importing from "
- "langchain will no longer be supported as of langchain==0.2.0. "
- "Please import from langchain-community instead:\n\n"
- f"`from langchain_community.storage import {name}`.\n\n"
- "To install langchain-community run `pip install -U langchain-community`.",
- category=LangChainDeprecationWarning,
- )
- return getattr(storage, name)
+def __getattr__(name: str) -> Any:
+ """Look up attributes dynamically."""
+ return _import_attribute(name)
__all__ = [
- "EncoderBackedStore",
- "RedisStore",
- "create_lc_store",
"create_kv_docstore",
- "LocalFileStore",
+ "create_lc_store",
+ "EncoderBackedStore",
+ "InMemoryByteStore",
"InMemoryStore",
"InvalidKeyException",
- "InMemoryByteStore",
+ "LocalFileStore",
+ "RedisStore",
"UpstashRedisByteStore",
"UpstashRedisStore",
]
diff --git a/libs/langchain/langchain/storage/redis.py b/libs/langchain/langchain/storage/redis.py
index bea04c92b7..80a6234c05 100644
--- a/libs/langchain/langchain/storage/redis.py
+++ b/libs/langchain/langchain/storage/redis.py
@@ -1,3 +1,23 @@
-from langchain_community.storage.redis import RedisStore
+from typing import TYPE_CHECKING, Any
-__all__ = ["RedisStore"]
+from langchain._api import create_importer
+
+if TYPE_CHECKING:
+ from langchain_community.storage import RedisStore
+
+# Create a way to dynamically look up deprecated imports.
+# Used to consolidate logic for raising deprecation warnings and
+# handling optional imports.
+DEPRECATED_LOOKUP = {"RedisStore": "langchain_community.storage"}
+
+_import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP)
+
+
+def __getattr__(name: str) -> Any:
+ """Look up attributes dynamically."""
+ return _import_attribute(name)
+
+
+__all__ = [
+ "RedisStore",
+]
diff --git a/libs/langchain/langchain/storage/upstash_redis.py b/libs/langchain/langchain/storage/upstash_redis.py
index 590389394e..59f3b10699 100644
--- a/libs/langchain/langchain/storage/upstash_redis.py
+++ b/libs/langchain/langchain/storage/upstash_redis.py
@@ -1,6 +1,27 @@
-from langchain_community.storage.upstash_redis import (
- UpstashRedisByteStore,
- UpstashRedisStore,
-)
+from typing import TYPE_CHECKING, Any
-__all__ = ["UpstashRedisStore", "UpstashRedisByteStore"]
+from langchain._api import create_importer
+
+if TYPE_CHECKING:
+ from langchain_community.storage import UpstashRedisByteStore, UpstashRedisStore
+
+# Create a way to dynamically look up deprecated imports.
+# Used to consolidate logic for raising deprecation warnings and
+# handling optional imports.
+DEPRECATED_LOOKUP = {
+ "UpstashRedisStore": "langchain_community.storage",
+ "UpstashRedisByteStore": "langchain_community.storage",
+}
+
+_import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP)
+
+
+def __getattr__(name: str) -> Any:
+ """Look up attributes dynamically."""
+ return _import_attribute(name)
+
+
+__all__ = [
+ "UpstashRedisStore",
+ "UpstashRedisByteStore",
+]