Overview
1. LoaderStrategy
Reads raw text from a data source.ABC
Built-in: TextLoader
Reads plain text and markdown files.Built-in: PdfLoader
Extracts text from PDF files. Requirespip install graphrag-sdk[pdf].
Built-in: MarkdownLoader
Extracts text from Markdown files. Requirespip install graphrag-sdk[markdown].
MarkdownLoader intentionally outputs the raw markdown source (including pipes |, list dashes -, and code fences) rather than stripping the syntax. While this introduces minor syntax “noise”, it preserves critical structural cues (such as spatial column alignment and nested indentation) that the LLM requires during the Extraction phase to accurately parse relational data.
Default Behavior
If no loader is specified iningest():
.pdffiles usePdfLoader.mdfiles useMarkdownLoader- Everything else uses
TextLoader - If
text=is passed directly, the loader is skipped
Writing Your Own
2. ChunkingStrategy
Splits document text into overlapping chunks for processing.ABC
Built-in: FixedSizeChunking
Fixed-size character windows with configurable overlap.- Default (
1000/100) works well for general use - Benchmark-winning config uses
1500/200for richer extraction context - Smaller chunks (500) for fine-grained retrieval, larger (2000) for broader context
Built-in: SentenceTokenCapChunking
Splits at sentence boundaries (never mid-sentence) and enforces a hard token cap per chunk using tiktoken. No LLM or embedder required.Built-in: ContextualChunking
Sentence-boundary chunking with LLM-generated context prefixes prepended to each chunk (Anthropic’s contextual retrieval approach). Improves retrieval for cross-chunk co-reference questions.Cost note: generates one LLM call per chunk at ingestion time.
Built-in: CallableChunking (bring your own framework)
Adapts anytext -> list[str] function into a chunking strategy. Use this to plug in any chunking library — LlamaIndex, LangChain, Unstructured, spaCy, or your own logic — without the SDK carrying those dependencies.
Works with sync functions, async functions, and callable classes.
Built-in: StructuralChunking
Groups content by heading hierarchy into token-bounded chunks. Each chunk stores a breadcrumbs metadata field that is written as a property on the Chunk node in the knowledge graph, making section paths directly queryable via Cypher.- Strict Fallback Configuration: If you supply a custom
fallback_chunker(to handle elements that individually exceedmax_tokens), you cannot pass shorthand arguments likeoverlap_sentencesorencoding_nametoStructuralChunking. Those must be configured directly on your custom fallback chunker instance. This prevents configuration parameters from being silently dropped. - Deep-Tree Resilience: While loaders like
MarkdownLoaderproduce flat element lists, the internal_flattenalgorithm uses a recursive DFS approach. This guarantees future compatibility with highly nested DOM structures (like HTML or DOCX parsers) while preserving full hierarchical breadcrumbs. - Graceful Raw Text Fallback: Designed to compose safely with any loader. If the preceding loader does not extract structural AST elements (e.g.,
PdfLoaderorTextLoaderwhich outputelements=None), the chunker gracefully bypasses its structural logic and delegates the entire raw text to the fallback chunker, without crashing or dropping content.
Writing Your Own
3. ExtractionStrategy
Extracts entities, relationships, and entity mentions from text chunks.ABC
Built-in: GraphExtraction
Composable 2-step extraction with pluggable entity NER and LLM relationship extraction. Step 1 — Entity NER (pluggable viaEntityExtractor ABC):
GLiNERExtractor(default): Local GLiNER transformer model, no API calls. Returns typed entities with confidence scores and character spans.LLMExtractor: Uses a structured NER prompt. Returns entities with confidence, spans, and descriptions.- Custom: Subclass
EntityExtractorand implementextract_entities().
"Unknown". There are three ways to define the ontology:
1. Use the defaults (11 built-in types, good for general use):
entity_types directly (overrides defaults completely):
GraphSchema entities (schema types override both defaults and entity_types):
schema.entities > entity_types parameter > defaults.
Default entity types: Person, Organization, Technology, Product, Location, Date, Event, Concept, Law, Dataset, Method.
Choosing an Entity Extractor
Both extractors use
threshold as the confidence cut-off for a typed entity, but they treat spans below it differently. LLMExtractor keeps every span it is given and labels those with confidence below threshold as "Unknown". GLiNERExtractor only keeps spans down to candidate_threshold (by default 25 % under threshold) — those in that band are labeled "Unknown" and handed to step 2, and anything lower is discarded inside the model and never reaches the pipeline; see Extraction for details.
Graph output:
- All relationships use
RELATESedge type. The original type (e.g.WORKS_AT) is inproperties["rel_type"]. - Entity IDs are type-qualified:
compute_entity_id("Paris", "Location")->"paris__location". - Character spans stored as
properties["spans"]={chunk_id: [{start, end}]}on both entities and relationships. - Entity mentions (
MENTIONED_INedges) link entities to source chunks.
Writing Your Own Entity Extractor
SubclassEntityExtractor and implement extract_entities():
Writing Your Own Extraction Strategy
Replace the entire 2-step pipeline by subclassingExtractionStrategy:
4. ResolutionStrategy
Deduplicates entities that refer to the same real-world thing.ABC
ResolutionResult with deduplicated nodes, remapped relationships, and merged_count.
Built-in: ExactMatchResolution (ingest default)
Deduplicates by(normalized name, label) only. Fast, no embeddings, no LLM calls. Same-name entities with different labels (Person “Paris” vs Location “Paris”) stay separate; the survivor keeps every member’s description as a list (descriptions) and joined with " | " (description), and every relationship endpoint is re-pointed to it.
Airbus / Airbus SE) are left for finalize(), where the resolver pass and the LLM-judged phase see the whole graph (see Storage).
Built-in: LLMVerifiedResolution (opt-in at ingest, default at finalize())
Three tiers that catch the near-duplicates exact match misses (e.g. Acme Corp vs Acme Corporation):
(normalized name, label)exact-match merge — the same first passExactMatchResolutionperforms.- Embed the surviving nodes as
"name: description"and compare pairs in one unified same/cross-label candidate stage:similarity >= hard_threshold: merged immediately, no LLM callsoft_threshold <= similarity < hard_threshold: sent to the LLM for a YES/NO verdict, several pairs per callsimilarity < soft_threshold: skipped A label-family gate keeps pairs that cannot be one thing (Person vs Place) away from the LLM, and a cross-label merge needs a second, A/B-swapped vote to agree.
- Merge the confirmed clusters, then remap and deduplicate relationships.
" | " (concatenated below force_summary_threshold, LLM-summarized at or above it); labels absorbed from cross-label merges are kept in merged_labels and promoted to real Cypher labels on write.
GraphRAG.ingest() wires its own LLM and embedder only into the default instance. With no embedder it degrades to exact match, and with no llm the ambiguous zone is skipped so only the hard_threshold tier merges.
When to use: Pass it to ingest(resolver=...) when one document names the same entity inconsistently; recall on real duplicates is roughly 2.5x that of exact match on the benchmark corpus, at the cost of embedding and LLM calls during ingest. Across documents, finalize() already runs it over the whole graph by default (resolve=False turns that off).
5. RetrievalStrategy
Searches the knowledge graph to find context for answering a question. Uses the Template Method pattern:search() handles validation and formatting, you implement _execute().
ABC
Built-in: LocalRetrieval
Simple retrieval: vector search on chunks + 1-hop entity traversal.Built-in: MultiPathRetrieval
Production-grade retrieval with RELATES edge vector search, 2-path entity discovery, 4-path chunk retrieval, and cosine reranking. This is the default, and the strategy used for the GraphRAG-Bench results.Writing Your Own
6. RerankingStrategy
Reranks retrieval results before they are passed to the LLM for answer generation.ABC
Built-in: CosineReranker
Reranks by cosine similarity between query embedding and item embeddings.MultiPathRetrieval already includes cosine reranking internally. The standalone CosineReranker is useful when using LocalRetrieval or a custom strategy.