Skip to content

BrainAPI is a knowledge graph–powered AI memory layer that transforms unstructured data into structured knowledge, enabling intelligent search, recommendations, and contextual memory for AI agents and applications.

fix(neo4j): added safe db existence check

Christian6ae960940b74
diff --git a/README.md b/README.md
index 0328c19..a848b80 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,5 @@
<p align="center">
- <img src="https://img.shields.io/badge/version-2.0.3--dev-blue?style=for-the-badge" alt="Version"/>
+ <img src="https://img.shields.io/badge/version-2.0.6--dev-blue?style=for-the-badge" alt="Version"/>
<img src="https://img.shields.io/badge/python-3.11+-green?style=for-the-badge&logo=python&logoColor=white" alt="Python"/>
<img src="https://img.shields.io/badge/license-AGPLv3%20%2B%20Commons%20Clause-purple?style=for-the-badge" alt="License"/>
</p>
diff --git a/pyproject.toml b/pyproject.toml
index 2588477..5be78ea 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
[project]
name = "brainapi2"
-version = "2.0.5-dev"
+version = "2.0.6-dev"
description = "Version 2.x.x of the BrainAPI memory layer."
authors = [
{name = "Christian",email = "[email protected]"}
@@ -57,6 +57,7 @@ levenshtein = ">=0.27.1,<0.28.0"
pillow = ">=10.0.0,<12.0.0"
sentence-transformers = ">=2.2.0,<3.0.0"
tenacity = ">=8.2.3,<10.0.0"
+python-multipart = ">=0.0.20,<0.0.21"
[tool.pylint.messages_control]
enable = ["abstract-method"]
diff --git a/src/lib/neo4j/client.py b/src/lib/neo4j/client.py
index c77fcf0..5e4a441 100644
--- a/src/lib/neo4j/client.py
+++ b/src/lib/neo4j/client.py
@@ -52,6 +52,9 @@ class Neo4jClient(GraphClient):
"""
Ensure a database exists.
"""
+ if self._verify_database_accessible(database):
+ return
+
try:
cypher_query = f"CREATE DATABASE {database}"
self.driver.execute_query(cypher_query, database_="system")
@@ -61,13 +64,25 @@ class Neo4jClient(GraphClient):
"already exists" in error_msg
or "not supported in community edition" in error_msg
or "unsupported administration command" in error_msg
- or "database not found" in error_msg
- or "graph not found" in error_msg
):
pass
else:
raise
+ retries = 0
+ max_wait_retries = 30
+ while retries < max_wait_retries:
+ if self._verify_database_accessible(database):
+ return
+ time.sleep(0.2)
+ retries += 1
+
+ if not self._verify_database_accessible(database):
+ raise RuntimeError(
+ f"Database '{database}' could not be created or is not accessible. "
+ "If using Neo4j Community Edition, you can only use the default 'neo4j' database."
+ )
+
def _verify_database_accessible(self, database: str) -> bool:
"""
Verify that a database is accessible by executing a simple query.