Home / Class/ StringPromptTemplate Class — langchain Architecture

StringPromptTemplate Class — langchain Architecture

Architecture documentation for the StringPromptTemplate class in string.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  a3097815_cadb_7ebc_b29a_72117ccf6e27["StringPromptTemplate"]
  dddce6a3_2420_c71d_01fe_f214c3eb2503["BasePromptTemplate"]
  a3097815_cadb_7ebc_b29a_72117ccf6e27 -->|extends| dddce6a3_2420_c71d_01fe_f214c3eb2503
  a7a9f16f_a913_8e85_a792_d083dd92c428["string.py"]
  a3097815_cadb_7ebc_b29a_72117ccf6e27 -->|defined in| a7a9f16f_a913_8e85_a792_d083dd92c428
  2d750c68_62fe_8ad6_0681_a0f78d9317f3["get_lc_namespace()"]
  a3097815_cadb_7ebc_b29a_72117ccf6e27 -->|method| 2d750c68_62fe_8ad6_0681_a0f78d9317f3
  387c2349_0d2e_c313_bc73_fac3da07811e["format_prompt()"]
  a3097815_cadb_7ebc_b29a_72117ccf6e27 -->|method| 387c2349_0d2e_c313_bc73_fac3da07811e
  7c2e93d1_551b_0323_4cea_22ee59d7b66b["aformat_prompt()"]
  a3097815_cadb_7ebc_b29a_72117ccf6e27 -->|method| 7c2e93d1_551b_0323_4cea_22ee59d7b66b
  f2111c77_3867_1c07_6d6d_01dfad0cc994["format()"]
  a3097815_cadb_7ebc_b29a_72117ccf6e27 -->|method| f2111c77_3867_1c07_6d6d_01dfad0cc994
  51f6ea54_b5f1_f3c0_e18c_2e8f1d74f2a8["pretty_repr()"]
  a3097815_cadb_7ebc_b29a_72117ccf6e27 -->|method| 51f6ea54_b5f1_f3c0_e18c_2e8f1d74f2a8
  02fe17e5_5037_fda4_3044_50c8e47385c5["pretty_print()"]
  a3097815_cadb_7ebc_b29a_72117ccf6e27 -->|method| 02fe17e5_5037_fda4_3044_50c8e47385c5

Relationship Graph

Source Code

libs/core/langchain_core/prompts/string.py lines 309–371

class StringPromptTemplate(BasePromptTemplate, ABC):
    """String prompt that exposes the format method, returning a prompt."""

    @classmethod
    def get_lc_namespace(cls) -> list[str]:
        """Get the namespace of the LangChain object.

        Returns:
            `["langchain", "prompts", "base"]`
        """
        return ["langchain", "prompts", "base"]

    def format_prompt(self, **kwargs: Any) -> PromptValue:
        """Format the prompt with the inputs.

        Args:
            **kwargs: Any arguments to be passed to the prompt template.

        Returns:
            A formatted string.
        """
        return StringPromptValue(text=self.format(**kwargs))

    async def aformat_prompt(self, **kwargs: Any) -> PromptValue:
        """Async format the prompt with the inputs.

        Args:
            **kwargs: Any arguments to be passed to the prompt template.

        Returns:
            A formatted string.
        """
        return StringPromptValue(text=await self.aformat(**kwargs))

    @override
    @abstractmethod
    def format(self, **kwargs: Any) -> str: ...

    def pretty_repr(
        self,
        html: bool = False,  # noqa: FBT001,FBT002
    ) -> str:
        """Get a pretty representation of the prompt.

        Args:
            html: Whether to return an HTML-formatted string.

        Returns:
            A pretty representation of the prompt.
        """
        # TODO: handle partials
        dummy_vars = {
            input_var: "{" + f"{input_var}" + "}" for input_var in self.input_variables
        }
        if html:
            dummy_vars = {
                k: get_colored_text(v, "yellow") for k, v in dummy_vars.items()
            }
        return self.format(**dummy_vars)

    def pretty_print(self) -> None:
        """Print a pretty representation of the prompt."""
        print(self.pretty_repr(html=is_interactive_env()))  # noqa: T201

Frequently Asked Questions

What is the StringPromptTemplate class?
StringPromptTemplate is a class in the langchain codebase, defined in libs/core/langchain_core/prompts/string.py.
Where is StringPromptTemplate defined?
StringPromptTemplate is defined in libs/core/langchain_core/prompts/string.py at line 309.
What does StringPromptTemplate extend?
StringPromptTemplate extends BasePromptTemplate.

Analyze Your Own Codebase

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

Try Supermodel Free