Skip to main content
In this lesson we build a simple “stuff documents” chain with LangChain to synthesize information from two TechCrunch articles. The objective is to:
  • Load both web pages,
  • Combine (or “stuff”) their text into a single prompt context, and
  • Send that combined context with a prompt to an LLM using LangChain’s create_stuff_documents_chain.
We’ll use two articles that both cover recent developments in generative AI, which makes them a good fit for synthesis across sources.
The image shows a webpage from TechCrunch discussing Microsoft's investment in Mistral AI, with some ads and a navigation bar on the left side.
Overview
  • Input: two TechCrunch URLs about Mistral AI and AI21 Labs.
  • Process: fetch page text with WebBaseLoader, concatenate documents into {context}, and run a “stuff” chain that uses a chat LLM.
  • Output: a synthesized answer that extracts model names or other requested facts from both articles.
Imports and setup
  • Import the chat model, the chat prompt template helper, the web loader, and the helper that builds a “stuff documents” chain.
Load the two URLs and inspect the loaded documents
  • Define the two TechCrunch URLs, construct a WebBaseLoader with both, and call load(). The loader returns a list of Document objects (one per page in this example), each containing page_content and metadata.
  • Inspect data[0].page_content or data[1].page_content to preview the scraped text. Each Document has the text and any available metadata (e.g., source).
The image shows a JupyterLab interface with a document open, displaying a large block of text discussing Microsoft's investment in Mistral AI.
Construct the prompt
  • Create a system-style prompt template that expects a context variable. The stuffs chain will concatenate document texts and inject them into {context}.
  • This simple template asks the model to extract model names (and can be adapted to request summaries, comparisons, or bullet lists).
Initialize the LLM and create the stuff documents chain
  • Instantiate the chat LLM (here using GPT-3.5-turbo) and pass it with the prompt template into create_stuff_documents_chain.
Invoke the chain with the loaded documents
  • Call the chain with the input_documents parameter set to the data list returned by the loader. The stuff chain concatenates the Documents and places that text into the prompt’s {context} variable before sending the combined prompt to the LLM.
Example model response (illustrative)
  • The LLM may return a concise synthesized answer combining information from both articles, for example:
How the stuff documents chain works
  • The “stuff” approach:
    • Concatenates all document contents into a single context string.
    • Inserts that string into the prompt template’s {context}.
    • Sends the full prompt to the LLM in one request.
When to use a stuff chain vs. retrieval
  • Use a stuff documents chain when:
    • The combined text comfortably fits within your model’s context window.
    • You want a simple, deterministic pipeline for small document sets.
  • Prefer a retrieval-based chain when:
    • You have many documents or very long texts.
    • You need relevance filtering or semantic search over chunks before prompting.
Summary
  • Steps covered:
    1. Load multiple web pages with WebBaseLoader.
    2. Build a prompt template that accepts a {context} placeholder.
    3. Create a stuff documents chain using create_stuff_documents_chain(llm, prompt).
    4. Invoke the chain with the loaded documents to get a synthesized response.
Quick reference Links and references
Use the stuff documents chain when your documents’ combined size is comfortably within the model’s context window. If you expect larger corpora or many documents, prefer a retrieval chain to select relevant chunks before prompting the LLM.

Watch Video