LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
  • Overview
  • MCP Adapters
    • Overview
    • Agents
    • Callbacks
    • Chains
    • Chat models
    • Embeddings
    • Evaluation
    • Globals
    • Hub
    • Memory
    • Output parsers
    • Retrievers
    • Runnables
    • LangSmith
    • Storage
    Standard Tests
    Text Splitters
    ⌘I

    LangChain Assistant

    Ask a question to get started

    Enter to send•Shift+Enter new line

    Menu

    MCP Adapters
    OverviewAgentsCallbacksChainsChat modelsEmbeddingsEvaluationGlobalsHubMemoryOutput parsersRetrieversRunnablesLangSmithStorage
    Standard Tests
    Text Splitters
    Language
    Theme
    Pythonlangchain-classicchainscombine_documentsbase
    Module●Since v1.0

    base

    Base interface for chains combining documents.

    Attributes

    attribute
    DEFAULT_DOCUMENT_SEPARATOR: str
    attribute
    DOCUMENTS_KEY: str
    attribute
    DEFAULT_DOCUMENT_PROMPT

    Classes

    class
    Chain

    Abstract base class for creating structured sequences of calls to components.

    Chains should be used to encode a sequence of calls to components like models, document retrievers, other chains, etc., and provide a simple interface to this sequence.

    class
    BaseCombineDocumentsChain

    Base interface for chains combining documents.

    Subclasses of this chain deal with combining documents in a variety of ways. This base class exists to add some uniformity in the interface these types of chains should expose. Namely, they expect an input key related to the documents to use (default input_documents), and then also expose a method to calculate the length of a prompt from documents (useful for outside callers to use to determine whether it's safe to pass a list of documents into this chain or whether that will be longer than the context length).

    deprecatedclass
    AnalyzeDocumentChain

    Chain that splits documents, then analyzes it in pieces.

    This chain is parameterized by a TextSplitter and a CombineDocumentsChain. This chain takes a single document as input, and then splits it up into chunks and then passes those chucks to the CombineDocumentsChain.

    This class is deprecated. See below for alternative implementations which supports async and streaming modes of operation.

    If the underlying combine documents chain takes one input_documents argument (e.g., chains generated by load_summarize_chain):

    split_text = lambda x: text_splitter.create_documents([x])
    
    summarize_document_chain = split_text | chain

    If the underlying chain takes additional arguments (e.g., load_qa_chain, which takes an additional question argument), we can use the following:

    from operator import itemgetter
    from langchain_core.runnables import RunnableLambda, RunnableParallel
    
    split_text = RunnableLambda(lambda x: text_splitter.create_documents([x]))
    summarize_document_chain = RunnableParallel(
        question=itemgetter("question"),
        input_documents=itemgetter("input_document") | split_text,
    ) | chain.pick("output_text")

    To additionally return the input parameters, as AnalyzeDocumentChain does, we can wrap this construction with RunnablePassthrough:

    from operator import itemgetter
    from langchain_core.runnables import (
        RunnableLambda,
        RunnableParallel,
        RunnablePassthrough,
    )
    
    split_text = RunnableLambda(lambda x: text_splitter.create_documents([x]))
    summarize_document_chain = RunnablePassthrough.assign(
        output_text=RunnableParallel(
            question=itemgetter("question"),
            input_documents=itemgetter("input_document") | split_text,
        )
        | chain.pick("output_text")
    )
    View source on GitHub