Text Summarization of Large Documents using LangChain 🦜🔗

Source notebook

Repo path: gemini/use-cases/document-processing/summarization_large_documents_langchain.ipynb · Open on GitHub · intermediate

Summarizes large PDFs with LangChain and Gemini using stuff, map-reduce, and refine chains.

Summary

This notebook teaches how to use LangChain with Gemini on Vertex AI to summarize a downloaded MLOps whitepaper PDF. It initializes Vertex AI, loads and splits the PDF with PyPDFLoader, then compares stuffing, MapReduce, and Refine summarization chains. The workflow designs prompts, runs summaries, returns intermediate steps, and organizes chunk summaries with page metadata in pandas.

Key code patterns

Initialize Vertex AI model

PROJECT_ID = PROJECT_ID if PROJECT_ID != '[your-project-id]' else os.environ.get('GOOGLE_CLOUD_PROJECT')
REGION = os.environ.get('GOOGLE_CLOUD_REGION', 'global')
vertexai.init(project=PROJECT_ID, location=REGION)
vertex_llm_text = VertexAI(model_name='gemini-3.5-flash')

Sets project and region, then binds LangChain’s VertexAI wrapper to the Gemini model.

Load and split PDF

pdf_url = 'https://services.google.com/fh/files/misc/practitioners_guide_to_mlops_whitepaper.pdf'
urllib.request.urlretrieve(pdf_url, pdf_file)
pdf_loader = PyPDFLoader(pdf_file)
pages = pdf_loader.load_and_split()

Downloads the source document and converts PDF pages into LangChain documents.

Stuff chain

prompt = PromptTemplate(template=prompt_template, input_variables=['text'])
stuff_chain = load_summarize_chain(vertex_llm_text, chain_type='stuff', prompt=prompt)
try:
    print(stuff_chain.run(pages[:3]))
except Exception as e:
    print('The code failed since it will not run inference on such a huge context:', e)

Shows single-call summarization and catches failures caused by oversized context.

MapReduce chain

map_reduce_chain = load_summarize_chain(
    vertex_llm_text,
    chain_type='map_reduce',
    map_prompt=map_prompt,
    combine_prompt=combine_prompt,
    return_intermediate_steps=True,
)
map_reduce_outputs = map_reduce_chain({'input_documents': pages})

Summarizes chunks first, then combines chunk summaries into one document summary.

Refine chain

refine_chain = load_summarize_chain(
    vertex_llm_text,
    chain_type='refine',
    question_prompt=question_prompt,
    refine_prompt=refine_prompt,
    return_intermediate_steps=True,
)
refine_outputs = refine_chain({'input_documents': pages})

Builds an initial summary, then sequentially refines it with later document chunks.

Inspect intermediate summaries

for doc, out in zip(outputs['input_documents'], outputs['intermediate_steps'], strict=False):
    rows.append({
        'file_name': p(doc.metadata['source']).stem,
        'page_number': doc.metadata['page'],
        'chunks': doc.page_content,
        'concise_summary': out,
    })
summary_df = pd.DataFrame.from_dict(rows)

Keeps summaries tied to source file, page number, and original chunk text for validation.

Models & APIs used

  • Models: gemini-3.5-flash
  • APIs / services: Vertex AI
  • SDKs / libraries: vertexai, langchain, langchain-google-vertexai, langchain-community, google-cloud-aiplatform, pypdf, PyPDF2, pandas

When to use this

Use this pattern when a PDF or long document must be summarized beyond a single prompt’s context window.

Gotchas & caveats

  • This tutorial uses billable Vertex AI components.
  • Colab users must run auth.authenticate_user; Vertex AI Workbench users are pointed to separate setup instructions.
  • The notebook installs OCR and PDF utilities, then restarts the Colab kernel so packages are available.
  • PROJECT_ID falls back to GOOGLE_CLOUD_PROJECT, and REGION falls back to GOOGLE_CLOUD_REGION or global.
  • Stuffing can fail when the prompt is larger than the model context length.
  • MapReduce requires multiple model calls and can lose context between pages.
  • Refine requires many sequential calls, cannot be parallelized, depends on document order, and can suffer recency bias.
  • LangChain is described as using a tokenizer with a default 1024 token limit for map_reduce chunks.
  • The refine display code creates pdf_refine_summary from final_refine_data, then overwrites it with pdf_mp_summary.sort_values.
  • The conclusion mentions PaLM API, while executable model setup uses VertexAI with gemini-3.5-flash.

Best practices

  • Use environment variables as fallbacks for Google Cloud project and region configuration.
  • Keep summarization instructions in PromptTemplate objects for stuffing, map, combine, question, and refine prompts.
  • Test stuffing on a small page subset and handle exceptions from oversized context.
  • Use map_reduce or refine when document length exceeds what stuffing can handle.
  • Set return_intermediate_steps=True to inspect chunk-level outputs.
  • Store file name, file type, page number, chunk text, and concise summary for review in a pandas DataFrame.
  • Sort summaries by file name and page number before inspecting results.