From 03858bc06b9eb78f535eddbf14f76a3fbadd5eb0 Mon Sep 17 00:00:00 2001 From: Chaitany <67532224+patelchaitany@users.noreply.github.com> Date: Wed, 24 Sep 2025 13:13:23 +0530 Subject: [PATCH] fix: Fixes get_filtered_graph_data in kuzu adapter output format is same as the othere adpaters get_filtered_graph_data. ## Description ## Type of Change - [-] Bug fix (non-breaking change that fixes an issue) - [ ] New feature (non-breaking change that adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update - [ ] Code refactoring - [ ] Performance improvement - [ ] Other (please specify): ## Changes Made Only minimal changes are made in the file for adapter for kuzu database. ## Testing ## Screenshots/Videos (if applicable) ## Pre-submission Checklist - [-] **I have tested my changes thoroughly before submitting this PR** - [-] **This PR contains minimal changes necessary to address the issue/feature** - [-] My code follows the project's coding standards and style guidelines - [-] I have added tests that prove my fix is effective or that my feature works - [-] I have added necessary documentation (if applicable) - [-] All new and existing tests pass - [-] I have searched existing PRs to ensure this change hasn't been submitted already - [-] I have linked any relevant issues in the description - [-] My commits have clear and descriptive messages ## Related Issues #1436 ## Additional Notes ## DCO Affirmation I affirm that all code in every commit of this pull request conforms to the terms of the Topoteretes Developer Certificate of Origin. --------- Co-authored-by: hajdul88 <52442977+hajdul88@users.noreply.github.com> --- .../databases/graph/kuzu/adapter.py | 41 +++++++++++++++++-- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/cognee/infrastructure/databases/graph/kuzu/adapter.py b/cognee/infrastructure/databases/graph/kuzu/adapter.py index 085d7cd00..95f534788 100644 --- a/cognee/infrastructure/databases/graph/kuzu/adapter.py +++ b/cognee/infrastructure/databases/graph/kuzu/adapter.py @@ -1277,7 +1277,6 @@ class KuzuAdapter(GraphDBInterface): A tuple containing a list of filtered node properties and a list of filtered edge properties. """ - where_clauses = [] params = {} @@ -1288,16 +1287,50 @@ class KuzuAdapter(GraphDBInterface): params[param_name] = values where_clause = " AND ".join(where_clauses) - nodes_query = f"MATCH (n:Node) WHERE {where_clause} RETURN properties(n)" + nodes_query = ( + f"MATCH (n:Node) WHERE {where_clause} RETURN n.id, {{properties: n.properties}}" + ) edges_query = f""" MATCH (n1:Node)-[r:EDGE]->(n2:Node) WHERE {where_clause.replace("n.", "n1.")} AND {where_clause.replace("n.", "n2.")} - RETURN properties(r) + RETURN n1.id, n2.id, r.relationship_name, r.properties """ nodes, edges = await asyncio.gather( self.query(nodes_query, params), self.query(edges_query, params) ) - return ([n[0] for n in nodes], [e[0] for e in edges]) + formatted_nodes = [] + for n in nodes: + if n[0]: + node_id = str(n[0]) + props = n[1] + if props.get("properties"): + try: + additional_props = json.loads(props["properties"]) + props.update(additional_props) + del props["properties"] + except json.JSONDecodeError: + logger.warning(f"Failed to parse properties JSON for node {node_id}") + formatted_nodes.append((node_id, props)) + if not formatted_nodes: + logger.warning("No nodes found in the database") + return [], [] + + formatted_edges = [] + for e in edges: + if e and len(e) >= 3: + source_id = str(e[0]) + target_id = str(e[1]) + rel_type = str(e[2]) + props = {} + if len(e) > 3 and e[3]: + try: + props = json.loads(e[3]) + except (json.JSONDecodeError, TypeError): + logger.warning( + f"Failed to parse edge properties for {source_id}->{target_id}" + ) + formatted_edges.append((source_id, target_id, rel_type, props)) + return formatted_nodes, formatted_edges async def get_graph_metrics(self, include_optional=False) -> Dict[str, Any]: """