Skip to content

Conversation

@Naseem77
Copy link
Contributor

@Naseem77 Naseem77 commented Aug 26, 2025

Summary by CodeRabbit

  • New Features

    • Added an explicit option to refresh the ontology from the database, allowing updates to be reflected immediately after schema changes without restarting or recreating objects.
  • Refactor

    • Promoted the internal schema graph reference to persistent instance state, improving consistency across operations and enabling more reliable ontology loading and saving.

@coderabbitai
Copy link

coderabbitai bot commented Aug 26, 2025

Walkthrough

Promotes the ontology schema graph to an instance attribute (self.ontology_graph), updates load/save paths to use it, and adds KnowledgeGraph.refresh_ontology() to reload ontology from the schema graph.

Changes

Cohort / File(s) Summary
KnowledgeGraph ontology handling
graphrag_sdk/kg.py
- Introduced instance attribute self.ontology_graph (replacing local ontology_graph).
- Updated ontology load/save to use Ontology.from_schema_graph(self.ontology_graph) and ontology.save_to_graph(self.ontology_graph).
- Added public method refresh_ontology() to reload and assign self._ontology.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant Client
  participant KnowledgeGraph
  participant SchemaGraph as ontology_graph
  participant Ontology

  rect rgba(230,240,255,0.5)
  note over KnowledgeGraph: Initialization
  Client->>KnowledgeGraph: __init__(schema_graph, ...)
  KnowledgeGraph->>KnowledgeGraph: self.ontology_graph = schema_graph
  KnowledgeGraph->>Ontology: from_schema_graph(self.ontology_graph)
  Ontology-->>KnowledgeGraph: ontology
  KnowledgeGraph->>KnowledgeGraph: self._ontology = ontology
  end

  rect rgba(235,255,235,0.5)
  note over KnowledgeGraph: Refresh ontology
  Client->>KnowledgeGraph: refresh_ontology()
  KnowledgeGraph->>Ontology: from_schema_graph(self.ontology_graph)
  Ontology-->>KnowledgeGraph: ontology
  KnowledgeGraph->>KnowledgeGraph: self._ontology = ontology
  end

  rect rgba(255,245,230,0.5)
  note over KnowledgeGraph: Save ontology
  Client->>KnowledgeGraph: save (implicit/elsewhere)
  KnowledgeGraph->>Ontology: save_to_graph(self.ontology_graph)
  Ontology-->>KnowledgeGraph: saved
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • swilly22

Poem

I nudge my whiskers at a graph so bright,
A schema hops from local into sight.
Refresh! I thump—new paths align,
Ontology blooms along the vine.
Save and load, a tidy burrow’s art—
Little paws, big graphs, a bounding heart. 🐇✨

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch add-refresh-ontology-to-kg

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
graphrag_sdk/kg.py (1)

193-201: Also delete the schema/ontology graph to avoid orphaned resources.

Now that ontology_graph is a first-class attribute, delete() should clean it up alongside the data graph; otherwise, {name}_schema remains in the DB.

Proposed change:

         # Delete KnowledgeGraph
         if self.name in available_graphs:
             self.graph.delete()
 
+        # Delete Ontology/Schema graph
+        schema_graph_name = f"{{{self._name}}}_schema"
+        if schema_graph_name in available_graphs:
+            self.ontology_graph.delete()
+
         # Nullify all attributes
         for key in self.__dict__.keys():
             setattr(self, key, None)
🧹 Nitpick comments (2)
graphrag_sdk/kg.py (2)

64-65: Centralize schema graph naming and confirm Redis Cluster hashtag intent.

Good move introducing a dedicated schema graph handle. To avoid hardcoding and to reuse the same name elsewhere (e.g., deletion), extract the naming into a helper or attribute. Also, please confirm the use of curly braces is intentional for Redis Cluster hashtagging; if users pass names that already contain braces, the key could become awkward.

Proposed minimal change:

-        self.ontology_graph = self.db.select_graph("{" + name + "}" + "_schema")
+        self._schema_graph_name = f"{{{name}}}_schema"
+        self.ontology_graph = self.db.select_graph(self._schema_graph_name)

Optional helper to normalize user-provided names with/without braces:

+    def _schema_graph_name_for(self, base: str) -> str:
+        # Ensure Redis Cluster hashtag for co-location, even if user supplied braces.
+        if base.startswith("{") and base.endswith("}"):
+            tag = base[1:-1]
+        else:
+            tag = base
+        return f"{{{tag}}}_schema"

214-228: refresh_ontology: docstring vs. behavior mismatch — choose one and align.

The docstring says it may raise if the refreshed ontology is empty, but the implementation always overwrites, even with an empty ontology. Pick one behavior and codify it.

Option A — enforce non-empty refresh (raise and keep the old ontology):

     def refresh_ontology(self) -> None:
@@
-        Raises:
-            Exception: If the refreshed ontology is empty and no fallback is available.
+        Raises:
+            Exception: If the refreshed ontology is empty.
@@
-        refreshed_ontology = Ontology.from_schema_graph(self.ontology_graph)
-        
-        # Always update the ontology, even if it's empty
-        # This allows users to intentionally clear the ontology if needed
-        self._ontology = refreshed_ontology
+        refreshed_ontology = Ontology.from_schema_graph(self.ontology_graph)
+        if len(refreshed_ontology.entities) == 0 and len(refreshed_ontology.relations) == 0:
+            raise Exception("The refreshed ontology is empty; refusing to overwrite the current ontology.")
+        self._ontology = refreshed_ontology

Option B — allow empty refreshes (update docstring to reflect current behavior):

     def refresh_ontology(self) -> None:
@@
-        Raises:
-            Exception: If the refreshed ontology is empty and no fallback is available.
+        Note:
+            If the refreshed ontology is empty, the current ontology will be replaced
+            with an empty ontology. This allows intentionally clearing the ontology.

Would you like me to add unit tests to validate both paths (non-empty refresh and empty refresh)? I can scaffold tests around mocked Ontology.from_schema_graph.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 6862196 and e53f6c5.

📒 Files selected for processing (1)
  • graphrag_sdk/kg.py (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
graphrag_sdk/kg.py (1)
graphrag_sdk/ontology.py (3)
  • Ontology (35-404)
  • from_schema_graph (104-125)
  • save_to_graph (389-404)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: test (openai/gpt-4.1)
🔇 Additional comments (1)
graphrag_sdk/kg.py (1)

66-76: LGTM: load/save now correctly target the instance-level schema graph.

Using Ontology.from_schema_graph(self.ontology_graph) on load and ontology.save_to_graph(self.ontology_graph) on save keeps concerns separated between data graph and schema graph. The emptiness check on first load remains intact.

@Naseem77 Naseem77 requested a review from galshubeli August 26, 2025 08:28
@galshubeli galshubeli merged commit fdea376 into main Sep 17, 2025
7 checks passed
@galshubeli galshubeli deleted the add-refresh-ontology-to-kg branch September 17, 2025 07:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants