From f49da71e87f714003f144f3bc7e5beb66339d9a9 Mon Sep 17 00:00:00 2001 From: Dan O'Donovan Date: Tue, 3 Sep 2024 19:24:18 +0100 Subject: [PATCH] community[patch]: change default Neo4j username/password (#25226) **Description:** Change the default Neo4j username/password (when not supplied as environment variable or in code) from `None` to `""`. Neo4j has an option to [disable auth](https://neo4j.com/docs/operations-manual/current/configuration/configuration-settings/#config_dbms.security.auth_enabled) which is helpful when developing. When auth is disabled, the username / password through the `neo4j` module should be `""` (ie an empty string). Empty strings get marked as false in `langchain_core.utils.env.get_from_dict_or_env` -- changing this code / behaviour would have a wide impact and is undesirable. In order to both _allow_ access to Neo4j with auth disabled and _not_ impact `langchain_core` this patch is presented. The downside would be that if a user forgets to set NEO4J_USERNAME or NEO4J_PASSWORD they would see an invalid credentials error rather than missing credentials error. This could be mitigated but would result in a less elegant patch! **Issue:** Fix issue where langchain cannot communicate with Neo4j if Neo4j auth is disabled. --- .../langchain_community/graphs/neo4j_graph.py | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/libs/community/langchain_community/graphs/neo4j_graph.py b/libs/community/langchain_community/graphs/neo4j_graph.py index f19ff07330c..309f5301250 100644 --- a/libs/community/langchain_community/graphs/neo4j_graph.py +++ b/libs/community/langchain_community/graphs/neo4j_graph.py @@ -346,18 +346,27 @@ class Neo4jGraph(GraphStore): ) url = get_from_dict_or_env({"url": url}, "url", "NEO4J_URI") - username = get_from_dict_or_env( - {"username": username}, "username", "NEO4J_USERNAME" - ) - password = get_from_dict_or_env( - {"password": password}, "password", "NEO4J_PASSWORD" - ) + # if username and password are "", assume Neo4j auth is disabled + if username == "" and password == "": + auth = None + else: + username = get_from_dict_or_env( + {"username": username}, + "username", + "NEO4J_USERNAME", + ) + password = get_from_dict_or_env( + {"password": password}, + "password", + "NEO4J_PASSWORD", + ) + auth = (username, password) database = get_from_dict_or_env( {"database": database}, "database", "NEO4J_DATABASE", "neo4j" ) self._driver = neo4j.GraphDatabase.driver( - url, auth=(username, password), **(driver_config or {}) + url, auth=auth, **(driver_config or {}) ) self._database = database self.timeout = timeout