create_stuff_documents_chain() — langchain Function Reference
Architecture documentation for the create_stuff_documents_chain() function in stuff.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 122c3575_8823_8e18_14fe_77f730b1f9cd["create_stuff_documents_chain()"] b665d6cc_6c91_59f8_2e5b_85b1b35bfe07["stuff.py"] 122c3575_8823_8e18_14fe_77f730b1f9cd -->|defined in| b665d6cc_6c91_59f8_2e5b_85b1b35bfe07 style 122c3575_8823_8e18_14fe_77f730b1f9cd fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/langchain/langchain_classic/chains/combine_documents/stuff.py lines 25–101
def 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]:
r"""Create a chain for passing a list of Documents to a model.
Args:
llm: Language model.
prompt: Prompt template. Must contain input variable `"context"` (override by
setting document_variable), which will be used for passing in the formatted
documents.
output_parser: Output parser. Defaults to `StrOutputParser`.
document_prompt: 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_separator: String separator to use between formatted document strings.
document_variable_name: Variable name to use for the formatted documents in the
prompt. Defaults to `"context"`.
Returns:
An LCEL Runnable. The input is a dictionary that must have a `"context"` key
that maps to a `list[Document]`, and any other input variables expected in the
prompt. The `Runnable` return type depends on `output_parser` used.
Example:
```python
# 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})
```
"""
_validate_prompt(prompt, document_variable_name)
_document_prompt = document_prompt or DEFAULT_DOCUMENT_PROMPT
_output_parser = output_parser or StrOutputParser()
def format_docs(inputs: dict) -> str:
return document_separator.join(
format_document(doc, _document_prompt)
for doc in inputs[document_variable_name]
)
return (
RunnablePassthrough.assign(**{document_variable_name: format_docs}).with_config(
run_name="format_inputs",
)
| prompt
| llm
| _output_parser
).with_config(run_name="stuff_documents_chain")
Domain
Subdomains
Source
Frequently Asked Questions
What does create_stuff_documents_chain() do?
create_stuff_documents_chain() is a function in the langchain codebase, defined in libs/langchain/langchain_classic/chains/combine_documents/stuff.py.
Where is create_stuff_documents_chain() defined?
create_stuff_documents_chain() is defined in libs/langchain/langchain_classic/chains/combine_documents/stuff.py at line 25.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free