Skip to main content
GraphRAG SDK uses the Strategy pattern for every algorithmic concern. Each concern has an abstract base class (ABC) with one or more built-in implementations. You can swap any implementation or write your own.

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. Requires pip install graphrag-sdk[pdf].

Built-in: MarkdownLoader

Extracts text from Markdown files. Requires pip install graphrag-sdk[markdown].
Design Note: Markup Preservation For complex elements like tables, lists, and code blocks, 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 in ingest():
  • .pdf files use PdfLoader
  • .md files use MarkdownLoader
  • 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.
Tuning guidance:
  • Default (1000/100) works well for general use
  • Benchmark-winning config uses 1500/200 for 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 any text -> 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.
Design Features:
  • Strict Fallback Configuration: If you supply a custom fallback_chunker (to handle elements that individually exceed max_tokens), you cannot pass shorthand arguments like overlap_sentences or encoding_name to StructuralChunking. 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 MarkdownLoader produce flat element lists, the internal _flatten algorithm 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., PdfLoader or TextLoader which output elements=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 via EntityExtractor 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 EntityExtractor and implement extract_entities().
Step 2 — LLM Verify + Relationship Extraction: The LLM receives the pre-extracted entities and original text, verifies entities (removes invalid, adds missed), and extracts relationships with descriptions, keywords, confidence, and evidence spans. Entity Ontology: Every extracted entity is mapped to a known type from the ontology. Entities that don’t match any type are labeled "Unknown". There are three ways to define the ontology: 1. Use the defaults (11 built-in types, good for general use):
2. Pass entity_types directly (overrides defaults completely):
3. Use GraphSchema entities (schema types override both defaults and entity_types):
The priority order is: schema.entities > entity_types parameter > defaults. Default entity types: Person, Organization, Technology, Product, Location, Date, Event, Concept, Law, Dataset, Method.

Choosing an Entity Extractor

Entity Extractors: All extractors share the same threshold behavior: entities with confidence below the threshold are labeled "Unknown". Graph output:
  • All relationships use RELATES edge type. The original type (e.g. WORKS_AT) is in properties["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_IN edges) link entities to source chunks.

Writing Your Own Entity Extractor

Subclass EntityExtractor and implement extract_entities():

Writing Your Own Extraction Strategy

Replace the entire 2-step pipeline by subclassing ExtractionStrategy:

4. ResolutionStrategy

Deduplicates entities that refer to the same real-world thing.

ABC

Returns ResolutionResult with deduplicated nodes, remapped relationships, and merged_count.

Built-in: ExactMatchResolution

Deduplicates by exact property match (default: id). Fast, no LLM calls.
When to use: Default. Fast and deterministic. Works well when extraction produces consistent entity IDs.

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_threshold descriptions: concatenates them
  • If more: uses LLM to summarize into a single description
When to use: Multi-document ingestion where the same entity appears with different descriptions. Useful when the same entity is described differently across documents.

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.
When to use: Simple use cases, low latency requirements, small graphs.

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.
When to use: Default choice. Best accuracy on benchmark. Handles complex multi-hop questions.

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.
Note: MultiPathRetrieval already includes cosine reranking internally. The standalone CosineReranker is useful when using LocalRetrieval or a custom strategy.

Writing Your Own