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 parsing

Christian69cde40c5802
diff --git a/README.md b/README.md
index 2aa251c..3942a31 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,5 @@
<p align="center">
- <img src="https://img.shields.io/badge/version-2.4.1--dev-blue?style=for-the-badge" alt="Version"/>
+ <img src="https://img.shields.io/badge/version-2.4.2--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/example-docker-compose.yaml b/example-docker-compose.yaml
index 142b035..fba2806 100644
--- a/example-docker-compose.yaml
+++ b/example-docker-compose.yaml
@@ -218,15 +218,13 @@ services:
container_name: brainapi-worker
env_file:
- .env
+ environment:
+ CELERY_WORKER_CONCURRENCY: ${CELERY_WORKER_CONCURRENCY:-4}
command: >
/app/.venv/bin/celery
-A src.workers.app:ingestion_app worker
-Q ingest_data,process_architect_relationships,ingest_structured_data,consolidate_graph
--loglevel=info
- --concurrency=10
- --max-tasks-per-child=50
- --time-limit=3600
- --soft-time-limit=3000
--prefetch-multiplier=1
--optimization=fair
depends_on:
diff --git a/pyproject.toml b/pyproject.toml
index 0785061..19cba72 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
[project]
name = "brainapi2"
-version = "2.4.1-dev"
+version = "2.4.2-dev"
description = "Version 2.x.x of the BrainAPI memory layer."
authors = [
{name = "Christian",email = "[email protected]"}
diff --git a/src/lib/neo4j/client.py b/src/lib/neo4j/client.py
index e1a5c24..9fda8b9 100644
--- a/src/lib/neo4j/client.py
+++ b/src/lib/neo4j/client.py
@@ -28,6 +28,7 @@ from src.constants.kg import (
Triple,
)
from src.utils.logging import log
+from src.utils.serialization.data import always_dict
class Neo4jClient(GraphClient):
@@ -1569,12 +1570,22 @@ class Neo4jClient(GraphClient):
type(r) as rel_type,
r['description'] as rel_description,
properties(r) as rel_properties,
+ r['flow_key'] as rel_flowkey,
+ r['uuid'] as rel_uuid,
+ r['last_updated'] as rel_last_updated,
+ r['observations'] as rel_observations,
+ r['amount'] as rel_amount,
CASE WHEN startNode(r) = n THEN 'out' ELSE 'in' END AS direction,
m['uuid'] as m_uuid,
m['name'] as m_name,
labels(m) as m_labels,
m['description'] as m_description,
- properties(m) as m_properties
+ properties(m) as m_properties,
+ m['polarity'] as m_polarity,
+ m['metadata'] as m_metadata,
+ m['happened_at'] as m_happened_at,
+ m['last_updated'] as m_last_updated,
+ m['observations'] as m_observations
"""
self.ensure_database(brain_id)
@@ -1595,14 +1606,24 @@ class Neo4jClient(GraphClient):
name=record.get("m_name", "") or "",
labels=record.get("m_labels", []) or [],
description=record.get("m_description", "") or "",
- properties=record.get("m_properties", {}) or {},
+ properties=always_dict(record.get("m_properties", {})),
+ polarity=record.get("m_polarity", "neutral"),
+ metadata=always_dict(record.get("m_metadata", {})),
+ happened_at=record.get("m_happened_at", "") or "",
+ last_updated=record.get("m_last_updated", "") or "",
+ observations=record.get("m_observations", []) or [],
)
predicate = Predicate(
name=record.get("rel_type", "") or "",
description=record.get("rel_description", "") or "",
direction=record.get("direction", "neutral"),
- properties=record.get("rel_properties", {}) or {},
+ properties=always_dict(record.get("rel_properties", {})),
+ flow_key=record.get("rel_flowkey", "") or "",
+ uuid=record.get("rel_uuid", "") or "",
+ last_updated=record.get("rel_last_updated", "") or "",
+ observations=record.get("rel_observations", []) or [],
+ amount=record.get("rel_amount"),
)
nested_neighbors = self._get_neighborhood_recursive(
@@ -1660,9 +1681,9 @@ class Neo4jClient(GraphClient):
name=record.get("m_name", "") or "",
labels=record.get("m_labels", []) or [],
description=record.get("m_description", "") or "",
- properties=record.get("m_properties", {}) or {},
+ properties=always_dict(record.get("m_properties", {})),
polarity=record.get("m_polarity", "neutral"),
- metadata=record.get("m_metadata", {}) or {},
+ metadata=always_dict(record.get("m_metadata", {})),
happened_at=record.get("m_happened_at", "") or "",
last_updated=record.get("m_last_updated", "") or "",
observations=record.get("m_observations", []) or [],
@@ -1672,7 +1693,7 @@ class Neo4jClient(GraphClient):
name=record.get("r2_type", "") or "",
description=record.get("r2_description", "") or "",
direction=record.get("r2_direction", "neutral"),
- properties=record.get("r2_properties", {}) or {},
+ properties=always_dict(record.get("r2_properties", {})),
flow_key=record.get("r2_flow_key", "") or "",
last_updated=record.get("r2_last_updated", "") or "",
observations=record.get("r2_observations", []) or [],
@@ -1683,9 +1704,9 @@ class Neo4jClient(GraphClient):
name=record.get("b_name", "") or "",
labels=record.get("b_labels", []) or [],
description=record.get("b_description", "") or "",
- properties=record.get("b_properties", {}) or {},
+ properties=always_dict(record.get("b_properties", {})),
polarity=record.get("b_polarity", "neutral"),
- metadata=record.get("b_metadata", {}) or {},
+ metadata=always_dict(record.get("b_metadata", {})),
happened_at=record.get("b_happened_at", "") or "",
last_updated=record.get("b_last_updated", "") or "",
observations=record.get("b_observations", []) or [],
diff --git a/src/utils/serialization/data.py b/src/utils/serialization/data.py
index 56a3c1b..5c86eab 100644
--- a/src/utils/serialization/data.py
+++ b/src/utils/serialization/data.py
@@ -10,6 +10,7 @@ Modified By: Christian Nonis <[email protected]>
import json
import re
+from typing import Any
def str_to_json(text: str | None, empty_fallback: bool = False) -> list[str]:
@@ -37,3 +38,23 @@ def str_to_json(text: str | None, empty_fallback: bool = False) -> list[str]:
if empty_fallback:
return []
raise Exception(f"Invalid JSON string: {text}")
+
+
+def always_dict(obj: Any) -> dict:
+ """
+ Convert a string or object to a dict.
+ """
+ if not obj:
+ return {}
+ if isinstance(obj, dict):
+ return obj
+ if isinstance(obj, str):
+ try:
+ return json.loads(obj)
+ except Exception:
+ try:
+ return json.loads(obj)
+ except Exception:
+ print(f"Invalid JSON string: {obj}")
+ return {}
+ return {}