Skip to content

The agent engineering platform.

langchain[patch]: Migrate graphs to use optional community imports (#21100)

Eugene Yurtsev3853fe9f6438
diff --git a/libs/langchain/langchain/graphs/__init__.py b/libs/langchain/langchain/graphs/__init__.py
index 2e1f418ad6..265bb5c8d5 100644
--- a/libs/langchain/langchain/graphs/__init__.py
+++ b/libs/langchain/langchain/graphs/__init__.py
@@ -1,13 +1,45 @@
"""**Graphs** provide a natural language interface to graph databases."""
-from typing import Any
+from typing import TYPE_CHECKING, Any
from langchain._api import create_importer
-importer = create_importer(__package__, fallback_module="langchain_community.graphs")
+if TYPE_CHECKING:
+ from langchain_community.graphs import (
+ ArangoGraph,
+ FalkorDBGraph,
+ HugeGraph,
+ KuzuGraph,
+ MemgraphGraph,
+ NebulaGraph,
+ Neo4jGraph,
+ NeptuneGraph,
+ NetworkxEntityGraph,
+ RdfGraph,
+ )
+
+
+# Create a way to dynamically look up deprecated imports.
+# Used to consolidate logic for raising deprecation warnings and
+# handling optional imports.
+DEPRECATED_LOOKUP = {
+ "MemgraphGraph": "langchain_community.graphs",
+ "NetworkxEntityGraph": "langchain_community.graphs",
+ "Neo4jGraph": "langchain_community.graphs",
+ "NebulaGraph": "langchain_community.graphs",
+ "NeptuneGraph": "langchain_community.graphs",
+ "KuzuGraph": "langchain_community.graphs",
+ "HugeGraph": "langchain_community.graphs",
+ "RdfGraph": "langchain_community.graphs",
+ "ArangoGraph": "langchain_community.graphs",
+ "FalkorDBGraph": "langchain_community.graphs",
+}
+
+_import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP)
def __getattr__(name: str) -> Any:
- return importer(name)
+ """Look up attributes dynamically."""
+ return _import_attribute(name)
__all__ = [
diff --git a/libs/langchain/langchain/graphs/arangodb_graph.py b/libs/langchain/langchain/graphs/arangodb_graph.py
index 9ef5f7e7ba..bbfbf1ab11 100644
--- a/libs/langchain/langchain/graphs/arangodb_graph.py
+++ b/libs/langchain/langchain/graphs/arangodb_graph.py
@@ -1,4 +1,26 @@
-from langchain_community.graphs.arangodb_graph import ArangoGraph, get_arangodb_client
+from typing import TYPE_CHECKING, Any
+
+from langchain._api import create_importer
+
+if TYPE_CHECKING:
+ from langchain_community.graphs import ArangoGraph
+ from langchain_community.graphs.arangodb_graph import get_arangodb_client
+
+# Create a way to dynamically look up deprecated imports.
+# Used to consolidate logic for raising deprecation warnings and
+# handling optional imports.
+DEPRECATED_LOOKUP = {
+ "ArangoGraph": "langchain_community.graphs",
+ "get_arangodb_client": "langchain_community.graphs.arangodb_graph",
+}
+
+_import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP)
+
+
+def __getattr__(name: str) -> Any:
+ """Look up attributes dynamically."""
+ return _import_attribute(name)
+
__all__ = [
"ArangoGraph",
diff --git a/libs/langchain/langchain/graphs/falkordb_graph.py b/libs/langchain/langchain/graphs/falkordb_graph.py
index 61318eb392..2f8edf58ab 100644
--- a/libs/langchain/langchain/graphs/falkordb_graph.py
+++ b/libs/langchain/langchain/graphs/falkordb_graph.py
@@ -1,6 +1,22 @@
-from langchain_community.graphs.falkordb_graph import (
- FalkorDBGraph,
-)
+from typing import TYPE_CHECKING, Any
+
+from langchain._api import create_importer
+
+if TYPE_CHECKING:
+ from langchain_community.graphs import FalkorDBGraph
+
+# Create a way to dynamically look up deprecated imports.
+# Used to consolidate logic for raising deprecation warnings and
+# handling optional imports.
+DEPRECATED_LOOKUP = {"FalkorDBGraph": "langchain_community.graphs"}
+
+_import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP)
+
+
+def __getattr__(name: str) -> Any:
+ """Look up attributes dynamically."""
+ return _import_attribute(name)
+
__all__ = [
"FalkorDBGraph",
diff --git a/libs/langchain/langchain/graphs/graph_document.py b/libs/langchain/langchain/graphs/graph_document.py
index 3704b1b7d1..6848bca961 100644
--- a/libs/langchain/langchain/graphs/graph_document.py
+++ b/libs/langchain/langchain/graphs/graph_document.py
@@ -1,3 +1,33 @@
-from langchain_community.graphs.graph_document import GraphDocument, Node, Relationship
+from typing import TYPE_CHECKING, Any
-__all__ = ["Node", "Relationship", "GraphDocument"]
+from langchain._api import create_importer
+
+if TYPE_CHECKING:
+ from langchain_community.graphs.graph_document import (
+ GraphDocument,
+ Node,
+ Relationship,
+ )
+
+# Create a way to dynamically look up deprecated imports.
+# Used to consolidate logic for raising deprecation warnings and
+# handling optional imports.
+DEPRECATED_LOOKUP = {
+ "Node": "langchain_community.graphs.graph_document",
+ "Relationship": "langchain_community.graphs.graph_document",
+ "GraphDocument": "langchain_community.graphs.graph_document",
+}
+
+_import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP)
+
+
+def __getattr__(name: str) -> Any:
+ """Look up attributes dynamically."""
+ return _import_attribute(name)
+
+
+__all__ = [
+ "Node",
+ "Relationship",
+ "GraphDocument",
+]
diff --git a/libs/langchain/langchain/graphs/graph_store.py b/libs/langchain/langchain/graphs/graph_store.py
index 78b1074362..8587d5771c 100644
--- a/libs/langchain/langchain/graphs/graph_store.py
+++ b/libs/langchain/langchain/graphs/graph_store.py
@@ -1,3 +1,23 @@
-from langchain_community.graphs.graph_store import GraphStore
+from typing import TYPE_CHECKING, Any
-__all__ = ["GraphStore"]
+from langchain._api import create_importer
+
+if TYPE_CHECKING:
+ from langchain_community.graphs.graph_store import GraphStore
+
+# Create a way to dynamically look up deprecated imports.
+# Used to consolidate logic for raising deprecation warnings and
+# handling optional imports.
+DEPRECATED_LOOKUP = {"GraphStore": "langchain_community.graphs.graph_store"}
+
+_import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP)
+
+
+def __getattr__(name: str) -> Any:
+ """Look up attributes dynamically."""
+ return _import_attribute(name)
+
+
+__all__ = [
+ "GraphStore",
+]
diff --git a/libs/langchain/langchain/graphs/hugegraph.py b/libs/langchain/langchain/graphs/hugegraph.py
index d50ccbbde4..04435454ce 100644
--- a/libs/langchain/langchain/graphs/hugegraph.py
+++ b/libs/langchain/langchain/graphs/hugegraph.py
@@ -1,3 +1,23 @@
-from langchain_community.graphs.hugegraph import HugeGraph
+from typing import TYPE_CHECKING, Any
-__all__ = ["HugeGraph"]
+from langchain._api import create_importer
+
+if TYPE_CHECKING:
+ from langchain_community.graphs import HugeGraph
+
+# Create a way to dynamically look up deprecated imports.
+# Used to consolidate logic for raising deprecation warnings and
+# handling optional imports.
+DEPRECATED_LOOKUP = {"HugeGraph": "langchain_community.graphs"}
+
+_import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP)
+
+
+def __getattr__(name: str) -> Any:
+ """Look up attributes dynamically."""
+ return _import_attribute(name)
+
+
+__all__ = [
+ "HugeGraph",
+]
diff --git a/libs/langchain/langchain/graphs/kuzu_graph.py b/libs/langchain/langchain/graphs/kuzu_graph.py
index 1c6c06b0b7..f0c142cebf 100644
--- a/libs/langchain/langchain/graphs/kuzu_graph.py
+++ b/libs/langchain/langchain/graphs/kuzu_graph.py
@@ -1,3 +1,23 @@
-from langchain_community.graphs.kuzu_graph import KuzuGraph
+from typing import TYPE_CHECKING, Any
-__all__ = ["KuzuGraph"]
+from langchain._api import create_importer
+
+if TYPE_CHECKING:
+ from langchain_community.graphs import KuzuGraph
+
+# Create a way to dynamically look up deprecated imports.
+# Used to consolidate logic for raising deprecation warnings and
+# handling optional imports.
+DEPRECATED_LOOKUP = {"KuzuGraph": "langchain_community.graphs"}
+
+_import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP)
+
+
+def __getattr__(name: str) -> Any:
+ """Look up attributes dynamically."""
+ return _import_attribute(name)
+
+
+__all__ = [
+ "KuzuGraph",
+]
diff --git a/libs/langchain/langchain/graphs/memgraph_graph.py b/libs/langchain/langchain/graphs/memgraph_graph.py
index 568d88e7f3..64962e6957 100644
--- a/libs/langchain/langchain/graphs/memgraph_graph.py
+++ b/libs/langchain/langchain/graphs/memgraph_graph.py
@@ -1,5 +1,23 @@
-from langchain_community.graphs.memgraph_graph import (
- MemgraphGraph,
-)
+from typing import TYPE_CHECKING, Any
-__all__ = ["MemgraphGraph"]
+from langchain._api import create_importer
+
+if TYPE_CHECKING:
+ from langchain_community.graphs import MemgraphGraph
+
+# Create a way to dynamically look up deprecated imports.
+# Used to consolidate logic for raising deprecation warnings and
+# handling optional imports.
+DEPRECATED_LOOKUP = {"MemgraphGraph": "langchain_community.graphs"}
+
+_import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP)
+
+
+def __getattr__(name: str) -> Any:
+ """Look up attributes dynamically."""
+ return _import_attribute(name)
+
+
+__all__ = [
+ "MemgraphGraph",
+]
diff --git a/libs/langchain/langchain/graphs/nebula_graph.py b/libs/langchain/langchain/graphs/nebula_graph.py
index a5cae8edf4..7df6a2664c 100644
--- a/libs/langchain/langchain/graphs/nebula_graph.py
+++ b/libs/langchain/langchain/graphs/nebula_graph.py
@@ -1,3 +1,23 @@
-from langchain_community.graphs.nebula_graph import NebulaGraph
+from typing import TYPE_CHECKING, Any
-__all__ = ["NebulaGraph"]
+from langchain._api import create_importer
+
+if TYPE_CHECKING:
+ from langchain_community.graphs import NebulaGraph
+
+# Create a way to dynamically look up deprecated imports.
+# Used to consolidate logic for raising deprecation warnings and
+# handling optional imports.
+DEPRECATED_LOOKUP = {"NebulaGraph": "langchain_community.graphs"}
+
+_import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP)
+
+
+def __getattr__(name: str) -> Any:
+ """Look up attributes dynamically."""
+ return _import_attribute(name)
+
+
+__all__ = [
+ "NebulaGraph",
+]
diff --git a/libs/langchain/langchain/graphs/neo4j_graph.py b/libs/langchain/langchain/graphs/neo4j_graph.py
index 8bdce3ba66..abdff5b9f5 100644
--- a/libs/langchain/langchain/graphs/neo4j_graph.py
+++ b/libs/langchain/langchain/graphs/neo4j_graph.py
@@ -1,5 +1,23 @@
-from langchain_community.graphs.neo4j_graph import (
- Neo4jGraph,
-)
+from typing import TYPE_CHECKING, Any
-__all__ = ["Neo4jGraph"]
+from langchain._api import create_importer
+
+if TYPE_CHECKING:
+ from langchain_community.graphs import Neo4jGraph
+
+# Create a way to dynamically look up deprecated imports.
+# Used to consolidate logic for raising deprecation warnings and
+# handling optional imports.
+DEPRECATED_LOOKUP = {"Neo4jGraph": "langchain_community.graphs"}
+
+_import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP)
+
+
+def __getattr__(name: str) -> Any:
+ """Look up attributes dynamically."""
+ return _import_attribute(name)
+
+
+__all__ = [
+ "Neo4jGraph",
+]
diff --git a/libs/langchain/langchain/graphs/neptune_graph.py b/libs/langchain/langchain/graphs/neptune_graph.py
index bc7c4847d8..3398975918 100644
--- a/libs/langchain/langchain/graphs/neptune_graph.py
+++ b/libs/langchain/langchain/graphs/neptune_graph.py
@@ -1,3 +1,23 @@
-from langchain_community.graphs.neptune_graph import NeptuneGraph
+from typing import TYPE_CHECKING, Any
-__all__ = ["NeptuneGraph"]
+from langchain._api import create_importer
+
+if TYPE_CHECKING:
+ from langchain_community.graphs import NeptuneGraph
+
+# Create a way to dynamically look up deprecated imports.
+# Used to consolidate logic for raising deprecation warnings and
+# handling optional imports.
+DEPRECATED_LOOKUP = {"NeptuneGraph": "langchain_community.graphs"}
+
+_import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP)
+
+
+def __getattr__(name: str) -> Any:
+ """Look up attributes dynamically."""
+ return _import_attribute(name)
+
+
+__all__ = [
+ "NeptuneGraph",
+]
diff --git a/libs/langchain/langchain/graphs/networkx_graph.py b/libs/langchain/langchain/graphs/networkx_graph.py
index 3795a82799..1b360f15b8 100644
--- a/libs/langchain/langchain/graphs/networkx_graph.py
+++ b/libs/langchain/langchain/graphs/networkx_graph.py
@@ -1,13 +1,34 @@
-from langchain_community.graphs.networkx_graph import (
- KG_TRIPLE_DELIMITER,
- KnowledgeTriple,
- NetworkxEntityGraph,
- get_entities,
- parse_triples,
-)
+from typing import TYPE_CHECKING, Any
+
+from langchain._api import create_importer
+
+if TYPE_CHECKING:
+ from langchain_community.graphs import NetworkxEntityGraph
+ from langchain_community.graphs.networkx_graph import (
+ KnowledgeTriple,
+ get_entities,
+ parse_triples,
+ )
+
+# Create a way to dynamically look up deprecated imports.
+# Used to consolidate logic for raising deprecation warnings and
+# handling optional imports.
+DEPRECATED_LOOKUP = {
+ "KnowledgeTriple": "langchain_community.graphs.networkx_graph",
+ "parse_triples": "langchain_community.graphs.networkx_graph",
+ "get_entities": "langchain_community.graphs.networkx_graph",
+ "NetworkxEntityGraph": "langchain_community.graphs",
+}
+
+_import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP)
+
+
+def __getattr__(name: str) -> Any:
+ """Look up attributes dynamically."""
+ return _import_attribute(name)
+
__all__ = [
- "KG_TRIPLE_DELIMITER",
"KnowledgeTriple",
"parse_triples",
"get_entities",
diff --git a/libs/langchain/langchain/graphs/rdf_graph.py b/libs/langchain/langchain/graphs/rdf_graph.py
index b08ed2264f..7f85ffb8c3 100644
--- a/libs/langchain/langchain/graphs/rdf_graph.py
+++ b/libs/langchain/langchain/graphs/rdf_graph.py
@@ -1,6 +1,22 @@
-from langchain_community.graphs.rdf_graph import (
- RdfGraph,
-)
+from typing import TYPE_CHECKING, Any
+
+from langchain._api import create_importer
+
+if TYPE_CHECKING:
+ from langchain_community.graphs import RdfGraph
+
+# Create a way to dynamically look up deprecated imports.
+# Used to consolidate logic for raising deprecation warnings and
+# handling optional imports.
+DEPRECATED_LOOKUP = {"RdfGraph": "langchain_community.graphs"}
+
+_import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP)
+
+
+def __getattr__(name: str) -> Any:
+ """Look up attributes dynamically."""
+ return _import_attribute(name)
+
__all__ = [
"RdfGraph",