PromptTemplate Class — langchain Architecture
Architecture documentation for the PromptTemplate class in prompt.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 21bd3731_3333_8ace_7023_5dd43ed308cf["PromptTemplate"] a3097815_cadb_7ebc_b29a_72117ccf6e27["StringPromptTemplate"] 21bd3731_3333_8ace_7023_5dd43ed308cf -->|extends| a3097815_cadb_7ebc_b29a_72117ccf6e27 21bd3731_3333_8ace_7023_5dd43ed308cf["PromptTemplate"] 21bd3731_3333_8ace_7023_5dd43ed308cf -->|extends| 21bd3731_3333_8ace_7023_5dd43ed308cf 173da1b5_3918_9794_592e_b4158a7a631c["prompt.py"] 21bd3731_3333_8ace_7023_5dd43ed308cf -->|defined in| 173da1b5_3918_9794_592e_b4158a7a631c c0dc699c_04a2_c246_5d14_49a0a0368e13["lc_attributes()"] 21bd3731_3333_8ace_7023_5dd43ed308cf -->|method| c0dc699c_04a2_c246_5d14_49a0a0368e13 2a817e6e_53ea_2272_3482_b09b3b169eb8["get_lc_namespace()"] 21bd3731_3333_8ace_7023_5dd43ed308cf -->|method| 2a817e6e_53ea_2272_3482_b09b3b169eb8 ec831a8d_7cb6_22ac_1b03_cb779e44bd3e["pre_init_validation()"] 21bd3731_3333_8ace_7023_5dd43ed308cf -->|method| ec831a8d_7cb6_22ac_1b03_cb779e44bd3e a4033e6d_ebc2_247b_9581_71bdf4bc32c8["get_input_schema()"] 21bd3731_3333_8ace_7023_5dd43ed308cf -->|method| a4033e6d_ebc2_247b_9581_71bdf4bc32c8 f47e62b1_9bb6_9680_773a_88b17699f42c["__add__()"] 21bd3731_3333_8ace_7023_5dd43ed308cf -->|method| f47e62b1_9bb6_9680_773a_88b17699f42c 6b23bf24_bee9_4ad1_b9a3_5f9e340944c9["_prompt_type()"] 21bd3731_3333_8ace_7023_5dd43ed308cf -->|method| 6b23bf24_bee9_4ad1_b9a3_5f9e340944c9 2d4bc191_fb27_aca3_7fa8_c474bd7b9f71["format()"] 21bd3731_3333_8ace_7023_5dd43ed308cf -->|method| 2d4bc191_fb27_aca3_7fa8_c474bd7b9f71 22573c07_8b17_9077_9444_ee546b219e79["from_examples()"] 21bd3731_3333_8ace_7023_5dd43ed308cf -->|method| 22573c07_8b17_9077_9444_ee546b219e79 fd622af6_8679_bc46_296c_4242a6c8150b["from_file()"] 21bd3731_3333_8ace_7023_5dd43ed308cf -->|method| fd622af6_8679_bc46_296c_4242a6c8150b 5dd38bd6_87a8_f04c_534c_00fe5bafa00f["from_template()"] 21bd3731_3333_8ace_7023_5dd43ed308cf -->|method| 5dd38bd6_87a8_f04c_534c_00fe5bafa00f
Relationship Graph
Source Code
libs/core/langchain_core/prompts/prompt.py lines 24–312
class PromptTemplate(StringPromptTemplate):
"""Prompt template for a language model.
A prompt template consists of a string template. It accepts a set of parameters
from the user that can be used to generate a prompt for a language model.
The template can be formatted using either f-strings (default), jinja2, or mustache
syntax.
!!! 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.
Example:
```python
from langchain_core.prompts import PromptTemplate
# Instantiation using from_template (recommended)
prompt = PromptTemplate.from_template("Say {foo}")
prompt.format(foo="bar")
# Instantiation using initializer
prompt = PromptTemplate(template="Say {foo}")
```
"""
@property
@override
def lc_attributes(self) -> dict[str, Any]:
return {
"template_format": self.template_format,
}
@classmethod
@override
def get_lc_namespace(cls) -> list[str]:
"""Get the namespace of the LangChain object.
Returns:
`["langchain", "prompts", "prompt"]`
"""
return ["langchain", "prompts", "prompt"]
template: str
"""The prompt template."""
template_format: PromptTemplateFormat = "f-string"
"""The format of the prompt template.
Options are: `'f-string'`, `'mustache'`, `'jinja2'`.
"""
validate_template: bool = False
"""Whether or not to try validating the template."""
@model_validator(mode="before")
@classmethod
def pre_init_validation(cls, values: dict) -> Any:
"""Check that template and input variables are consistent."""
if values.get("template") is None:
# Will let pydantic fail with a ValidationError if template
# is not provided.
return values
# Set some default values based on the field defaults
values.setdefault("template_format", "f-string")
values.setdefault("partial_variables", {})
if values.get("validate_template"):
if values["template_format"] == "mustache":
msg = "Mustache templates cannot be validated."
Defined In
Source
Frequently Asked Questions
What is the PromptTemplate class?
PromptTemplate is a class in the langchain codebase, defined in libs/core/langchain_core/prompts/prompt.py.
Where is PromptTemplate defined?
PromptTemplate is defined in libs/core/langchain_core/prompts/prompt.py at line 24.
What does PromptTemplate extend?
PromptTemplate extends StringPromptTemplate, PromptTemplate.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free