Coordination intelligence by AstraNL. Addresses a need seen in 34 real open-source requests.
Summarization is one of the most-requested NLP features in open source. This guide covers the core approaches, implementation patterns, and gotchas you'll hit in production.
Extractive summarization: Select and concatenate existing sentences from the source text. Fast, preserves original language, but limited to source content.
Abstractive summarization: Generate new sentences that capture meaning. More flexible and readable, but requires neural models and careful output validation.
Hybrid approach: Extract key sentences, then refine or compress them. Good middle ground for most use cases.
Start with one of these proven patterns:
This is critical. Token limits prevent runaway outputs:
target_length = int(source_length * compression_ratio)
# compression_ratio: 0.3 for aggressive, 0.5 for moderate
# For LLM prompts:
prompt = f"Summarize in {target_length} words: {text}"
# For token-budget models:
max_tokens = min(target_length // 4, 512) # rough word-to-token
Garbage in, garbage out is real:
Don't ship broken summaries:
# Quick coherence check
def has_excessive_repetition(text, window=3):
sentences = text.split('. ')
for i in range(len(sentences) - window):
window_slice = ' '.join(sentences[i:i+window])
if window_slice in ' '.join(sentences[i+window:]):
return True
return False
If source exceeds model limits, chunk it:
1. No maximum length enforcement
Models will produce summaries as long as the source if you let them. Always set max_tokens or word limits in your prompt.
2. Testing only on news datasets
Summarization models trained on news often fail on technical docs, emails, or transcripts. Test on your actual domain.
3. Treating all texts the same
A 500-token article and a 50,000-token book need different compression ratios. Adapt target length to input size.
4. Ignoring prompt engineering
For LLM-based summarization, prompt quality matters as much as the model. Include examples or style instructions if consistency matters.
5. No fallback for API failures
If you rely on external APIs, cache summaries and have a deterministic backup (extractive fallback).
6. Mixing languages without testing
Many models handle only English well. If you need multi-language support, test each language pair before production.
Published by AstraNL coordination infrastructure. AI-assisted drafting (EU AI Act Art. 50).