Knowledge Base Summarization Pipeline#
Dify's knowledge base summarization pipeline automatically generates LLM-powered summaries for each DocumentSegment when a document is uploaded and indexed. Each summary is independently embedded and stored in the vector database, creating a secondary retrieval layer alongside the original chunk vectors.
Prerequisites#
Summarization only runs when all three conditions hold :
document.need_summaryisTruedataset.indexing_technique == "high_quality"(economy mode is excluded)dataset.summary_index_setting.enable == True
The feature is also skipped for qa_model documents .
Configuration — SummaryIndexSettingDict#
Configuration is stored as a JSON column on the Dataset model and typed as SummaryIndexSettingDict:
| Field | Required | Description |
|---|---|---|
enable | Yes | Enables/disables automatic summary generation |
model_name | No | LLM model used for summarization |
model_provider_name | No | Provider for the model |
summary_prompt | No | Custom prompt; falls back to DEFAULT_GENERATOR_SUMMARY_PROMPT |
If summary_prompt is omitted, the default prompt is used with a {language} placeholder filled from the document's detected language .
Pipeline Orchestration#
Trigger: Post-Indexing#
After document indexing completes in document_indexing_task.py, the indexing task checks each completed document and enqueues a separate Celery task :
generate_summary_index_task.delay(dataset.id, document.id, None)
Celery Task: generate_summary_index_task#
generate_summary_index_task runs on the dataset_summary Celery queue (separate from the main dataset queue). It:
- Validates dataset, document, and configuration preconditions
- Determines whether to process only parent chunks (for
parent_child_indexdatasets) - Delegates to
SummaryIndexService.generate_summaries_for_document()
SummaryIndexService.generate_summaries_for_document()#
This method iterates over all enabled, completed segments for the document:
- Batch-creates stub
DocumentSegmentSummaryrecords withNOT_STARTEDstatus upfront — this enables status tracking before generation completes. - Loops over segments, calling
generate_and_vectorize_summary()for each. Errors on individual segments are caught and logged; processing continues for remaining segments .
Summary Generation — ParagraphIndexProcessor.generate_summary()#
The core LLM call in ParagraphIndexProcessor:
- Resolves model/provider from
summary_index_setting - Checks if the model supports vision; if so, extracts images from
SegmentAttachmentBindingor from markdown![]()links in the segment text - Invokes the LLM with
stream=Falseand returns(summary_content, LLMUsage) - Deducts token quota via
deduct_llm_quota
Vectorization — SummaryIndexService.vectorize_summary()#
After generation, the summary text is embedded and stored in the vector DB:
- Creates a
Documentwith metadatais_summary=Trueandoriginal_chunk_idlinking it back to the sourceDocumentSegment - Retries up to 3 times with exponential backoff for transient connection errors
- Updates
DocumentSegmentSummary.statustoCOMPLETED(orERROR) and saves thesummary_index_node_idand token count
Summary Lifecycle#
| Status | Meaning |
|---|---|
NOT_STARTED | Record created; generation not yet begun |
GENERATING | LLM call in progress |
COMPLETED | Summary generated and vectorized |
ERROR | Generation or vectorization failed |
DocumentSegmentSummary.enabled stays in sync with the parent DocumentSegment.enabled — disabling a segment removes its summary vector but preserves the record . Re-enabling a segment re-vectorizes the existing summary content .
Manual Triggers#
Beyond the automatic post-indexing flow, summaries can be triggered or updated via:
- REST API:
POST /datasets/<dataset_id>/documents/generate-summary— queuesgenerate_summary_index_taskper document - Segment update: When a user edits a segment with a manual summary,
update_summary_for_segment()re-vectorizes immediately (synchronously) so it is searchable at once
Key Files#
| File | Role |
|---|---|
api/tasks/generate_summary_index_task.py | Celery entry point (dataset_summary queue) |
api/services/summary_index_service.py | Orchestration, vectorization, lifecycle management |
api/core/rag/index_processor/processor/paragraph_index_processor.py | LLM call, vision image extraction, quota deduction |
api/tasks/document_indexing_task.py | Post-indexing trigger that enqueues the summary task |
api/core/rag/index_processor/index_processor_base.py | SummaryIndexSettingDict type definition |