Home / Function/ format_document() — langchain Function Reference

format_document() — langchain Function Reference

Architecture documentation for the format_document() function in base.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  38ecad37_d22e_eadb_cf12_ac64aaa1fe0c["format_document()"]
  66049afc_0602_fbed_73fa_20a4904169e2["base.py"]
  38ecad37_d22e_eadb_cf12_ac64aaa1fe0c -->|defined in| 66049afc_0602_fbed_73fa_20a4904169e2
  7b4c9028_73c1_6e8c_5317_60688210c25a["format()"]
  38ecad37_d22e_eadb_cf12_ac64aaa1fe0c -->|calls| 7b4c9028_73c1_6e8c_5317_60688210c25a
  9d13d622_aa7b_65ce_8282_95208f051595["_get_document_info()"]
  38ecad37_d22e_eadb_cf12_ac64aaa1fe0c -->|calls| 9d13d622_aa7b_65ce_8282_95208f051595
  style 38ecad37_d22e_eadb_cf12_ac64aaa1fe0c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/prompts/base.py lines 414–446

def format_document(doc: Document, prompt: BasePromptTemplate[str]) -> str:
    """Format a document into a string based on a prompt template.

    First, this pulls information from the document from two sources:

    1. `page_content`: This takes the information from the `document.page_content` and
        assigns it to a variable named `page_content`.
    2. `metadata`: This takes information from `document.metadata` and assigns it to
        variables of the same name.

    Those variables are then passed into the `prompt` to produce a formatted string.

    Args:
        doc: `Document`, the `page_content` and `metadata` will be used to create the
            final string.
        prompt: `BasePromptTemplate`, will be used to format the `page_content` and
            `metadata` into the final string.

    Returns:
        String of the document formatted.

    Example:
        ```python
        from langchain_core.documents import Document
        from langchain_core.prompts import PromptTemplate

        doc = Document(page_content="This is a joke", metadata={"page": "1"})
        prompt = PromptTemplate.from_template("Page {page}: {page_content}")
        format_document(doc, prompt)
        # -> "Page 1: This is a joke"
        ```
    """
    return prompt.format(**_get_document_info(doc, prompt))

Subdomains

Frequently Asked Questions

What does format_document() do?
format_document() is a function in the langchain codebase, defined in libs/core/langchain_core/prompts/base.py.
Where is format_document() defined?
format_document() is defined in libs/core/langchain_core/prompts/base.py at line 414.
What does format_document() call?
format_document() calls 2 function(s): _get_document_info, format.

Analyze Your Own Codebase

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

Try Supermodel Free