Home / Function/ from_template() — langchain Function Reference

from_template() — langchain Function Reference

Architecture documentation for the from_template() function in prompt.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  6328e53b_3705_ca29_5da3_cc71fb6aca10["from_template()"]
  b38b43f8_5be7_5b6c_b01e_36ed41e70435["PromptTemplate"]
  6328e53b_3705_ca29_5da3_cc71fb6aca10 -->|defined in| b38b43f8_5be7_5b6c_b01e_36ed41e70435
  21dc52b9_c2ed_f1ec_e44f_ef2212ac4429["__add__()"]
  21dc52b9_c2ed_f1ec_e44f_ef2212ac4429 -->|calls| 6328e53b_3705_ca29_5da3_cc71fb6aca10
  da847412_f35f_de5d_ee7c_9ad7d2609c59["from_file()"]
  da847412_f35f_de5d_ee7c_9ad7d2609c59 -->|calls| 6328e53b_3705_ca29_5da3_cc71fb6aca10
  style 6328e53b_3705_ca29_5da3_cc71fb6aca10 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/prompts/prompt.py lines 257–312

    def from_template(
        cls,
        template: str,
        *,
        template_format: PromptTemplateFormat = "f-string",
        partial_variables: dict[str, Any] | None = None,
        **kwargs: Any,
    ) -> PromptTemplate:
        """Load a prompt template from a template.

        !!! warning "Security"

            Prefer using `template_format='f-string'` instead of
            `template_format='jinja2'`, or make sure to NEVER accept jinja2 templates
            from untrusted sources as they may lead to arbitrary Python code execution.

            As of LangChain 0.0.329, Jinja2 templates will be rendered using Jinja2's
            SandboxedEnvironment by default. This sand-boxing should be treated as a
            best-effort approach rather than a guarantee of security, as it is an
            opt-out rather than opt-in approach.

            Despite the sandboxing, we recommend to never use jinja2 templates from
            untrusted sources.

        Args:
            template: The template to load.
            template_format: The format of the template.

                Use `jinja2` for jinja2, `mustache` for mustache, and `f-string` for
                f-strings.
            partial_variables: A dictionary of variables that can be used to partially
                fill in the template.

                For example, if the template is `'{variable1} {variable2}'`, and
                `partial_variables` is `{"variable1": "foo"}`, then the final prompt
                will be `'foo {variable2}'`.
            **kwargs: Any other arguments to pass to the prompt template.

        Returns:
            The prompt template loaded from the template.
        """
        input_variables = get_template_variables(template, template_format)
        partial_variables_ = partial_variables or {}

        if partial_variables_:
            input_variables = [
                var for var in input_variables if var not in partial_variables_
            ]

        return cls(
            input_variables=input_variables,
            template=template,
            template_format=template_format,
            partial_variables=partial_variables_,
            **kwargs,
        )

Domain

Subdomains

Frequently Asked Questions

What does from_template() do?
from_template() is a function in the langchain codebase, defined in libs/core/langchain_core/prompts/prompt.py.
Where is from_template() defined?
from_template() is defined in libs/core/langchain_core/prompts/prompt.py at line 257.
What calls from_template()?
from_template() is called by 2 function(s): __add__, from_file.

Analyze Your Own Codebase

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

Try Supermodel Free