From 181e4fc0e024d4bca182e7903259a0da4e56b7a4 Mon Sep 17 00:00:00 2001 From: Tomaz Bratanic Date: Mon, 9 Sep 2024 03:40:43 +0900 Subject: [PATCH] Add session expired retry to neo4j graph (#26182) Co-authored-by: Erick Friis --- .../langchain_community/graphs/neo4j_graph.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/libs/community/langchain_community/graphs/neo4j_graph.py b/libs/community/langchain_community/graphs/neo4j_graph.py index 309f5301250..6c93dc253e8 100644 --- a/libs/community/langchain_community/graphs/neo4j_graph.py +++ b/libs/community/langchain_community/graphs/neo4j_graph.py @@ -410,7 +410,9 @@ class Neo4jGraph(GraphStore): """Returns the structured schema of the Graph""" return self.structured_schema - def query(self, query: str, params: dict = {}) -> List[Dict[str, Any]]: + def query( + self, query: str, params: dict = {}, retry_on_session_expired: bool = True + ) -> List[Dict[str, Any]]: """Query Neo4j database. Args: @@ -421,7 +423,7 @@ class Neo4jGraph(GraphStore): List[Dict[str, Any]]: The list of dictionaries containing the query results. """ from neo4j import Query - from neo4j.exceptions import CypherSyntaxError + from neo4j.exceptions import CypherSyntaxError, SessionExpired with self._driver.session(database=self._database) as session: try: @@ -432,6 +434,15 @@ class Neo4jGraph(GraphStore): return json_data except CypherSyntaxError as e: raise ValueError(f"Generated Cypher Statement is not valid\n{e}") + except ( + SessionExpired + ) as e: # Session expired is a transient error that can be retried + if retry_on_session_expired: + return self.query( + query, params=params, retry_on_session_expired=False + ) + else: + raise e def refresh_schema(self) -> None: """