Open-Source Wikis

/

LangChain

/

Primitives

/

Prompts

langchain-ai/langchain

Prompts

Templating for prompts and chat conversations. Source: libs/core/langchain_core/prompts/.

Purpose

A PromptTemplate is a Runnable[dict, PromptValue]. It takes a dict of variables and returns a PromptValue that can be cast to a string (for completion-style models) or a list of messages (for chat models). Templates support partial substitution, few-shot example formatting, and image/multimodal content.

Directory layout

libs/core/langchain_core/prompts/
├── __init__.py
├── base.py                  # BasePromptTemplate, StringPromptTemplate
├── chat.py                  # ChatPromptTemplate (~50 KB)
├── dict.py                  # DictPromptTemplate
├── few_shot.py              # FewShotPromptTemplate, FewShotChatMessagePromptTemplate
├── few_shot_with_templates.py
├── image.py                 # ImagePromptTemplate (multimodal)
├── loading.py               # load_prompt(...)
├── message.py               # MessagePromptTemplate base
├── prompt.py                # PromptTemplate (the string template)
├── string.py                # StringPromptTemplate utilities (Mustache, f-string)
└── structured.py            # StructuredPrompt — pairs a template with a response schema

Key abstractions

Symbol File Description
BasePromptTemplate libs/core/langchain_core/prompts/base.py Common base; defines input_variables, partial_variables, format, format_prompt, partial(...)
PromptTemplate libs/core/langchain_core/prompts/prompt.py f-string / Mustache / Jinja2 string template
ChatPromptTemplate libs/core/langchain_core/prompts/chat.py List of MessagePromptTemplates; renders to a list of messages
MessagePromptTemplate (and subclasses SystemMessagePromptTemplate, HumanMessagePromptTemplate, AIMessagePromptTemplate, ChatMessagePromptTemplate) libs/core/langchain_core/prompts/chat.py Templates for individual messages
MessagesPlaceholder libs/core/langchain_core/prompts/chat.py Slot for inserting a list of pre-built messages (e.g. chat history)
FewShotPromptTemplate libs/core/langchain_core/prompts/few_shot.py Renders examples + a final question
FewShotChatMessagePromptTemplate libs/core/langchain_core/prompts/few_shot.py Few-shot variant for chat templates
ImagePromptTemplate libs/core/langchain_core/prompts/image.py Render an image content block from a URL/base64 template
DictPromptTemplate libs/core/langchain_core/prompts/dict.py Render a dict of fields into structured input
StructuredPrompt libs/core/langchain_core/prompts/structured.py Pair a prompt with a response schema (response_format)
load_prompt(path) libs/core/langchain_core/prompts/loading.py Load a prompt from JSON/YAML on disk or from the LangSmith Hub

How chat prompts work

from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant in {language}."),
    MessagesPlaceholder("history"),
    ("user", "{question}"),
])

messages = prompt.format_messages(
    language="French",
    history=[HumanMessage("Bonjour"), AIMessage("Bonjour!")],
    question="What is 2+2?",
)

from_messages accepts:

  • Tuples (role, template_str) — converted to SystemMessagePromptTemplate, HumanMessagePromptTemplate, AIMessagePromptTemplate based on role.
  • MessagesPlaceholder("name") — a slot for inserting a list of messages at format time.
  • Existing MessagePromptTemplate instances.
  • Plain BaseMessage instances (rendered as-is).

Template engines

PromptTemplate supports three template formats:

  • f-string (default) — Python's f-string syntax; variables are {name}.
  • Mustache{{name}}. Useful for templates that should not interpret { as syntax.
  • Jinja2 — full Jinja templating; opt in via template_format="jinja2".

The format is detected from template_format on construction; the base class uses the corresponding renderer.

Partial substitution

prompt.partial(language="French") returns a new prompt with language pre-filled. The remaining input_variables are exposed for downstream use. This is the canonical way to bake in a per-context value without fully rendering the prompt.

LCEL composition

PromptTemplate and ChatPromptTemplate both implement Runnable, so they pipe naturally:

chain = prompt | model | StrOutputParser()
chain.invoke({"language": "French", "question": "What is 2+2?"})

Loading prompts

load_prompt(path_or_uri) understands:

  • Local JSON or YAML files following the LangChain prompt schema.
  • LangSmith Hub URIs (hub:owner/prompt-name) — backed by langchain_classic.hub.pull for legacy code, but load_prompt is the preferred entry point.

This is how community-shared prompts get loaded.

Integration points

  • Every chain and agent uses prompts. ChatPromptTemplate is the most common starting point.
  • Few-shot retrievers (e.g. SemanticSimilarityExampleSelector in libs/core/langchain_core/example_selectors/) feed examples to FewShotPromptTemplate.
  • MessagesPlaceholder is the standard way to splice agent state's messages into a system prompt.

Entry points for modification

  • For a new template engine, extend string.py's renderers and add a template_format value.
  • For a new message type in chat prompts, extend MessagePromptTemplate in chat.py and register it in from_messages.
  • For multimodal templating, see image.py and the content-block taxonomy in primitives/messages.

Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.

Prompts – LangChain wiki | Factory