Skip to content

Commit

Permalink
Fix neo4j sanitize (langchain-ai#16439)
Browse files Browse the repository at this point in the history
Fix the sanitization bug and add an integration test
  • Loading branch information
tomasonjo authored Jan 23, 2024
1 parent 5de59f9 commit d0a8082
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion libs/community/langchain_community/graphs/neo4j_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def query(self, query: str, params: dict = {}) -> List[Dict[str, Any]]:
data = session.run(Query(text=query, timeout=self.timeout), params)
json_data = [r.data() for r in data]
if self.sanitize:
json_data = value_sanitize(json_data)
json_data = [value_sanitize(el) for el in json_data]
return json_data
except CypherSyntaxError as e:
raise ValueError(f"Generated Cypher Statement is not valid\n{e}")
Expand Down
28 changes: 28 additions & 0 deletions libs/community/tests/integration_tests/graphs/test_neo4j.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,31 @@ def test_neo4j_timeout() -> None:
e.code
== "Neo.ClientError.Transaction.TransactionTimedOutClientConfiguration"
)


def test_neo4j_sanitize_values() -> None:
"""Test that neo4j uses the timeout correctly."""
url = os.environ.get("NEO4J_URI")
username = os.environ.get("NEO4J_USERNAME")
password = os.environ.get("NEO4J_PASSWORD")
assert url is not None
assert username is not None
assert password is not None

graph = Neo4jGraph(url=url, username=username, password=password, sanitize=True)
# Delete all nodes in the graph
graph.query("MATCH (n) DETACH DELETE n")
# Create two nodes and a relationship
graph.query(
"""
CREATE (la:LabelA {property_a: 'a'})
CREATE (lb:LabelB)
CREATE (lc:LabelC)
MERGE (la)-[:REL_TYPE]-> (lb)
MERGE (la)-[:REL_TYPE {rel_prop: 'abc'}]-> (lc)
"""
)
graph.refresh_schema()

output = graph.query("RETURN range(0,130,1) AS result")
assert output == [{}]

0 comments on commit d0a8082

Please sign in to comment.