You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I found strange inconsistency with ChatPromptTemplate with template_format='mustache', see attached code. I expected that placeholder formatting will use similar template format engine, but it remains f-string.
Test code assertion would fail, and only replacing {{messages}} with {messages} would fix it.
If this is intentionally, then probably it should be reflected in documentation, which is not currently.
Thank you.
System Info
System Information
OS: Linux
OS Version: #1 SMP Thu Feb 27 20:22:48 UTC 2020
Python Version: 3.9.6 (default, Feb 28 2022, 11:53:11)
[GCC 7.3.1 20180712 (Red Hat 7.3.1-9)]
aiohttp: 3.11.9
async-timeout: 4.0.3
dataclasses-json: 0.6.7
httpx: 0.28.0
httpx-sse: 0.4.0
jsonpatch<2.0,>=1.33: Installed. No version info available.
langsmith-pyo3: Installed. No version info available.
langsmith<0.4,>=0.1.125: Installed. No version info available.
numpy: 1.26.4
openai: 1.56.2
orjson: 3.10.12
packaging<25,>=23.2: Installed. No version info available.
pgvector: 0.2.5
psycopg: 3.2.3
psycopg-pool: 3.2.4
pydantic: 2.10.3
pydantic-settings: 2.4.0
pydantic<3.0.0,>=2.5.2;: Installed. No version info available.
pydantic<3.0.0,>=2.7.4;: Installed. No version info available.
PyYAML: 6.0.2
PyYAML>=5.3: Installed. No version info available.
requests: 2.32.3
requests-toolbelt: 1.0.0
sqlalchemy: 2.0.29
SQLAlchemy: 2.0.29
tenacity: 9.0.0
tenacity!=8.4.0,<10.0.0,>=8.1.0: Installed. No version info available.
tiktoken: 0.7.0
typing-extensions>=4.7: Installed. No version info available.
The text was updated successfully, but these errors were encountered:
dosubotbot
added
Ɑ: core
Related to langchain-core
🤖:bug
Related to a bug, vulnerability, unexpected error with an existing feature
labels
Mar 6, 2025
When using template_format='mustache', you would expect all variable placeholders to follow mustache syntax ({{variable}}), but the MessagesPlaceholder still expects the f-string format ({variable}).
Let's look at how to fix this:
Solution
You need to use {messages} for the placeholder variable name, even when the template format is set to mustache:
fromlangchain_core.promptsimportChatPromptTemplatefromlangchain_core.messagesimportAIMessage, HumanMessagedeftest_prompt_template_bug():
template=ChatPromptTemplate([
('system', ''),
('placeholder', '{messages}'), # Use f-string format here
('user', '{{user_input}}'), # Use mustache format here
], template_format='mustache')
prompt_value=template.invoke({
"user_input": "User input!",
"messages": [HumanMessage(content="messages")]
})
assertlen(prompt_value.messages) ==3
Why This Happens
Looking at the codebase, the MessagesPlaceholder class in doesn't respect the template format setting of the parent ChatPromptTemplate. It always uses the variable name directly without any template formatting.
I think at least that must be covered in documentation, because it was unexpected surprise to our team: we changed template engine and lost all conversation without any error raised on that. Only adding langchain specific test could help avoiding this case (what we actually did in our tests), but that's not what people expect from any library.
I think at least that must be covered in documentation, because it was unexpected surprise to our team: we changed template engine and lost all conversation without any error raised on that. Only adding langchain specific test could help avoiding this case (what we actually did in our tests), but that's not what people expect from any library.
I'm glad that I solved your problem so can you mark my answer my comment as a correct answer that solves your problem so that others can know the problem
Checked other resources
Example Code
Error Message and Stack Trace (if applicable)
No response
Description
Hi Langchain,
I found strange inconsistency with
ChatPromptTemplate
withtemplate_format='mustache'
, see attached code. I expected that placeholder formatting will use similar template format engine, but it remains f-string.Test code assertion would fail, and only replacing
{{messages}}
with{messages}
would fix it.If this is intentionally, then probably it should be reflected in documentation, which is not currently.
Thank you.
System Info
System Information
Package Information
Optional packages not installed
Other Dependencies
The text was updated successfully, but these errors were encountered: