Home / Class/ BasePromptTemplate Class — langchain Architecture

BasePromptTemplate Class — langchain Architecture

Architecture documentation for the BasePromptTemplate class in base.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  dddce6a3_2420_c71d_01fe_f214c3eb2503["BasePromptTemplate"]
  66049afc_0602_fbed_73fa_20a4904169e2["base.py"]
  dddce6a3_2420_c71d_01fe_f214c3eb2503 -->|defined in| 66049afc_0602_fbed_73fa_20a4904169e2
  d0343daa_9ffe_cb93_0bbc_1c193fa3e3d1["validate_variable_names()"]
  dddce6a3_2420_c71d_01fe_f214c3eb2503 -->|method| d0343daa_9ffe_cb93_0bbc_1c193fa3e3d1
  b685b693_653a_5d54_bc80_d2807ac390ca["get_lc_namespace()"]
  dddce6a3_2420_c71d_01fe_f214c3eb2503 -->|method| b685b693_653a_5d54_bc80_d2807ac390ca
  a353b33d_88a1_071b_e1cd_e7454fa4d182["is_lc_serializable()"]
  dddce6a3_2420_c71d_01fe_f214c3eb2503 -->|method| a353b33d_88a1_071b_e1cd_e7454fa4d182
  76b8d554_a278_a15f_2840_e9d38c145e75["_serialized()"]
  dddce6a3_2420_c71d_01fe_f214c3eb2503 -->|method| 76b8d554_a278_a15f_2840_e9d38c145e75
  a08ac98c_f933_d6ed_ffd9_cc44a20e7044["OutputType()"]
  dddce6a3_2420_c71d_01fe_f214c3eb2503 -->|method| a08ac98c_f933_d6ed_ffd9_cc44a20e7044
  fccdd4c7_614e_f914_5cb4_a1d21fc06c86["get_input_schema()"]
  dddce6a3_2420_c71d_01fe_f214c3eb2503 -->|method| fccdd4c7_614e_f914_5cb4_a1d21fc06c86
  d0875090_5168_ab1c_b4a1_6a5a1c0f0e82["_validate_input()"]
  dddce6a3_2420_c71d_01fe_f214c3eb2503 -->|method| d0875090_5168_ab1c_b4a1_6a5a1c0f0e82
  7f533acd_fecc_61f7_633f_d2e92168a487["_format_prompt_with_error_handling()"]
  dddce6a3_2420_c71d_01fe_f214c3eb2503 -->|method| 7f533acd_fecc_61f7_633f_d2e92168a487
  4a9a3c18_1b3a_b844_3033_a42d8d660a0c["_aformat_prompt_with_error_handling()"]
  dddce6a3_2420_c71d_01fe_f214c3eb2503 -->|method| 4a9a3c18_1b3a_b844_3033_a42d8d660a0c
  cb452c76_8a94_ba42_0e22_9ad2f706a15f["invoke()"]
  dddce6a3_2420_c71d_01fe_f214c3eb2503 -->|method| cb452c76_8a94_ba42_0e22_9ad2f706a15f
  329000e1_4342_ec8c_5ebc_2857ae4d8234["ainvoke()"]
  dddce6a3_2420_c71d_01fe_f214c3eb2503 -->|method| 329000e1_4342_ec8c_5ebc_2857ae4d8234
  0312f1bf_408e_5989_3fb2_27409c276e7d["format_prompt()"]
  dddce6a3_2420_c71d_01fe_f214c3eb2503 -->|method| 0312f1bf_408e_5989_3fb2_27409c276e7d
  dd903a3e_634e_a0d7_96df_4fbdcf96fe2f["aformat_prompt()"]
  dddce6a3_2420_c71d_01fe_f214c3eb2503 -->|method| dd903a3e_634e_a0d7_96df_4fbdcf96fe2f

Relationship Graph

Source Code

libs/core/langchain_core/prompts/base.py lines 39–393

class BasePromptTemplate(
    RunnableSerializable[dict, PromptValue], ABC, Generic[FormatOutputType]
):
    """Base class for all prompt templates, returning a prompt."""

    input_variables: list[str]
    """A list of the names of the variables whose values are required as inputs to the
    prompt.
    """

    optional_variables: list[str] = Field(default=[])
    """A list of the names of the variables for placeholder or `MessagePlaceholder` that
    are optional.

    These variables are auto inferred from the prompt and user need not provide them.
    """

    input_types: builtins.dict[str, Any] = Field(default_factory=dict, exclude=True)
    """A dictionary of the types of the variables the prompt template expects.

    If not provided, all variables are assumed to be strings.
    """

    output_parser: BaseOutputParser | None = None
    """How to parse the output of calling an LLM on this formatted prompt."""

    partial_variables: Mapping[str, Any] = Field(default_factory=dict)
    """A dictionary of the partial variables the prompt template carries.

    Partial variables populate the template so that you don't need to pass them in every
    time you call the prompt.
    """

    metadata: builtins.dict[str, Any] | None = None
    """Metadata to be used for tracing."""

    tags: list[str] | None = None
    """Tags to be used for tracing."""

    @model_validator(mode="after")
    def validate_variable_names(self) -> Self:
        """Validate variable names do not include restricted names."""
        if "stop" in self.input_variables:
            msg = (
                "Cannot have an input variable named 'stop', as it is used internally,"
                " please rename."
            )
            raise ValueError(
                create_message(message=msg, error_code=ErrorCode.INVALID_PROMPT_INPUT)
            )
        if "stop" in self.partial_variables:
            msg = (
                "Cannot have an partial variable named 'stop', as it is used "
                "internally, please rename."
            )
            raise ValueError(
                create_message(message=msg, error_code=ErrorCode.INVALID_PROMPT_INPUT)
            )

        overall = set(self.input_variables).intersection(self.partial_variables)
        if overall:
            msg = f"Found overlapping input and partial variables: {overall}"
            raise ValueError(
                create_message(message=msg, error_code=ErrorCode.INVALID_PROMPT_INPUT)
            )
        return self

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

        Returns:
            `["langchain", "schema", "prompt_template"]`
        """
        return ["langchain", "schema", "prompt_template"]

    @classmethod
    def is_lc_serializable(cls) -> bool:
        """Return `True` as this class is serializable."""
        return True

Frequently Asked Questions

What is the BasePromptTemplate class?
BasePromptTemplate is a class in the langchain codebase, defined in libs/core/langchain_core/prompts/base.py.
Where is BasePromptTemplate defined?
BasePromptTemplate is defined in libs/core/langchain_core/prompts/base.py at line 39.

Analyze Your Own Codebase

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

Try Supermodel Free