ChatPromptTemplate.format_messages does not handle f-strings inherited from BaseMessage, such as SystemMessage, etc #29034
Labels
🤖:bug
Related to a bug, vulnerability, unexpected error with an existing feature
investigate
Flagged for investigation.
Checked other resources
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
The text was updated successfully, but these errors were encountered: