Skip to main content
A step-by-step tutorial for building a knowledge graph from documents and querying it with natural language.

1. Prerequisites

  • Python 3.10+
  • FalkorDB (easiest via Docker — see below)
  • An LLM API key from one of the supported providers:
    • Azure OpenAI
    • OpenAI
    • Anthropic

2. Installation

Install the SDK with all optional dependencies:
For a local editable install from a cloned repo:

3. Start FalkorDB

Run FalkorDB as a Docker container:
Verify it is running:

4. Configure Environment

Set the environment variables for your LLM provider. The example below uses Azure OpenAI:
If you use a .env file, load it yourself before importing the SDK (e.g., via python-dotenv or export commands). The SDK reads environment variables but does not auto-load .env files.

5. Define a Schema

A GraphSchema tells the extraction pipeline which entity and relationship types to look for in your documents.
You can add as many entity and relationship types as your domain requires. Descriptions help the LLM decide when to extract each type.

6. Initialize GraphRAG

Create a GraphRAG instance by providing a connection, LLM, embedder, and schema:
The default is 256 (matched-Matryoshka dimensions of text-embedding-3-large). If your embedding model produces a different dimensionality (e.g., 1024, 1536, or 3072), set embedding_dimension accordingly. ConnectionConfig accepts additional parameters such as port, username, password, and query_timeout_ms. See Configuration for the full list.
Alternative providers: The SDK also exports OpenRouterLLM and OpenRouterEmbedder for use with OpenRouter. See Configuration for details.

7. Ingest a Document

From a file path

From raw text

The ingestion pipeline runs a 9-step process: Load, Chunk, Lexical Graph, Extract (includes quality filtering), Prune, Resolve, Write, then Mentions and Chunk Indexing in parallel.

8. Query the Knowledge Graph

Retrieve context only

Use retrieve() when you want to inspect the context or use your own LLM:

Generate an answer

Use completion() for the full RAG pipeline — retrieval + answer generation:

With context inspection

Pass return_context=True to see which chunks and entities the retriever used to build the answer:

Multi-turn conversations

completion() supports native multi-turn conversations. Messages are passed directly to the LLM’s chat API as structured messages:
You can also pass history as plain dicts:
Supported roles: "system", "user", "assistant". Invalid roles raise ValueError.

9. Inspect Graph Contents

Use get_statistics() to see a summary of what the graph contains:
You can also run raw Cypher queries against the graph:

10. Finalize

After all documents have been ingested, run finalize() to deduplicate entities, backfill embeddings, and create indexes:
This step is important for query accuracy. It merges duplicate entities (e.g., “J. Doe” and “Jane Doe”) and ensures all entities have vector embeddings for semantic search.

11. Next Steps

  • Configuration — Tuning connection settings, chunking parameters, and retrieval options.
  • Strategies — Custom extraction and resolution strategies.
  • Benchmark — Reproducing benchmark results on the GraphRAG-Bench Novel corpus (20 novels, 2,010 questions).

Synchronous API

If you are not in an async context, use the synchronous convenience methods:
These wrap the async methods in asyncio.run() and are useful for scripts and notebooks.