Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

docs(kg): document GraphBuilder public methods (#878)

cakeni1a3dd5038ace
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6aff20a7..d263f9d5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
+### Changed
+
+- **`GraphBuilder`'s 6 public methods now have Google-style docstrings** (#878, closes #876) by @cakeni
+ - `semantica/kg/graph_builder.py`'s `build`, `build_single_source`, `add_temporal_edge`, `create_temporal_snapshot`, `query_temporal`, and `load_from_neo4j` — the core knowledge-graph construction API, imported directly by callers — previously had zero docstrings across all 6 methods, the only file in a 10-file audit sample with that gap, despite CONTRIBUTING.md requiring Google-style `Args`/`Returns`/`Raises`/`Example` docs for public methods. Added full docstrings for all 6, plus the previously undocumented `build_single_source`, with runnable (`# doctest: +SKIP`) usage examples
+ - **Corrected during review**: `query_temporal`'s docstring claimed the query text was used to filter the graph; the implementation only records it in the result (`results = {"query": query, ...}`) with no interpretation or filtering. Corrected to state that explicitly
+ - **Corrected during review**: `create_temporal_snapshot`'s docstring implied entities were filtered for validity at the snapshot timestamp like relationships are; the implementation copies all entities unfiltered and only filters `relationships` by `valid_from`/`valid_until`. Docstring now distinguishes the two
+ - **Corrected during review**: `add_temporal_edge`/`create_temporal_snapshot` docstrings overclaimed numeric-timestamp support; `_parse_time()` only special-cases `str` and `datetime`, falling back to a bare `str()` cast for anything else (not true numeric parsing). Narrowed to "datetime or ISO-formatted string"
+ - **Fixed along the way**: `build()`'s `**options` documented a default only for `extract`; `extract_relations`, `extract_triplets`, `ner_method`, `relation_method`, and `triplet_method` all have concrete defaults in `_extract_from_text()` (`True`, `True`, `"llm"`, `"llm"`, `"llm"`) that were left unstated, inconsistent with CONTRIBUTING.md's own docstring example of noting defaults inline
+ - `python -m pytest tests/kg/test_kg.py tests/kg/test_graph_builder_external.py -q`: 45 passed
+
### Fixed
- **MCP server reported a stale `0.4.0` version instead of the installed package version** (#870, closes #863) by @oiahoon
diff --git a/semantica/kg/graph_builder.py b/semantica/kg/graph_builder.py
index b3b54dc6..6ed783f9 100644
--- a/semantica/kg/graph_builder.py
+++ b/semantica/kg/graph_builder.py
@@ -16,7 +16,7 @@ Key Features:
Example Usage:
>>> from semantica.kg import GraphBuilder
>>> builder = GraphBuilder(merge_entities=True, resolve_conflicts=True)
- >>> graph = builder.build(sources=[{"entities": [...], "relationships": [...]}])
+ >>> graph = builder.build(sources=[{"entities": [...], "relationships": [...]}]) # doctest: +SKIP
Author: Semantica Contributors
License: MIT
@@ -291,21 +291,47 @@ class GraphBuilder:
pipeline_id: Optional[str] = None,
**options,
) -> Dict[str, Any]:
- """
- Build knowledge graph from sources.
+ """Build a knowledge graph from one or more sources.
Args:
- sources: Entities or sources list
- second_arg: Optional relationships list or entity_resolver (for backward compatibility)
- pipeline_id: Optional pipeline ID for progress tracking
- **options: Additional build options
- - extract: Whether to extract entities from text (default: True)
- - extract_relations: Whether to extract relations from text (default: False)
- - ner_method: NER method to use (default: "ml")
- - triplet_method: Triplet extraction method (default: "pattern")
+ sources: A source or list of sources. Sources may be text,
+ pre-extracted objects, or dictionaries containing ``entities``
+ and ``relationships``.
+ second_arg: An optional relationship list or entity resolver kept
+ for backward compatibility.
+ pipeline_id: Optional pipeline identifier used for progress
+ tracking.
+ **options: Additional graph-building options:
+
+ - ``extract`` (bool): Whether to run text extraction when a
+ raw string or ``{"text": ...}`` dict is passed as a source
+ (default: ``True``).
+ - ``extract_relations`` (bool): Whether to extract relations
+ during text extraction (default: ``True``).
+ - ``extract_triplets`` (bool): Whether to extract triplets
+ during text extraction (default: ``True``).
+ - ``ner_method`` (str): NER backend used for text extraction
+ (e.g. ``"ml"``, ``"pattern"``, ``"llm"``; default: ``"llm"``).
+ - ``relation_method`` (str): Relation-extraction backend
+ (e.g. ``"pattern"``, ``"llm"``; default: ``"llm"``).
+ - ``triplet_method`` (str): Triplet-extraction backend
+ (e.g. ``"pattern"``, ``"llm"``; default: ``"llm"``).
+ - ``entity_resolver``: An :class:`EntityResolver` instance
+ that overrides the one configured on the builder.
+ - ``relationships`` (list): An explicit list of relationships
+ to include in addition to those found in *sources*.
Returns:
- Dictionary containing entities and relationships
+ A dictionary containing the graph's ``entities``,
+ ``relationships``, and build ``metadata``.
+
+ Example:
+ >>> builder = GraphBuilder(resolve_conflicts=False)
+ >>> graph = builder.build( # doctest: +SKIP
+ ... {"entities": [{"id": "ada"}], "relationships": []}
+ ... )
+ >>> graph["metadata"]["num_entities"] # doctest: +SKIP
+ 1
"""
# Handle arguments
entity_resolver = None
@@ -730,6 +756,26 @@ class GraphBuilder:
pipeline_id: Optional[str] = None,
**options,
) -> Dict[str, Any]:
+ """Build a knowledge graph from a single source dictionary.
+
+ Args:
+ kg_data: Source data containing entities, relationships, or both.
+ pipeline_id: Optional pipeline identifier used for progress
+ tracking.
+ **options: Additional options forwarded to :meth:`build`.
+
+ Returns:
+ A dictionary containing the graph's ``entities``,
+ ``relationships``, and build ``metadata``.
+
+ Example:
+ >>> builder = GraphBuilder(resolve_conflicts=False)
+ >>> graph = builder.build_single_source( # doctest: +SKIP
+ ... {"entities": [{"id": "ada"}], "relationships": []}
+ ... )
+ >>> len(graph["entities"]) # doctest: +SKIP
+ 1
+ """
return self.build(kg_data, pipeline_id=pipeline_id, **options)
def add_temporal_edge(
@@ -743,21 +789,33 @@ class GraphBuilder:
temporal_metadata=None,
**kwargs,
):
- """
- Add edge with temporal validity information.
+ """Add an edge with temporal validity information to a graph.
Args:
- graph: Knowledge graph to add edge to
- source: Source entity/node
- target: Target entity/node
- relationship: Relationship type
- valid_from: Start time for relationship validity (datetime, timestamp, or ISO string)
- valid_until: End time for relationship validity (None for ongoing)
- temporal_metadata: Additional temporal metadata (timezone, precision, etc.)
- **kwargs: Additional edge properties
+ graph: Mutable knowledge-graph dictionary to update.
+ source: Identifier of the source entity or node.
+ target: Identifier of the target entity or node.
+ relationship: Relationship type for the edge.
+ valid_from: Start of the validity period. Accepts a datetime or
+ ISO-formatted string; defaults to the current time.
+ valid_until: End of the validity period, or ``None`` for an
+ ongoing relationship.
+ temporal_metadata: Optional metadata such as timezone or
+ precision information.
+ **kwargs: Additional properties to include on the edge.
Returns:
- Edge object with temporal annotations
+ The temporal edge dictionary appended to the graph's
+ ``relationships`` list.
+
+ Example:
+ >>> builder = GraphBuilder(resolve_conflicts=False)
+ >>> graph = {"entities": [], "relationships": []}
+ >>> edge = builder.add_temporal_edge( # doctest: +SKIP
+ ... graph, "ada", "analytical-engine", "DESIGNED"
+ ... )
+ >>> edge["type"] # doctest: +SKIP
+ 'DESIGNED'
"""
tracking_id = self.progress_tracker.start_tracking(
module="kg",
@@ -806,17 +864,29 @@ class GraphBuilder:
def create_temporal_snapshot(
self, graph, timestamp=None, snapshot_name=None, **options
):
- """
- Create temporal snapshot of graph at specific time point.
+ """Create a snapshot of a graph at a specific point in time.
Args:
- graph: Knowledge graph to snapshot
- timestamp: Time point for snapshot (None for current time)
- snapshot_name: Optional name for snapshot
- **options: Additional snapshot options
+ graph: Knowledge graph whose entities and relationships will be
+ copied into the snapshot.
+ timestamp: Snapshot time, or ``None`` to use the current time.
+ snapshot_name: Optional human-readable snapshot name.
+ **options: Additional snapshot options reserved for extensions.
Returns:
- Temporal snapshot object
+ A snapshot dictionary containing the name, timestamp, all copied
+ entities, relationships valid at the timestamp, and summary
+ metadata.
+
+ Example:
+ >>> builder = GraphBuilder(resolve_conflicts=False)
+ >>> snapshot = builder.create_temporal_snapshot( # doctest: +SKIP
+ ... {"entities": [{"id": "ada"}], "relationships": []},
+ ... timestamp="2026-01-01T00:00:00",
+ ... snapshot_name="new-year",
+ ... )
+ >>> snapshot["name"] # doctest: +SKIP
+ 'new-year'
"""
tracking_id = self.progress_tracker.start_tracking(
module="kg",
@@ -897,19 +967,32 @@ class GraphBuilder:
temporal_window=None,
**options,
):
- """
- Query graph at specific time point or time range.
+ """Query a graph at a specific time or over a time range.
Args:
- graph: Knowledge graph to query
- query: Query (Cypher, SPARQL, or natural language)
- at_time: Query at specific time point
- time_range: Query within time range (start, end)
- temporal_window: Temporal window size
- **options: Additional query options
+ graph: Knowledge graph to query.
+ query: Query text to record in the result. The current
+ implementation does not interpret it or filter the graph.
+ at_time: Optional point in time at which to query the graph.
+ time_range: Optional ``(start, end)`` time range. The graph is
+ evaluated at the end of the range.
+ temporal_window: Optional temporal-window value reserved for
+ query-engine integrations.
+ **options: Additional query options reserved for extensions.
Returns:
- Query results with temporal context
+ A dictionary containing the query, temporal context, entities and
+ relationships from the selected graph or snapshot, and graph
+ metadata.
+
+ Example:
+ >>> builder = GraphBuilder(resolve_conflicts=False)
+ >>> result = builder.query_temporal( # doctest: +SKIP
+ ... {"entities": [{"id": "ada"}], "relationships": []},
+ ... "MATCH (n) RETURN n",
+ ... )
+ >>> result["entities"][0]["id"] # doctest: +SKIP
+ 'ada'
"""
tracking_id = self.progress_tracker.start_tracking(
module="kg",
@@ -972,20 +1055,34 @@ class GraphBuilder:
temporal_property="valid_time",
**kwargs,
):
- """
- Load graph from Neo4j database.
+ """Load a knowledge graph from a Neo4j database.
Args:
- uri: Neo4j connection URI
- username: Neo4j username
- password: Neo4j password
- database: Neo4j database name
- enable_temporal: Enable temporal features for loaded graph
- temporal_property: Property name for temporal data
- **kwargs: Additional connection options
+ uri: Neo4j connection URI.
+ username: Neo4j username.
+ password:
+ Authentication credential supplied for the Neo4j user.
+ database: Neo4j database name.
+ enable_temporal: Whether to read temporal relationship data.
+ temporal_property: Relationship property containing temporal
+ data.
+ **kwargs: Additional connection options reserved for extensions.
Returns:
- Knowledge graph loaded from Neo4j
+ A dictionary containing loaded entities, relationships, and
+ source metadata.
+
+ Raises:
+ ImportError: If the Neo4j driver is unavailable.
+
+ Example:
+ >>> import os
+ >>> builder = GraphBuilder(resolve_conflicts=False)
+ >>> graph = builder.load_from_neo4j( # doctest: +SKIP
+ ... "bolt://localhost:7687",
+ ... "neo4j",
+ ... os.environ["NEO4J_PASSWORD"],
+ ... )
"""
tracking_id = self.progress_tracker.start_tracking(
module="kg",