Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 17 additions & 2 deletions langfuse/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,18 @@ def compile(
Union[
Dict[str, Any], ChatMessageDict, ChatMessageWithPlaceholdersDict_Placeholder
]
]:
return self._compile(warn_on_unresolved_placeholders=True, **kwargs)

def _compile(
self,
*,
warn_on_unresolved_placeholders: bool,
**kwargs: Union[str, Any],
) -> Sequence[
Union[
Dict[str, Any], ChatMessageDict, ChatMessageWithPlaceholdersDict_Placeholder
]
]:
"""Compile the prompt with placeholders and variables.

Expand Down Expand Up @@ -381,7 +393,7 @@ def compile(
compiled_messages.append(chat_message)
unresolved_placeholders.append(chat_message["name"]) # type: ignore

if unresolved_placeholders:
if warn_on_unresolved_placeholders and unresolved_placeholders:
unresolved_placeholders_message = f"Placeholders {unresolved_placeholders} have not been resolved. Pass them as keyword arguments to compile()."
langfuse_logger.warning(unresolved_placeholders_message)

Expand Down Expand Up @@ -446,7 +458,10 @@ def get_langchain_prompt(
List of messages in the format expected by Langchain's ChatPromptTemplate:
(role, content) tuples for regular messages or MessagesPlaceholder objects for unresolved placeholders.
"""
compiled_messages = self.compile(**kwargs)
compiled_messages = self._compile(
warn_on_unresolved_placeholders=False,
**kwargs,
)
langchain_messages: List[Union[Tuple[str, str], Any]] = []

for msg in compiled_messages:
Expand Down
13 changes: 9 additions & 4 deletions tests/unit/test_prompt_compilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,8 @@ def test_chat_prompt_with_placeholders_langchain(self):

def test_get_langchain_prompt_with_unresolved_placeholders(self):
"""Test that unresolved placeholders become MessagesPlaceholder objects."""
from unittest.mock import patch

from langfuse.api import Prompt_Chat
from langfuse.model import ChatPromptClient

Expand All @@ -826,10 +828,13 @@ def test_get_langchain_prompt_with_unresolved_placeholders(self):
)

# Call get_langchain_prompt without resolving placeholder
langchain_messages = prompt_client.get_langchain_prompt(
role="helpful",
task="coding",
)
with patch("langfuse.logger.langfuse_logger.warning") as mock_warning:
langchain_messages = prompt_client.get_langchain_prompt(
role="helpful",
task="coding",
)

mock_warning.assert_not_called()

# Should have 3 items: system message, MessagesPlaceholder, user message
assert len(langchain_messages) == 3
Expand Down