Home / Function/ from_template() — langchain Function Reference

from_template() — langchain Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  48373769_1ea6_b4a1_72e7_07082679631b["from_template()"]
  894611e5_5039_7621_4673_821d0b9b6f17["_StringImageMessagePromptTemplate"]
  48373769_1ea6_b4a1_72e7_07082679631b -->|defined in| 894611e5_5039_7621_4673_821d0b9b6f17
  6966af16_ade2_de3e_4f06_f97eca43d8e0["from_template()"]
  6966af16_ade2_de3e_4f06_f97eca43d8e0 -->|calls| 48373769_1ea6_b4a1_72e7_07082679631b
  51e4c8c4_03c1_4bf3_6d35_36e10545e164["append()"]
  48373769_1ea6_b4a1_72e7_07082679631b -->|calls| 51e4c8c4_03c1_4bf3_6d35_36e10545e164
  a7addf49_35b7_d70e_edec_9401bf339038["extend()"]
  48373769_1ea6_b4a1_72e7_07082679631b -->|calls| a7addf49_35b7_d70e_edec_9401bf339038
  6966af16_ade2_de3e_4f06_f97eca43d8e0["from_template()"]
  48373769_1ea6_b4a1_72e7_07082679631b -->|calls| 6966af16_ade2_de3e_4f06_f97eca43d8e0
  style 48373769_1ea6_b4a1_72e7_07082679631b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/prompts/chat.py lines 410–529

    def from_template(
        cls: type[Self],
        template: str
        | list[str | _TextTemplateParam | _ImageTemplateParam | dict[str, Any]],
        template_format: PromptTemplateFormat = "f-string",
        *,
        partial_variables: dict[str, Any] | None = None,
        **kwargs: Any,
    ) -> Self:
        """Create a class from a string template.

        Args:
            template: a template.
            template_format: format of the template.

                Options are: `'f-string'`, `'mustache'`, `'jinja2'`.
            partial_variables: A dictionary of variables that can be used too partially.

            **kwargs: Keyword arguments to pass to the constructor.

        Returns:
            A new instance of this class.

        Raises:
            ValueError: If the template is not a string or list of strings.
        """
        if isinstance(template, str):
            prompt: StringPromptTemplate | list = PromptTemplate.from_template(
                template,
                template_format=template_format,
                partial_variables=partial_variables,
            )
            return cls(prompt=prompt, **kwargs)
        if isinstance(template, list):
            if (partial_variables is not None) and len(partial_variables) > 0:
                msg = "Partial variables are not supported for list of templates."
                raise ValueError(msg)
            prompt = []
            for tmpl in template:
                if isinstance(tmpl, str) or (
                    isinstance(tmpl, dict)
                    and "text" in tmpl
                    and set(tmpl.keys()) <= {"type", "text"}
                ):
                    if isinstance(tmpl, str):
                        text: str = tmpl
                    else:
                        text = cast("_TextTemplateParam", tmpl)["text"]  # type: ignore[assignment]
                    prompt.append(
                        PromptTemplate.from_template(
                            text, template_format=template_format
                        )
                    )
                elif (
                    isinstance(tmpl, dict)
                    and "image_url" in tmpl
                    and set(tmpl.keys())
                    <= {
                        "type",
                        "image_url",
                    }
                ):
                    img_template = cast("_ImageTemplateParam", tmpl)["image_url"]
                    input_variables = []
                    if isinstance(img_template, str):
                        variables = get_template_variables(
                            img_template, template_format
                        )
                        if variables:
                            if len(variables) > 1:
                                msg = (
                                    "Only one format variable allowed per image"
                                    f" template.\nGot: {variables}"
                                    f"\nFrom: {tmpl}"
                                )
                                raise ValueError(msg)
                            input_variables = [variables[0]]
                        img_template = {"url": img_template}
                        img_template_obj = ImagePromptTemplate(
                            input_variables=input_variables,
                            template=img_template,

Subdomains

Called By

Frequently Asked Questions

What does from_template() do?
from_template() is a function in the langchain codebase, defined in libs/core/langchain_core/prompts/chat.py.
Where is from_template() defined?
from_template() is defined in libs/core/langchain_core/prompts/chat.py at line 410.
What does from_template() call?
from_template() calls 3 function(s): append, extend, from_template.
What calls from_template()?
from_template() is called by 1 function(s): from_template.

Analyze Your Own Codebase

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

Try Supermodel Free