get_template_variables() — langchain Function Reference
Architecture documentation for the get_template_variables() function in string.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD f102027d_2a56_058a_1cf8_8ec4c6f8a103["get_template_variables()"] a7a9f16f_a913_8e85_a792_d083dd92c428["string.py"] f102027d_2a56_058a_1cf8_8ec4c6f8a103 -->|defined in| a7a9f16f_a913_8e85_a792_d083dd92c428 f102027d_2a56_058a_1cf8_8ec4c6f8a103["get_template_variables()"] f102027d_2a56_058a_1cf8_8ec4c6f8a103 -->|calls| f102027d_2a56_058a_1cf8_8ec4c6f8a103 f102027d_2a56_058a_1cf8_8ec4c6f8a103["get_template_variables()"] f102027d_2a56_058a_1cf8_8ec4c6f8a103 -->|calls| f102027d_2a56_058a_1cf8_8ec4c6f8a103 f53efb83_eef2_e36c_1360_c5fe4961e401["_get_jinja2_variables_from_template()"] f102027d_2a56_058a_1cf8_8ec4c6f8a103 -->|calls| f53efb83_eef2_e36c_1360_c5fe4961e401 8a2cca12_2ece_1825_717e_3fd839530d4d["mustache_template_vars()"] f102027d_2a56_058a_1cf8_8ec4c6f8a103 -->|calls| 8a2cca12_2ece_1825_717e_3fd839530d4d style f102027d_2a56_058a_1cf8_8ec4c6f8a103 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/core/langchain_core/prompts/string.py lines 254–306
def get_template_variables(template: str, template_format: str) -> list[str]:
"""Get the variables from the template.
Args:
template: The template string.
template_format: The template format.
Should be one of `'f-string'`, `'mustache'` or `'jinja2'`.
Returns:
The variables from the template.
Raises:
ValueError: If the template format is not supported.
"""
if template_format == "jinja2":
# Get the variables for the template
input_variables = _get_jinja2_variables_from_template(template)
elif template_format == "f-string":
input_variables = {
v for _, v, _, _ in Formatter().parse(template) if v is not None
}
elif template_format == "mustache":
input_variables = mustache_template_vars(template)
else:
msg = f"Unsupported template format: {template_format}"
raise ValueError(msg)
# For f-strings, block attribute access and indexing syntax
# This prevents template injection attacks via accessing dangerous attributes
if template_format == "f-string":
for var in input_variables:
# Formatter().parse() returns field names with dots/brackets if present
# e.g., "obj.attr" or "obj[0]" - we need to block these
if "." in var or "[" in var or "]" in var:
msg = (
f"Invalid variable name {var!r} in f-string template. "
f"Variable names cannot contain attribute "
f"access (.) or indexing ([])."
)
raise ValueError(msg)
# Block variable names that are all digits (e.g., "0", "100")
# These are interpreted as positional arguments, not keyword arguments
if var.isdigit():
msg = (
f"Invalid variable name {var!r} in f-string template. "
f"Variable names cannot be all digits as they are interpreted "
f"as positional arguments."
)
raise ValueError(msg)
return sorted(input_variables)
Domain
Subdomains
Defined In
Called By
Source
Frequently Asked Questions
What does get_template_variables() do?
get_template_variables() is a function in the langchain codebase, defined in libs/core/langchain_core/prompts/string.py.
Where is get_template_variables() defined?
get_template_variables() is defined in libs/core/langchain_core/prompts/string.py at line 254.
What does get_template_variables() call?
get_template_variables() calls 3 function(s): _get_jinja2_variables_from_template, get_template_variables, mustache_template_vars.
What calls get_template_variables()?
get_template_variables() is called by 1 function(s): get_template_variables.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free