Home / Function/ mustache_schema() — langchain Function Reference

mustache_schema() — langchain Function Reference

Architecture documentation for the mustache_schema() function in string.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  27b1f377_8df9_f5f4_a42b_75be2da0e4b9["mustache_schema()"]
  a7a9f16f_a913_8e85_a792_d083dd92c428["string.py"]
  27b1f377_8df9_f5f4_a42b_75be2da0e4b9 -->|defined in| a7a9f16f_a913_8e85_a792_d083dd92c428
  27b1f377_8df9_f5f4_a42b_75be2da0e4b9["mustache_schema()"]
  27b1f377_8df9_f5f4_a42b_75be2da0e4b9 -->|calls| 27b1f377_8df9_f5f4_a42b_75be2da0e4b9
  27b1f377_8df9_f5f4_a42b_75be2da0e4b9["mustache_schema()"]
  27b1f377_8df9_f5f4_a42b_75be2da0e4b9 -->|calls| 27b1f377_8df9_f5f4_a42b_75be2da0e4b9
  40a748dd_d17d_601e_bc46_b52640ccfdb2["is_subsequence()"]
  27b1f377_8df9_f5f4_a42b_75be2da0e4b9 -->|calls| 40a748dd_d17d_601e_bc46_b52640ccfdb2
  1ecd7eac_7aa7_00a4_c9fd_c01fd8e19cde["_create_model_recursive()"]
  27b1f377_8df9_f5f4_a42b_75be2da0e4b9 -->|calls| 1ecd7eac_7aa7_00a4_c9fd_c01fd8e19cde
  style 27b1f377_8df9_f5f4_a42b_75be2da0e4b9 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/prompts/string.py lines 156–192

def mustache_schema(template: str) -> type[BaseModel]:
    """Get the variables from a mustache template.

    Args:
        template: The template string.

    Returns:
        The variables from the template as a Pydantic model.
    """
    fields = {}
    prefix: tuple[str, ...] = ()
    section_stack: list[tuple[str, ...]] = []
    for type_, key in mustache.tokenize(template):
        if key == ".":
            continue
        if type_ == "end":
            if section_stack:
                prefix = section_stack.pop()
        elif type_ in {"section", "inverted section"}:
            section_stack.append(prefix)
            prefix += tuple(key.split("."))
            fields[prefix] = False
        elif type_ in {"variable", "no escape"}:
            fields[prefix + tuple(key.split("."))] = True

    for fkey, fval in fields.items():
        fields[fkey] = fval and not any(
            is_subsequence(fkey, k) for k in fields if k != fkey
        )
    defs: Defs = {}  # None means leaf node
    while fields:
        field, is_leaf = fields.popitem()
        current = defs
        for part in field[:-1]:
            current = current.setdefault(part, {})
        current.setdefault(field[-1], "" if is_leaf else {})  # type: ignore[arg-type]
    return _create_model_recursive("PromptInput", defs)

Subdomains

Called By

Frequently Asked Questions

What does mustache_schema() do?
mustache_schema() is a function in the langchain codebase, defined in libs/core/langchain_core/prompts/string.py.
Where is mustache_schema() defined?
mustache_schema() is defined in libs/core/langchain_core/prompts/string.py at line 156.
What does mustache_schema() call?
mustache_schema() calls 3 function(s): _create_model_recursive, is_subsequence, mustache_schema.
What calls mustache_schema()?
mustache_schema() is called by 1 function(s): mustache_schema.

Analyze Your Own Codebase

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

Try Supermodel Free