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

Template variable handling in ChatPromptTemplate #29034 #29056

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix #29034
  • Loading branch information
SANTHOSH-SACHIN committed Jan 7, 2025
commit 250342bb21c3ded0d477b13a0fe83930ff54b86e
32 changes: 26 additions & 6 deletions libs/core/langchain_core/prompts/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -1219,17 +1219,37 @@
"""
kwargs = self._merge_partial_and_user_variables(**kwargs)
result = []

for message_template in self.messages:
if isinstance(message_template, BaseMessage):
result.extend([message_template])
elif isinstance(
message_template, (BaseMessagePromptTemplate, BaseChatPromptTemplate)
):
# Check if the content contains any template variables
if any(char in message_template.content for char in "{}"):
try:
# Create a temporary template from the message content
temp_template = PromptTemplate.from_template(
message_template.content
)
# Format the content with provided variables
formatted_content = temp_template.format(**kwargs)
# Create a new message with formatted content
formatted_message = type(message_template)(
content=formatted_content,
additional_kwargs=message_template.additional_kwargs.copy()
)
result.append(formatted_message)
except (KeyError, ValueError):
# If formatting fails, use original message
result.append(message_template)
else:
# If no template variables, use original message
result.append(message_template)

elif isinstance(message_template, (BaseMessagePromptTemplate, BaseChatPromptTemplate)):

Check failure on line 1247 in libs/core/langchain_core/prompts/chat.py

View workflow job for this annotation

GitHub Actions / cd libs/core / make lint #3.11

Ruff (E501)

langchain_core/prompts/chat.py:1247:89: E501 Line too long (99 > 88)

Check failure on line 1247 in libs/core/langchain_core/prompts/chat.py

View workflow job for this annotation

GitHub Actions / cd libs/core / make lint #3.12

Ruff (E501)

langchain_core/prompts/chat.py:1247:89: E501 Line too long (99 > 88)

Check failure on line 1247 in libs/core/langchain_core/prompts/chat.py

View workflow job for this annotation

GitHub Actions / cd libs/core / make lint #3.13

Ruff (E501)

langchain_core/prompts/chat.py:1247:89: E501 Line too long (99 > 88)

Check failure on line 1247 in libs/core/langchain_core/prompts/chat.py

View workflow job for this annotation

GitHub Actions / cd libs/core / make lint #3.10

Ruff (E501)

langchain_core/prompts/chat.py:1247:89: E501 Line too long (99 > 88)

Check failure on line 1247 in libs/core/langchain_core/prompts/chat.py

View workflow job for this annotation

GitHub Actions / cd libs/core / make lint #3.9

Ruff (E501)

langchain_core/prompts/chat.py:1247:89: E501 Line too long (99 > 88)
message = message_template.format_messages(**kwargs)
result.extend(message)
else:
msg = f"Unexpected input: {message_template}"
raise ValueError(msg)
raise ValueError(f"Unexpected input: {message_template}")

Check failure on line 1251 in libs/core/langchain_core/prompts/chat.py

View workflow job for this annotation

GitHub Actions / cd libs/core / make lint #3.11

Ruff (EM102)

langchain_core/prompts/chat.py:1251:34: EM102 Exception must not use an f-string literal, assign to variable first

Check failure on line 1251 in libs/core/langchain_core/prompts/chat.py

View workflow job for this annotation

GitHub Actions / cd libs/core / make lint #3.12

Ruff (EM102)

langchain_core/prompts/chat.py:1251:34: EM102 Exception must not use an f-string literal, assign to variable first

Check failure on line 1251 in libs/core/langchain_core/prompts/chat.py

View workflow job for this annotation

GitHub Actions / cd libs/core / make lint #3.13

Ruff (EM102)

langchain_core/prompts/chat.py:1251:34: EM102 Exception must not use an f-string literal, assign to variable first

Check failure on line 1251 in libs/core/langchain_core/prompts/chat.py

View workflow job for this annotation

GitHub Actions / cd libs/core / make lint #3.10

Ruff (EM102)

langchain_core/prompts/chat.py:1251:34: EM102 Exception must not use an f-string literal, assign to variable first

Check failure on line 1251 in libs/core/langchain_core/prompts/chat.py

View workflow job for this annotation

GitHub Actions / cd libs/core / make lint #3.9

Ruff (EM102)

langchain_core/prompts/chat.py:1251:34: EM102 Exception must not use an f-string literal, assign to variable first

return result

async def aformat_messages(self, **kwargs: Any) -> list[BaseMessage]:
Expand Down
Loading