Home / Function/ collapse_docs() — langchain Function Reference

collapse_docs() — langchain Function Reference

Architecture documentation for the collapse_docs() function in reduce.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  e5e85969_ef5a_d0e5_f524_145250c67cf1["collapse_docs()"]
  c47d011a_bcd5_947b_be68_ac5c423fea9d["reduce.py"]
  e5e85969_ef5a_d0e5_f524_145250c67cf1 -->|defined in| c47d011a_bcd5_947b_be68_ac5c423fea9d
  5ec5e85d_0e34_733e_07b9_d13ee52ef47f["_collapse()"]
  5ec5e85d_0e34_733e_07b9_d13ee52ef47f -->|calls| e5e85969_ef5a_d0e5_f524_145250c67cf1
  style e5e85969_ef5a_d0e5_f524_145250c67cf1 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain/langchain_classic/chains/combine_documents/reduce.py lines 67–96

def collapse_docs(
    docs: list[Document],
    combine_document_func: CombineDocsProtocol,
    **kwargs: Any,
) -> Document:
    """Execute a collapse function on a set of documents and merge their metadatas.

    Args:
        docs: A list of `Document` objects to combine.
        combine_document_func: A function that takes in a list of `Document` objects and
            optionally addition keyword parameters and combines them into a single
            string.
        **kwargs: Arbitrary additional keyword params to pass to the
            `combine_document_func`.

    Returns:
        A single `Document` with the output of `combine_document_func` for the page
            content and the combined metadata's of all the input documents. All metadata
            values are strings, and where there are overlapping keys across documents
            the values are joined by `', '`.
    """
    result = combine_document_func(docs, **kwargs)
    combined_metadata = {k: str(v) for k, v in docs[0].metadata.items()}
    for doc in docs[1:]:
        for k, v in doc.metadata.items():
            if k in combined_metadata:
                combined_metadata[k] += f", {v}"
            else:
                combined_metadata[k] = str(v)
    return Document(page_content=result, metadata=combined_metadata)

Subdomains

Called By

Frequently Asked Questions

What does collapse_docs() do?
collapse_docs() is a function in the langchain codebase, defined in libs/langchain/langchain_classic/chains/combine_documents/reduce.py.
Where is collapse_docs() defined?
collapse_docs() is defined in libs/langchain/langchain_classic/chains/combine_documents/reduce.py at line 67.
What calls collapse_docs()?
collapse_docs() is called by 1 function(s): _collapse.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free