Chapter 3: Summarizing text using LangChain
The introduction explains what a summarization chain is and why automating summarization is useful for large volumes of documents.
After this chapter you will be able to
- Connected operations in a chain. Explain that a chain connects operations so one step’s output becomes the next step’s input, enabling tasks such as summarization to be automated.
- Value of summarization engines. Justify using a summarization engine when many documents need summaries: automation avoids work that would be impractical and costly to do manually, even with a tool such as ChatGPT.
- Chunked document summarization strategy. Describe how to summarize a document that exceeds the context window: tokenize and group its text into size-controlled chunks, summarize each chunk, then produce a final summary from those summaries.
- Initial text chunk size. Explain why the example splits the text into chunks of about 3,000 tokens.
- RunnableLambda split chain. Explain how RunnableLambda makes the split_text() function usable in a LangChain chain and what the function receives and produces.
- Independent chunk summarization. Describe how the map chain runs the same summarization prompt on each document chunk before the results are combined, and how RunnableParallel processes the sequence of chunks.
- Map chain component flow. Trace how the pipe operator connects the chunk prompt, LLM, and StrOutputParser, including what each passes to the next.
- Reduce chain summary flow. Trace how the reduce chain combines the map chain’s chunk summaries and produces a final summary, including the role of its prompt template.
- Combined MapReduce summarization flow. Trace how the combined chain splits an input document, summarizes its chunks, and compiles a final summary. Explain that calling map() on summarize_map_chain enables parallel processing of the chunks.
- Summary output and API limits. Describe how to display the generated summary and what to watch for when running MapReduce summarization on a large document.
- Cross-document MapReduce flow. Trace how text from multiple sources becomes one summary: load each source into a Document, summarize the Documents individually in the map operation, and combine those summaries in the reduce operation.
- Progressive refine summary. Describe how the refine technique updates a draft summary with each document or chunk until all have been processed.
- Existing documents versus chunks. Distinguish preparing a set of existing documents from preparing one large document: the large document is typically split into chunks, while the existing documents do not need splitting.
- Wikipedia document list and limit. Explain why WikipediaLoader can return a list of Documents for one requested article and why the example limits the number returned.
- File loader document lists. Explain that the Paestum Word, PDF, and text files are loaded into documents, and that a loader returns a list even when it loads only one document.
- Combined Document list. Explain that documents from different sources are merged into a single Document list before the final summary is generated.
- Progressive document summary refinement. Trace how the prepared documents become one summary: summarize each document with doc_summary_chain, then use refine_chain to combine that summary with the current overall summary.
- Summarization approach selection. Apply the flowchart to choose between stuffing, MapReduce, and refine based on whether there is one document or several, whether they fit in the context window, and whether the core of each document needs to appear in the final summary.
What it builds on
- Running prompts with LangChain (chapter 2) · Earlier use of LangChain to run prompts.
Section by section
Introduction
The introduction explains what a summarization chain is and why automating summarization is useful for large volumes of documents.
- Connected operations in a chain: Explain that a chain connects operations so one step’s output becomes the next step’s input, enabling tasks such as summarization to be automated.
Why it matters: This gives you the basic idea behind the summarization chains built later in the chapter. - Value of summarization engines: Justify using a summarization engine when many documents need summaries: automation avoids work that would be impractical and costly to do manually, even with a tool such as ChatGPT.
Why it matters: The volume of documents explains why a reusable summarization application is worth building.
Key terms: chain, summarization engines, large document volumes
3.1 Summarizing a document bigger than the context window
This section explains why a document can exceed an LLM’s prompt limit and introduces a chunk-and-summarize approach for handling it.
- LLM context window limits: Explain that an LLM’s context window limits how much text, including instructions and context, can be supplied in one prompt, and that the token limit varies by model.
Why it matters: The prompt limit explains why a long document cannot always be summarized in one request. - Chunked document summarization strategy: Describe how to summarize a document that exceeds the context window: tokenize and group its text into size-controlled chunks, summarize each chunk, then produce a final summary from those summaries.
Why it matters: This lets you summarize material that will not fit into a single prompt.
Key terms: context window, token limit, MapReduce, tokenizer, tokens, chunks, intermediate results
3.1.1 Chunking the text into Document objects
The section sets up a Moby Dick summarization example, chooses an initial chunk size, and explains how text length and model choice affect cost.
- Initial text chunk size: Explain why the example splits the text into chunks of about 3,000 tokens.
Why it matters: The chosen chunk size defines the constraint used in this summarization example. - Summarization run costs: Explain why the example supplies a shortened text file and how repeated runs or a different model affect cost.
Why it matters: The example’s input size and model choice make repeated experimentation more affordable.
Key terms: chunks, 3,000 tokens, context window, GPT-5-nano, Moby-Dick.txt, LLMChain, tokens
3.1.2 Split
This section prepares the OpenAI model and explains how a LangChain split chain wraps a text-splitting function as a Runnable component.
- RunnableLambda split chain: Explain how RunnableLambda makes the split_text() function usable in a LangChain chain and what the function receives and produces.
Why it matters: This explains how the text-splitting function fits into the LangChain chain. - Split process preparation: Describe the preparation steps given before setting up the split chain.
Why it matters: These are the setup steps the section gives before constructing the chain.
Key terms: Runnable, RunnableLambda, split_text(), chunks, getpass, OPENAI_API_KEY, OpenAI model
3.1.3 Map
This section shows how a LangChain map chain summarizes document chunks independently, how its components pass outputs along, and how that step fits the MapReduce model.
- Independent chunk summarization: Describe how the map chain runs the same summarization prompt on each document chunk before the results are combined, and how RunnableParallel processes the sequence of chunks.
Why it matters: This is how the section handles text chunk by chunk rather than asking the model to summarize it all at once. - Map chain component flow: Trace how the pipe operator connects the chunk prompt, LLM, and StrOutputParser, including what each passes to the next.
Why it matters: Following this flow makes clear what the summarization chain sends to the model and what it returns as text. - MapReduce stage distinction: Distinguish the independent map operation from the subsequent reduce operation in the MapReduce model described here.
Why it matters: The distinction explains why chunk summaries are produced before any combining step.
Key terms: map chain, document chunk, RunnableParallel, summarize_map_chain, pipe operator (|), summarize_chunk_prompt, llm, StrOutputParser(), MapReduce, map operation
3.1.4 Reduce
This section explains how the reduce chain turns summaries of document chunks into one final summary.
- Reduce chain summary flow: Trace how the reduce chain combines the map chain’s chunk summaries and produces a final summary, including the role of its prompt template.
Why it matters: This explains how separate chunk summaries become one summary of the combined content.
Key terms: reduce chain, map chain, lambda function, summarize_ summaries_prompt
3.1.5 MapReduce combined chain
This section combines splitting, mapping, and reducing into one MapReduce summarization chain and identifies how chunk processing can run in parallel.
- Combined MapReduce summarization flow: Trace how the combined chain splits an input document, summarizes its chunks, and compiles a final summary. Explain that calling map() on summarize_map_chain enables parallel processing of the chunks.
Why it matters: It connects the separate summarization stages into the chain that will be executed next.
Key terms: MapReduce chain, document-splitting chain, map chain, reduce chain, map(), summarize_map_chain
3.1.6 MapReduce execution
This section explains how to inspect the MapReduce summary and warns about execution costs and API limits.
- Summary output and API limits: Describe how to display the generated summary and what to watch for when running MapReduce summarization on a large document.
Why it matters: Checking the output and accounting for input length and API credit helps you run summarization without unexpected costs or errors.
Key terms: print(summary), map_reduce_chain, 429 Rate Limit error, OpenAI API credit balance
3.2 Summarizing across documents
This section explains how to summarize content from multiple sources using MapReduce or progressive refinement, and when each approach is useful.
- Cross-document MapReduce flow: Trace how text from multiple sources becomes one summary: load each source into a Document, summarize the Documents individually in the map operation, and combine those summaries in the reduce operation.
Why it matters: This gives you a way to describe how one summary can be made from several sources. - Progressive refine summary: Describe how the refine technique updates a draft summary with each document or chunk until all have been processed.
Why it matters: Refine explains how a summary can incorporate sources one at a time rather than merging separate summaries at the end. - Summarization technique tradeoff: Distinguish when the section favors MapReduce versus refine based on processing load and retention of each part’s essence.
Why it matters: The tradeoff helps you choose a summarization approach for the outcome you care about.
Key terms: Document, map operation, reduce operation, MapReduce, refine technique, current draft, final summary, content loss, processing load
3.2.1 Creating a list of Document objects
This section explains how preparing existing documents for summarization differs from splitting one large document, and why Document creation depends on the text source.
- Existing documents versus chunks: Distinguish preparing a set of existing documents from preparing one large document: the large document is typically split into chunks, while the existing documents do not need splitting.
Why it matters: It clarifies which preparation step this multi-document example can skip. - Source-dependent Document creation: Explain that creating each Document object depends on where its text comes from, so an appropriate DocumentLoader is selected for each data source; the section does not yet show the loading steps.
Why it matters: The example draws on different source types, so document creation is not identical for every source.
Key terms: chunks, Document objects, Document object, DocumentLoader, data source
3.2.2 Wikipedia content
This section explains how to load Wikipedia content for summarization and why the loader can return multiple documents.
- Website-specific content loaders: Distinguish the general WebBaseLoader from loaders tailored to particular websites, including WikipediaLoader.
Why it matters: Choosing a loader suited to the source is part of preparing content for summarization. - Wikipedia document list and limit: Explain why WikipediaLoader can return a list of Documents for one requested article and why the example limits the number returned.
Why it matters: One Wikipedia request may supply several documents to summarize, affecting cost.
Key terms: WebBaseLoader, WikipediaLoader, IMSDbLoader, AZLyricsLoader, Document list, maximum number of documents, summarization costs
3.2.3 File-based content
This section introduces the Paestum source files and explains how file loaders produce Document lists, contrasting that approach with manually creating token-sized documents.
- File loader document lists: Explain that the Paestum Word, PDF, and text files are loaded into documents, and that a loader returns a list even when it loads only one document.
Why it matters: The loader output is already in list form for the next step of working across documents. - Manual token sized documents: Distinguish loading the Paestum text file with TextLoader from the earlier manual reading of Moby-Dick.txt: the latter needed documents split to a specific number of tokens to fit the LLM prompt.
Why it matters: The contrast explains why two text files in the chapter are turned into documents differently.
Key terms: loader, documents, word_docs, pdf_docs, txt_docs, TextLoader, Python file reader, Document objects, tokens, LLM prompt
3.2.4 Creating the Document list
This section brings documents from multiple sources into one list for summarization and introduces loaders for mixed file types.
- Combined Document list: Explain that documents from different sources are merged into a single Document list before the final summary is generated.
Why it matters: The combined list gives the summarization step the content gathered from all the sources. - Mixed file document loaders: Distinguish UnstructuredLoader from DirectoryLoader as options for importing content from files of different formats.
Why it matters: These loaders offer ways to bring mixed-format files into the document collection.
Key terms: Document list, refine technique, UnstructuredLoader, DirectoryLoader, document loaders
3.2.5 Progressively refining the final summary
This section builds a step-by-step chain that summarizes each document, progressively refines an overall summary, and exposes the final and intermediate results.
- Progressive document summary refinement: Trace how the prepared documents become one summary: summarize each document with doc_summary_chain, then use refine_chain to combine that summary with the current overall summary.
Why it matters: This explains how the section turns a list of documents into a single evolving summary. - Final and intermediate outputs: Describe how to start summarization on the prepared document list and where to find the final summary and intermediate steps in full_summary.
Why it matters: The intermediate steps let you observe how the summary evolves, rather than seeing only its final version.
Key terms: doc_summary_chain, refine_chain, refine_summary(), full_summary, final_summary, intermediate_steps
3.3 Summarization flowchart
The section provides a flowchart for choosing a summarization approach based on document count, context-window fit, and the desired coverage of multiple documents.
- Summarization approach selection: Apply the flowchart to choose between stuffing, MapReduce, and refine based on whether there is one document or several, whether they fit in the context window, and whether the core of each document needs to appear in the final summary.
Why it matters: This decision rule helps you pick a summarization approach that fits the input and your coverage goal.
Key terms: context window, stuff, MapReduce, refine technique
3.4 Summary
This section recaps how to choose and assemble LangChain summarization approaches for documents of different sizes and counts.
Where this is used later
- Overview of a research summarization engine (chapter 4) · Motivates the more advanced research summarization engine.
- Summary (chapter 4) · Summarizing across multiple documents.
- Summarizing the web results (chapter 4) · Summarizing web results draws on the idea of combining summaries across multiple items.
Concepts explained in this chapter
- MapReduceMapReduce is a processing pattern that independently transforms chunks and then combines the partial results into a final result.
- summarizationSummarization is the generation of a shorter representation that preserves the important information in source content.
- pipelineA multi-step workflow is a process composed of several ordered or coordinated operations that collectively accomplish a task.
- DocumentA document is a LangChain container holding content together with metadata such as source, page number, author, or timestamp.
- text chunkA text splitter is a component that divides large text into smaller chunks or document objects.
- RunnableParallelRunnableParallel is a LangChain runnable component that executes runnable operations across inputs in parallel.
- pipe operatorThe pipe operator is syntax that passes the output of one operation to the next operation in a chain.
- LCELLCEL is LangChain's composition language for building modular and expressive large-language-model pipelines.
- context windowA context window is the maximum amount of input and conversational context a language model can process in one interaction.
- Runnable interfaceThe Runnable interface is a common LangChain programming interface for components that can be invoked and composed in workflows.
- LangChainLangChain is an open-source framework for building applications that combine language models with data sources, tools, and multi-step workflows.
- summarizerA summarization engine is a system that generates condensed representations of source content without requiring conversational interaction.
- document loaderA data loader is a component that ingests source data into a form suitable for downstream language-model processing.
- parallelismParallel processing is the simultaneous execution of the same or related operations on multiple independent inputs.
- templateA prompt template is a reusable structure that combines user input, context, and optionally examples into a model request.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
