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_documentsstuffcreate_stuff_documents_chain
    Function●Since v1.0

    create_stuff_documents_chain

    Create a chain for passing a list of Documents to a model.

    Copy
    create_stuff_documents_chain(
      llm: LanguageModelLike,
      prompt: BasePromptTemplate,
      *,
      output_parser: BaseOutputParser | None = None,
      document_prompt: BasePromptTemplate | None = None,
      document_separator: str = DEFAULT_DOCUMENT_SEPARATOR,
      document_variable_name: str = DOCUMENTS_KEY
    ) -> Runnable[dict[str, Any], Any]

    Example:

    # pip install -U langchain langchain-openai
    
    from langchain_openai import ChatOpenAI
    from langchain_core.documents import Document
    from langchain_core.prompts import ChatPromptTemplate
    from langchain_classic.chains.combine_documents import (
        create_stuff_documents_chain,
    )
    
    prompt = ChatPromptTemplate.from_messages(
        [("system", "What are everyone's favorite colors:\n\n{context}")]
    )
    model = ChatOpenAI(model="gpt-3.5-turbo")
    chain = create_stuff_documents_chain(model, prompt)
    
    docs = [
        Document(page_content="Jesse loves red but not yellow"),
        Document(
            page_content="Jamal loves green but not as much as he loves orange"
        ),
    ]
    
    chain.invoke({"context": docs})

    Used in Docs

    • Alibaba cloud mysql integration
    • ApertureDB integration
    • Docling integration
    • Image captions integration
    • Jina reranker integration

    Parameters

    NameTypeDescription
    llm*LanguageModelLike

    Language model.

    prompt*BasePromptTemplate

    Prompt template. Must contain input variable "context" (override by setting document_variable), which will be used for passing in the formatted documents.

    output_parserBaseOutputParser | None
    Default:None

    Output parser. Defaults to StrOutputParser.

    document_promptBasePromptTemplate | None
    Default:None

    Prompt used for formatting each document into a string. Input variables can be "page_content" or any metadata keys that are in all documents. "page_content" will automatically retrieve the Document.page_content, and all other inputs variables will be automatically retrieved from the Document.metadata dictionary. Default to a prompt that only contains Document.page_content.

    document_separatorstr
    Default:DEFAULT_DOCUMENT_SEPARATOR

    String separator to use between formatted document strings.

    document_variable_namestr
    Default:DOCUMENTS_KEY

    Variable name to use for the formatted documents in the prompt. Defaults to "context".

    View source on GitHub