Skip to content

feat(prompts): chat message placeholders #1222

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

Open
wants to merge 18 commits into
base: main
Choose a base branch
from

Conversation

nimarb
Copy link

@nimarb nimarb commented Jun 24, 2025

Important

Adds support for chat message placeholders in langfuse-python SDK, enabling dynamic templating in prompts.

  • Behavior:
    • Introduces ChatMessageWithPlaceholders type in chat_message_with_placeholders.py to handle both regular and placeholder messages.
    • Updates create_prompt_request.py and chat_prompt.py to use ChatMessageWithPlaceholders for prompt creation.
    • Adds PlaceholderMessage model in placeholder_message.py for template variables.
  • Client:
    • Modifies create_prompt method in client.py to accept ChatMessageWithPlaceholders.
  • Tests:
    • Adds tests in test_prompt.py and test_prompt_compilation.py for placeholder scenarios and edge cases.

This description was created by Ellipsis for e295821. You can customize this summary. It will automatically update as commits are pushed.


Greptile Summary

Disclaimer: Experimental PR review

Implements chat message placeholder functionality in the langfuse-python SDK, enabling dynamic templating in chat prompts. This feature is critical for scenarios like injecting conversation history or examples into prompts.

  • Added ChatMessageWithPlaceholders union type in langfuse/api/resources/prompts/types/chat_message_with_placeholders.py supporting both regular chat messages and placeholder messages
  • Modified prompt creation to use ChatMessageWithPlaceholders instead of ChatMessage in langfuse/api/resources/prompts/types/create_prompt_request.py
  • Added new PlaceholderMessage Pydantic model in langfuse/api/resources/prompts/types/placeholder_message.py for template variables
  • Added comprehensive test coverage in tests/test_prompt.py for placeholder scenarios and edge cases

@nimarb nimarb marked this pull request as ready for review June 25, 2025 16:47
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

15 files reviewed, 1 comment
Edit PR Review Bot Settings | Greptile

for chat_message in messages_with_placeholders_replaced
if "role" in chat_message and "content" in chat_message
]

def get_langchain_prompt(self, **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to cover langchain prompts as well where users are not compiling prompts with langfuse but with langchain. In this case, they might want to only fill in placeholders and keep variables untouched and then call get_langchain_prompt.

We can either add a separate method that is only handling replacing placeholders, or we rely on compileWithPlaceholders(variables={}, placeholders=...) but we need a test here to make sure this works as expected

) -> None:
"""Backward-compatible setter for raw prompt structure."""
for p in prompt:
if hasattr(p, "type") and hasattr(p, "name") and p.type == "placeholder":
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using hasattr(p, ...) in the prompt setter may fail for plain dict inputs which lack attribute access. Consider checking if p is a dict (e.g. using 'if isinstance(p, dict) and "type" in p') to support both dicts and objects.

err_placeholder_not_list = f"Placeholder '{chat_message['name']}' must contain a list of chat messages, got {type(placeholder_messages)}"
raise ValueError(err_placeholder_not_list)
else:
compiled_messages.append(
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unresolved placeholders are logged as warnings and a placeholder dict (with only 'type' and 'name') is appended. This may later cause KeyError in methods expecting 'role' and 'content'. Consider either raising an error or handling them explicitly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants