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

Fix template variable handling in ChatPromptTemplate BaseMessage objects #29034 #29057

Closed
wants to merge 4 commits into from

Conversation

SANTHOSH-SACHIN
Copy link

Title: core: Fix template variable handling in ChatPromptTemplate BaseMessage objects #29034

Description:
Fixes a bug in ChatPromptTemplate where template variables in BaseMessage objects (like SystemMessage) were not being processed. Previously, template variables in direct BaseMessage instances would be left unformatted, while variables in MessagePromptTemplate instances were processed correctly. This PR modifies the format_messages method to properly handle template variables in all message types.

Example of the bug:

template = ChatPromptTemplate.from_messages([
SystemMessage("Answer in {language}"), # Variable not processed
HumanMessagePromptTemplate.from_template("Hello") # Works fine
])
Before fix:

result = template.invoke({"language": "English"})

Output: SystemMessage(content='Answer in {language}') # Variable not replaced

After fix:

result = template.invoke({"language": "English"})

Output: SystemMessage(content='Answer in English') # Variable properly replaced

The fix adds template variable processing for BaseMessage objects while maintaining backward compatibility and existing functionality for other message types.

Key changes:

Modified format_messages method to detect and process template variables in BaseMessage content
Added template variable processing using PromptTemplate for BaseMessage instances
Implemented proper error handling for missing variables or formatting failures
Added comprehensive tests for template variable processing in all message types
Maintained backward compatibility with existing usage patterns
Implementation details:

Uses PromptTemplate for consistent variable formatting
Only attempts formatting when template variables are detected
Preserves original message if formatting fails
Maintains all existing functionality for MessagePromptTemplate instances
@baskaryan, @efriis, @eyurtsev, @ccurme

@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Jan 7, 2025
Copy link

vercel bot commented Jan 7, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
langchain ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 7, 2025 6:14am

@dosubot dosubot bot added Ɑ: core Related to langchain-core 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature labels 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 Ɑ: core Related to langchain-core size:M This PR changes 30-99 lines, ignoring generated files.
Projects
Status: Closed
Development

Successfully merging this pull request may close these issues.

1 participant