Skip to main content
The GraphRAG Python API allows you to integrate knowledge graph indexing and querying directly into your Python applications. This provides fine-grained control over the indexing pipeline and query execution.
This API is under development and may undergo changes in future releases. Backwards compatibility is not guaranteed at this time.

Installation

API overview

GraphRAG provides three main API modules:

Indexing API

Build knowledge graph indexes from your data

Query API

Search and query your knowledge graphs

Prompt tuning API

Generate custom prompts for your domain

Indexing API

The indexing API allows you to build knowledge graph indexes programmatically.

Import the API

Basic indexing

Create a simple indexing pipeline:

Advanced indexing options

build_index parameters

ParameterTypeDescriptionDefault
configGraphRagConfigThe GraphRAG configurationRequired
methodIndexingMethod | strIndexing method to useIndexingMethod.Standard
is_update_runboolWhether this is an incremental updateFalse
callbackslist[WorkflowCallbacks]Callbacks for pipeline eventsNone
additional_contextdict[str, Any]Custom context for the pipelineNone
verboseboolEnable verbose loggingFalse
input_documentspd.DataFrameOverride document loading with custom dataframeNone

Query API

The query API provides four search methods for querying your knowledge graph.

Import the API

Loading index data

Before querying, load the required parquet files:
Use global search for high-level questions about the entire dataset:
Use local search for specific entity-focused queries:
DRIFT combines local and global approaches:
Basic text similarity search without the knowledge graph:

Search API parameters

Global search parameters

ParameterTypeDescription
configGraphRagConfigGraphRAG configuration
entitiespd.DataFrameEntities dataframe
communitiespd.DataFrameCommunities dataframe
community_reportspd.DataFrameCommunity reports dataframe
community_levelint | NoneLeiden hierarchy level (higher = smaller communities)
dynamic_community_selectionboolEnable dynamic community selection
response_typestrResponse format description
querystrThe query string
callbackslist[QueryCallbacks]Optional callbacks
verboseboolEnable verbose logging

Local search parameters

ParameterTypeDescription
configGraphRagConfigGraphRAG configuration
entitiespd.DataFrameEntities dataframe
communitiespd.DataFrameCommunities dataframe
community_reportspd.DataFrameCommunity reports dataframe
text_unitspd.DataFrameText units dataframe
relationshipspd.DataFrameRelationships dataframe
covariatespd.DataFrame | NoneCovariates/claims dataframe
community_levelintLeiden hierarchy level
response_typestrResponse format description
querystrThe query string
callbackslist[QueryCallbacks]Optional callbacks
verboseboolEnable verbose logging

Prompt tuning API

Generate custom prompts tailored to your data domain:

Selection methods

Randomly select documents:

Query callbacks

Implement custom callbacks to track query execution:

Complete example

Here’s a complete example combining indexing and querying:

Error handling

Always handle potential errors in your pipeline:
Always validate your configuration with --dry-run via CLI before running programmatic indexing.

Next steps

CLI usage

Learn the command-line interface

Configuration

Understand configuration options

Best practices

Optimize your GraphRAG implementation

API reference

Detailed API documentation