langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications

Home Page:https://python.langchain.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Input variables is ignored even though passed in when MessagesPlaceholder.optional=True

LinboYuan opened this issue · comments

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code


memory_key = "chat_context"

prompt = ChatPromptTemplate(
    messages=[
        MessagesPlaceholder(variable_name=memory_key, optional=True),
        HumanMessagePromptTemplate.from_template("{question}")
    ]
)

memory = ConversationBufferMemory(memory_key=memory_key, return_messages=True)
conversation = LLMChain(
    llm=llm,
    verbose=True,
    memory=memory,
    prompt=prompt
)


user_input = input("Me: ")
while user_input != "exit":
    print(conversation.invoke({"question": user_input}))
    user_input = input("Me: ")

### Error Message and Stack Trace (if applicable)

![1](https://github.com/langchain-ai/langchain/assets/12031633/419f2b04-a259-4cec-b66a-2d9ce2d5e112)
![2](https://github.com/langchain-ai/langchain/assets/12031633/462381c0-83dc-4ec2-9d79-5662c3c7d292)


### Description

as the title described, when MessagesPlaceholder.optional=True, the input variables will be ignored, even though I passed the arguments in. **I suppose this to be a bug, because it makes no sense when init a MessagesPlaceholder and optional set True.**

or **if this is by design, could you pls share some original design intentions or use cases.**

![3](https://github.com/langchain-ai/langchain/assets/12031633/893cf930-7cdb-40d9-8341-6fa043ce90ed)


### System Info

Name: langchain-core
Version: 0.1.45
Summary: Building applications with LLMs through composability
Home-page: https://github.com/langchain-ai/langchain
Author: 
Author-email: 
License: MIT
Location: /Users/linbo.yuan/Library/Python/3.9/lib/python/site-packages
Requires: PyYAML, tenacity, pydantic, langsmith, jsonpatch, packaging
Required-by: langchain, langchain-text-splitters, langchain-openai, langchain-community

The problem arises when MessagePlaceHolder is set as optional as according to this code, the input variable name set by MessagePlaceHolder is excluded from the ChatPromptTemplate. Consequently, when you invoke the chain, the MessagePlaceHolder variable is not filled in by the ConversationBufferMemory.