Skip to content

Document Summarisation

Generate a one-paragraph summary of extracted documents for search snippets, indexing, or quick reviews. Choose extractive summarization for deterministic, network-free local processing, or abstractive for fluent, AI-generated prose.

Strategy Cargo feature Network Quality Latency
Extractive (default) summarization None — fully local Sentence-level selection from source < 100 ms typical
Abstractive summarization-llm LLM provider Generates novel prose, can summarise across sentences Provider-dependent
  • You need a one-paragraph TL;DR for indexing or search snippets.
  • You need a deterministic, network-free summary (extractive only).
  • You need a fluent abstractive summary for downstream LLM consumption.
  • You need full per-section summaries. Chunk the document first and summarise each chunk separately.
  • You need cross-document summarisation. Summarise per document, then summarise the summaries with the LLM backend.
Python
import asyncio
from xberg import ExtractInput, extract, ExtractionConfig, SummarizationConfig
async def main() -> None:
config = ExtractionConfig(
summarization=SummarizationConfig(
strategy="extractive",
max_tokens=200,
),
)
result = await extract(ExtractInput(uri="report.pdf"), config)
if result.results[0].summary:
print(result.results[0].summary.text)
asyncio.run(main())

Switch the strategy and attach an LlmConfig:

Python
import asyncio
from xberg import ExtractInput, extract, ExtractionConfig, SummarizationConfig, LlmConfig
async def main() -> None:
config = ExtractionConfig(
summarization=SummarizationConfig(
strategy="abstractive",
max_tokens=300,
llm=LlmConfig(model="openai/gpt-4o-mini"),
),
)
result = await extract(ExtractInput(uri="report.pdf"), config)
if result.results[0].summary:
print(result.results[0].summary.text)
asyncio.run(main())

The model receives the extracted content and returns the summary verbatim. Token usage records in ExtractedDocument.llm_usage with source = "summarisation_abstractive".

Strategy What max_tokens caps
Extractive Loose whitespace tokens in the output summary. The TextRank selector stops appending sentences once it would exceed the cap.
Abstractive A prompt hint asking the model for approximately this many tokens — not a provider hard cap. The provider’s request limit comes separately from SummarizationConfig.llm.max_tokens.

Leave None to let the backend pick a sensible default.

{
"summary": {
"text": "The contract sets out a 3-year support agreement with quarterly billing and a fixed escalation cap of 4%.",
"strategy": "extractive",
"token_count": 19
}
}

Pick any liter-llm provider — see LLM Integration. For most documents, gpt-4o-mini, claude-3-5-haiku, or google/gemini-2.0-flash give good cost / quality trade-offs.

API-key precedence:

  1. SummarizationConfig.llm.api_key
  2. XBERG_LLM_API_KEY
  3. Per-provider env var