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
All extractors share the same
threshold behavior: entities with confidence below the threshold are labeled "Unknown".
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
Deduplicates by exact property match (default:id). Fast, no LLM calls.
Built-in: DescriptionMergeResolution
Deduplicates by(normalized name, label) — same-name entities with different labels (e.g. Person “Paris” vs Location “Paris”) are kept separate. Merges descriptions:
- If fewer than
force_summary_thresholddescriptions: concatenates them - If more: uses LLM to summarize into a single description
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.