string.py — langchain Source File
Architecture documentation for string.py, a python file in the langchain codebase. 14 imports, 1 dependents.
Entity Profile
Dependency Diagram
graph LR a7a9f16f_a913_8e85_a792_d083dd92c428["string.py"] 0c635125_6987_b8b3_7ff7_d60249aecde7["warnings"] a7a9f16f_a913_8e85_a792_d083dd92c428 --> 0c635125_6987_b8b3_7ff7_d60249aecde7 cccbe73e_4644_7211_4d55_e8fb133a8014["abc"] a7a9f16f_a913_8e85_a792_d083dd92c428 --> cccbe73e_4644_7211_4d55_e8fb133a8014 a7a9f16f_a913_8e85_a792_d083dd92c428["string.py"] a7a9f16f_a913_8e85_a792_d083dd92c428 --> a7a9f16f_a913_8e85_a792_d083dd92c428 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"] a7a9f16f_a913_8e85_a792_d083dd92c428 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3 6e58aaea_f08e_c099_3cc7_f9567bfb1ae7["pydantic"] a7a9f16f_a913_8e85_a792_d083dd92c428 --> 6e58aaea_f08e_c099_3cc7_f9567bfb1ae7 91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"] a7a9f16f_a913_8e85_a792_d083dd92c428 --> 91721f45_4909_e489_8c1f_084f8bd87145 5b417886_56dd_6afa_13ab_a3cfc1dbcccd["langchain_core.prompt_values"] a7a9f16f_a913_8e85_a792_d083dd92c428 --> 5b417886_56dd_6afa_13ab_a3cfc1dbcccd 8c01ac33_5694_26d8_6f82_8267af524e4e["langchain_core.prompts.base"] a7a9f16f_a913_8e85_a792_d083dd92c428 --> 8c01ac33_5694_26d8_6f82_8267af524e4e f4d905c6_a2b2_eb8f_be9b_7808b72f6a16["langchain_core.utils"] a7a9f16f_a913_8e85_a792_d083dd92c428 --> f4d905c6_a2b2_eb8f_be9b_7808b72f6a16 5d3a2459_089e_5f20_61eb_223262657e65["langchain_core.utils.formatting"] a7a9f16f_a913_8e85_a792_d083dd92c428 --> 5d3a2459_089e_5f20_61eb_223262657e65 c98d597d_2ec1_88dc_2aec_af377c69a987["langchain_core.utils.interactive_env"] a7a9f16f_a913_8e85_a792_d083dd92c428 --> c98d597d_2ec1_88dc_2aec_af377c69a987 cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7["collections.abc"] a7a9f16f_a913_8e85_a792_d083dd92c428 --> cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7 858617cd_fc13_7477_4543_9f493e7473e0["jinja2"] a7a9f16f_a913_8e85_a792_d083dd92c428 --> 858617cd_fc13_7477_4543_9f493e7473e0 193e9163_2274_f907_eaf9_d148d274adee["jinja2.sandbox"] a7a9f16f_a913_8e85_a792_d083dd92c428 --> 193e9163_2274_f907_eaf9_d148d274adee a7a9f16f_a913_8e85_a792_d083dd92c428["string.py"] a7a9f16f_a913_8e85_a792_d083dd92c428 --> a7a9f16f_a913_8e85_a792_d083dd92c428 style a7a9f16f_a913_8e85_a792_d083dd92c428 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
"""`BasePrompt` schema definition."""
from __future__ import annotations
import warnings
from abc import ABC, abstractmethod
from string import Formatter
from typing import TYPE_CHECKING, Any, Literal, cast
from pydantic import BaseModel, create_model
from typing_extensions import override
from langchain_core.prompt_values import PromptValue, StringPromptValue
from langchain_core.prompts.base import BasePromptTemplate
from langchain_core.utils import get_colored_text, mustache
from langchain_core.utils.formatting import formatter
from langchain_core.utils.interactive_env import is_interactive_env
if TYPE_CHECKING:
from collections.abc import Callable, Sequence
try:
from jinja2 import meta
from jinja2.sandbox import SandboxedEnvironment
_HAS_JINJA2 = True
except ImportError:
_HAS_JINJA2 = False
PromptTemplateFormat = Literal["f-string", "mustache", "jinja2"]
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`."
// ... (321 more lines)
Domain
Subdomains
Functions
Classes
Dependencies
- abc
- collections.abc
- jinja2
- jinja2.sandbox
- langchain_core.prompt_values
- langchain_core.prompts.base
- langchain_core.utils
- langchain_core.utils.formatting
- langchain_core.utils.interactive_env
- pydantic
- string.py
- typing
- typing_extensions
- warnings
Imported By
Source
Frequently Asked Questions
What does string.py do?
string.py is a source file in the langchain codebase, written in python. It belongs to the PromptManagement domain, ExampleSelection subdomain.
What functions are defined in string.py?
string.py defines 13 function(s): _HAS_JINJA2, _create_model_recursive, _get_jinja2_variables_from_template, check_valid_template, collections, get_template_variables, is_subsequence, jinja2, jinja2_formatter, mustache_formatter, and 3 more.
What does string.py depend on?
string.py imports 14 module(s): abc, collections.abc, jinja2, jinja2.sandbox, langchain_core.prompt_values, langchain_core.prompts.base, langchain_core.utils, langchain_core.utils.formatting, and 6 more.
What files import string.py?
string.py is imported by 1 file(s): string.py.
Where is string.py in the architecture?
string.py is located at libs/core/langchain_core/prompts/string.py (domain: PromptManagement, subdomain: ExampleSelection, directory: libs/core/langchain_core/prompts).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free