jinja2_formatter() — langchain Function Reference
Architecture documentation for the jinja2_formatter() function in string.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 43cc29b7_e3fc_e3fc_42e0_3e366fa58a5e["jinja2_formatter()"] a7a9f16f_a913_8e85_a792_d083dd92c428["string.py"] 43cc29b7_e3fc_e3fc_42e0_3e366fa58a5e -->|defined in| a7a9f16f_a913_8e85_a792_d083dd92c428 43cc29b7_e3fc_e3fc_42e0_3e366fa58a5e["jinja2_formatter()"] 43cc29b7_e3fc_e3fc_42e0_3e366fa58a5e -->|calls| 43cc29b7_e3fc_e3fc_42e0_3e366fa58a5e 43cc29b7_e3fc_e3fc_42e0_3e366fa58a5e["jinja2_formatter()"] 43cc29b7_e3fc_e3fc_42e0_3e366fa58a5e -->|calls| 43cc29b7_e3fc_e3fc_42e0_3e366fa58a5e style 43cc29b7_e3fc_e3fc_42e0_3e366fa58a5e fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/core/langchain_core/prompts/string.py lines 33–70
def jinja2_formatter(template: str, /, **kwargs: Any) -> str:
"""Format a template using jinja2.
!!! warning "Security"
As of LangChain 0.0.329, this method uses Jinja2's `SandboxedEnvironment` by
default. However, this sandboxing should be treated as a best-effort approach
rather than a guarantee of security.
Do not accept jinja2 templates from untrusted sources as they may lead
to arbitrary Python code execution.
[More information.](https://jinja.palletsprojects.com/en/3.1.x/sandbox/)
Args:
template: The template string.
**kwargs: The variables to format the template with.
Returns:
The formatted string.
Raises:
ImportError: If jinja2 is not installed.
"""
if not _HAS_JINJA2:
msg = (
"jinja2 not installed, which is needed to use the jinja2_formatter. "
"Please install it with `pip install jinja2`."
"Please be cautious when using jinja2 templates. "
"Do not expand jinja2 templates using unverified or user-controlled "
"inputs as that can result in arbitrary Python code execution."
)
raise ImportError(msg)
# Use a restricted sandbox that blocks ALL attribute/method access
# Only simple variable lookups like {{variable}} are allowed
# Attribute access like {{variable.attr}} or {{variable.method()}} is blocked
return SandboxedEnvironment().from_string(template).render(**kwargs)
Domain
Subdomains
Defined In
Calls
Called By
Source
Frequently Asked Questions
What does jinja2_formatter() do?
jinja2_formatter() is a function in the langchain codebase, defined in libs/core/langchain_core/prompts/string.py.
Where is jinja2_formatter() defined?
jinja2_formatter() is defined in libs/core/langchain_core/prompts/string.py at line 33.
What does jinja2_formatter() call?
jinja2_formatter() calls 1 function(s): jinja2_formatter.
What calls jinja2_formatter()?
jinja2_formatter() is called by 1 function(s): jinja2_formatter.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free