Home / Class/ ChatPromptTemplate Class — langchain Architecture

ChatPromptTemplate Class — langchain Architecture

Architecture documentation for the ChatPromptTemplate class in chat.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  6be4a9a5_5fe4_e64f_c374_e63767576bf6["ChatPromptTemplate"]
  93017235_36b7_3b85_820e_aa7d145183e5["BaseChatPromptTemplate"]
  6be4a9a5_5fe4_e64f_c374_e63767576bf6 -->|extends| 93017235_36b7_3b85_820e_aa7d145183e5
  c5f45b6e_9126_7fa3_ba5a_e6a86d45b6d5["MessagesPlaceholder"]
  6be4a9a5_5fe4_e64f_c374_e63767576bf6 -->|extends| c5f45b6e_9126_7fa3_ba5a_e6a86d45b6d5
  6be4a9a5_5fe4_e64f_c374_e63767576bf6["ChatPromptTemplate"]
  6be4a9a5_5fe4_e64f_c374_e63767576bf6 -->|extends| 6be4a9a5_5fe4_e64f_c374_e63767576bf6
  b9553aad_b797_0a7b_73ed_8d05b0819c0f["BaseMessage"]
  6be4a9a5_5fe4_e64f_c374_e63767576bf6 -->|extends| b9553aad_b797_0a7b_73ed_8d05b0819c0f
  d735cdf0_4dd6_b10d_3d65_7f753809639d["chat.py"]
  6be4a9a5_5fe4_e64f_c374_e63767576bf6 -->|defined in| d735cdf0_4dd6_b10d_3d65_7f753809639d
  fd4d40b2_9f02_c4c9_852d_d998c70f1d1e["__init__()"]
  6be4a9a5_5fe4_e64f_c374_e63767576bf6 -->|method| fd4d40b2_9f02_c4c9_852d_d998c70f1d1e
  9255f839_8785_36ad_3d8e_039f801e61cb["get_lc_namespace()"]
  6be4a9a5_5fe4_e64f_c374_e63767576bf6 -->|method| 9255f839_8785_36ad_3d8e_039f801e61cb
  05fbd76f_5319_5d90_fd32_8ae595f01924["__add__()"]
  6be4a9a5_5fe4_e64f_c374_e63767576bf6 -->|method| 05fbd76f_5319_5d90_fd32_8ae595f01924
  667da55a_6fd3_6435_1529_5240cb4dec4c["validate_input_variables()"]
  6be4a9a5_5fe4_e64f_c374_e63767576bf6 -->|method| 667da55a_6fd3_6435_1529_5240cb4dec4c
  fb7525a5_49cc_cee5_9a12_dffafec8f35b["from_template()"]
  6be4a9a5_5fe4_e64f_c374_e63767576bf6 -->|method| fb7525a5_49cc_cee5_9a12_dffafec8f35b
  868943e8_3763_c08b_6ee7_cd2801948c3a["from_messages()"]
  6be4a9a5_5fe4_e64f_c374_e63767576bf6 -->|method| 868943e8_3763_c08b_6ee7_cd2801948c3a
  a912d932_8157_994e_b37d_ed55e4821d34["format_messages()"]
  6be4a9a5_5fe4_e64f_c374_e63767576bf6 -->|method| a912d932_8157_994e_b37d_ed55e4821d34
  14f1d3b5_b98f_d9bc_03bd_848d5ef5cead["aformat_messages()"]
  6be4a9a5_5fe4_e64f_c374_e63767576bf6 -->|method| 14f1d3b5_b98f_d9bc_03bd_848d5ef5cead
  490adae9_6c8b_f224_cfeb_133ca6ea29c8["partial()"]
  6be4a9a5_5fe4_e64f_c374_e63767576bf6 -->|method| 490adae9_6c8b_f224_cfeb_133ca6ea29c8
  51e4c8c4_03c1_4bf3_6d35_36e10545e164["append()"]
  6be4a9a5_5fe4_e64f_c374_e63767576bf6 -->|method| 51e4c8c4_03c1_4bf3_6d35_36e10545e164

Relationship Graph

Source Code

libs/core/langchain_core/prompts/chat.py lines 789–1327

class ChatPromptTemplate(BaseChatPromptTemplate):
    """Prompt template for chat models.

    Use to create flexible templated prompts for chat models.

    !!! example

        ```python
        from langchain_core.prompts import ChatPromptTemplate

        template = ChatPromptTemplate(
            [
                ("system", "You are a helpful AI bot. Your name is {name}."),
                ("human", "Hello, how are you doing?"),
                ("ai", "I'm doing well, thanks!"),
                ("human", "{user_input}"),
            ]
        )

        prompt_value = template.invoke(
            {
                "name": "Bob",
                "user_input": "What is your name?",
            }
        )
        # Output:
        # ChatPromptValue(
        #    messages=[
        #        SystemMessage(content='You are a helpful AI bot. Your name is Bob.'),
        #        HumanMessage(content='Hello, how are you doing?'),
        #        AIMessage(content="I'm doing well, thanks!"),
        #        HumanMessage(content='What is your name?')
        #    ]
        # )
        ```

    !!! note "Messages Placeholder"

        ```python
        # In addition to Human/AI/Tool/Function messages,
        # you can initialize the template with a MessagesPlaceholder
        # either using the class directly or with the shorthand tuple syntax:

        template = ChatPromptTemplate(
            [
                ("system", "You are a helpful AI bot."),
                # Means the template will receive an optional list of messages under
                # the "conversation" key
                ("placeholder", "{conversation}"),
                # Equivalently:
                # MessagesPlaceholder(variable_name="conversation", optional=True)
            ]
        )

        prompt_value = template.invoke(
            {
                "conversation": [
                    ("human", "Hi!"),
                    ("ai", "How can I assist you today?"),
                    ("human", "Can you make me an ice cream sundae?"),
                    ("ai", "No."),
                ]
            }
        )

        # Output:
        # ChatPromptValue(
        #    messages=[
        #        SystemMessage(content='You are a helpful AI bot.'),
        #        HumanMessage(content='Hi!'),
        #        AIMessage(content='How can I assist you today?'),
        #        HumanMessage(content='Can you make me an ice cream sundae?'),
        #        AIMessage(content='No.'),
        #    ]
        # )
        ```

    !!! note "Single-variable template"

        If your prompt has only a single input variable (i.e., one instance of
        `'{variable_nams}'`), and you invoke the template with a non-dict object, the

Frequently Asked Questions

What is the ChatPromptTemplate class?
ChatPromptTemplate is a class in the langchain codebase, defined in libs/core/langchain_core/prompts/chat.py.
Where is ChatPromptTemplate defined?
ChatPromptTemplate is defined in libs/core/langchain_core/prompts/chat.py at line 789.
What does ChatPromptTemplate extend?
ChatPromptTemplate extends BaseChatPromptTemplate, MessagesPlaceholder, ChatPromptTemplate, BaseMessage.

Analyze Your Own Codebase

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

Try Supermodel Free