Home / Function/ render() — langchain Function Reference

render() — langchain Function Reference

Architecture documentation for the render() function in mustache.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  0d68eb8b_8003_f5a3_d717_126f0919d1af["render()"]
  88ae0419_8f6b_8b31_dd8f_a3bc6bbcb7b6["mustache.py"]
  0d68eb8b_8003_f5a3_d717_126f0919d1af -->|defined in| 88ae0419_8f6b_8b31_dd8f_a3bc6bbcb7b6
  ee317545_08fa_c1b5_d4bc_6d8f5e1e4e7c["tokenize()"]
  0d68eb8b_8003_f5a3_d717_126f0919d1af -->|calls| ee317545_08fa_c1b5_d4bc_6d8f5e1e4e7c
  fef86d61_ac5c_9959_034b_78139e1c1dcc["_get_key()"]
  0d68eb8b_8003_f5a3_d717_126f0919d1af -->|calls| fef86d61_ac5c_9959_034b_78139e1c1dcc
  40658de4_5ff4_9ab6_c82f_d132aac5c2af["_html_escape()"]
  0d68eb8b_8003_f5a3_d717_126f0919d1af -->|calls| 40658de4_5ff4_9ab6_c82f_d132aac5c2af
  7c39b038_3cb9_2f80_cba3_175369a814de["_get_partial()"]
  0d68eb8b_8003_f5a3_d717_126f0919d1af -->|calls| 7c39b038_3cb9_2f80_cba3_175369a814de
  style 0d68eb8b_8003_f5a3_d717_126f0919d1af fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/utils/mustache.py lines 466–704

def render(
    template: str | list[tuple[str, str]] = "",
    data: Mapping[str, Any] = EMPTY_DICT,
    partials_dict: Mapping[str, str] = EMPTY_DICT,
    padding: str = "",
    def_ldel: str = "{{",
    def_rdel: str = "}}",
    scopes: Scopes | None = None,
    warn: bool = False,  # noqa: FBT001,FBT002
    keep: bool = False,  # noqa: FBT001,FBT002
) -> str:
    """Render a mustache template.

    Renders a mustache template with a data scope and inline partial capability.

    Args:
        template: A file-like object or a string containing the template.
        data: A python dictionary with your data scope.
        partials_dict: A python dictionary which will be search for partials
            before the filesystem is.

            `{'include': 'foo'}` is the same as a file called include.mustache
            (defaults to `{}`).
        padding: This is for padding partials, and shouldn't be used
            (but can be if you really want to).
        def_ldel: The default left delimiter

            (`'{{'` by default, as in spec compliant mustache).
        def_rdel: The default right delimiter

            (`'}}'` by default, as in spec compliant mustache).
        scopes: The list of scopes that `get_key` will look through.
        warn: Log a warning when a template substitution isn't found in the data
        keep: Keep unreplaced tags when a substitution isn't found in the data.

    Returns:
        A string containing the rendered template.
    """
    # If the template is a sequence but not derived from a string
    if isinstance(template, Sequence) and not isinstance(template, str):
        # Then we don't need to tokenize it
        # But it does need to be a generator
        tokens: Iterator[tuple[str, str]] = (token for token in template)
    elif template in g_token_cache:
        tokens = (token for token in g_token_cache[template])
    else:
        # Otherwise make a generator
        tokens = tokenize(template, def_ldel, def_rdel)

    output = ""

    if scopes is None:
        scopes = [data]

    # Run through the tokens
    for tag, key in tokens:
        # Set the current scope
        current_scope = scopes[0]

        # If we're an end tag
        if tag == "end":
            # Pop out of the latest scope
            del scopes[0]

        # If the current scope is falsy and not the only scope
        elif not current_scope and len(scopes) != 1:
            if tag in {"section", "inverted section"}:
                # Set the most recent scope to a falsy value
                scopes.insert(0, False)

        # If we're a literal tag
        elif tag == "literal":
            # Add padding to the key and add it to the output
            output += key.replace("\n", "\n" + padding)

        # If we're a variable tag
        elif tag == "variable":
            # Add the html escaped key to the output
            thing = _get_key(
                key, scopes, warn=warn, keep=keep, def_ldel=def_ldel, def_rdel=def_rdel
            )

Domain

Subdomains

Frequently Asked Questions

What does render() do?
render() is a function in the langchain codebase, defined in libs/core/langchain_core/utils/mustache.py.
Where is render() defined?
render() is defined in libs/core/langchain_core/utils/mustache.py at line 466.
What does render() call?
render() calls 4 function(s): _get_key, _get_partial, _html_escape, tokenize.

Analyze Your Own Codebase

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

Try Supermodel Free