KnowledgeGraph.extract_subgraph()

Extract a relevant subgraph for a query.

Usage

Source

KnowledgeGraph.extract_subgraph(
    query,
    *,
    max_hops=2,
    max_nodes=30,
    node_type=None,
    layer=None,
    include_ontology=False
)

Pipeline: match → expand → rank → prune.

Parameters

query: str

Text query to find seed nodes.

max_hops: int = 2

How many edge-hops to expand from each seed (default 2).

max_nodes: int = 30

Maximum nodes in the returned subgraph (default 30).

node_type: NodeType | None = None

Optional filter applied during the initial search.

layer: GraphLayer | None = None

Optional layer filter applied during the initial search.

include_ontology: bool = False
If True, the graph’s ontology is attached to the returned Subgraph for use by Subgraph.to_context().

Returns

Subgraph
The extracted subgraph with nodes, edges, and seed IDs.

Examples

sg = kg.extract_subgraph("revenue", max_hops=2, max_nodes=20)
context = sg.to_context(ontology=kg.ontology)