Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ChatPromptTemplate.format_messages does not handle f-strings inherited from BaseMessage, such as SystemMessage, etc #29034

Open
5 tasks done
TsinlingLi opened this issue Jan 5, 2025 · 1 comment
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature investigate Flagged for investigation.

Comments

@TsinlingLi
Copy link

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

from langchain_core.messages import HumanMessage, SystemMessage, AIMessage
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder

ChatPromptTemplate.from_messages([
SystemMessage("You are a helpful assistant. Answer all questions to the best of your ability in {language}"),
MessagesPlaceholder(variable_name="messages")
]).invoke({"language": "English", "messages": [HumanMessage("hello")]})

Error Message and Stack Trace (if applicable)

Unexpected results

Description

result,language Not replaced:
ChatPromptValue(messages=[SystemMessage(content='You are a helpful assistant. Answer all questions to the best of your ability in {language}', additional_kwargs={}, response_metadata={}), HumanMessage(content='hello', additional_kwargs={}, response_metadata={})])

because,No processing of objects integrated from BaseMessage

class ChatPromptTemplate(ChatPromptTemplate):
def format_messages(self, **kwargs: Any) -> list[BaseMessage]:
kwargs = self._merge_partial_and_user_variables(**kwargs)
result = []
for message_template in self.messages:
if isinstance(message_template, BaseMessage):
# No processing of objects integrated from BaseMessage
result.extend([message_template])
elif isinstance(
message_template, (BaseMessagePromptTemplate, BaseChatPromptTemplate)
):
message = message_template.format_messages(**kwargs)
result.extend(message)
else:
msg = f"Unexpected input: {message_template}"
raise ValueError(msg)
return result

System Info

python==3.12.7
langchain==0.3.14

@langcarl langcarl bot added the investigate Flagged for investigation. label Jan 5, 2025
@dosubot dosubot bot added the 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature label Jan 5, 2025
@sirine707
Copy link

sirine707 commented Jan 6, 2025

Hi there ,To fix this, you need to modify the format_messages method to process f-strings here's the updated code let me know if iam not correct !

class ChatPromptTemplate(ChatPromptTemplate):
    def format_messages(self, **kwargs: Any) -> list[BaseMessage]:
        kwargs = self._merge_partial_and_user_variables(**kwargs)
        result = []
        for message_template in self.messages:
            if isinstance(message_template, BaseMessage):
                # Process f-strings in BaseMessage objects
                formatted_content = message_template.content.format(**kwargs)
                message_template.content = formatted_content
                result.extend([message_template])
            elif isinstance(
                    message_template, (BaseMessagePromptTemplate, BaseChatPromptTemplate)
            ):
                message = message_template.format_messages(**kwargs)
                result.extend(message)
            else:
                msg = f"Unexpected input: {message_template}"
                raise ValueError(msg)
        return result

SANTHOSH-SACHIN added a commit to SANTHOSH-SACHIN/langchain that referenced this issue Jan 7, 2025
SANTHOSH-SACHIN added a commit to SANTHOSH-SACHIN/langchain that referenced this issue Jan 7, 2025
SANTHOSH-SACHIN added a commit to SANTHOSH-SACHIN/langchain that referenced this issue Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature investigate Flagged for investigation.
Projects
None yet
Development

No branches or pull requests

2 participants