-
Notifications
You must be signed in to change notification settings - Fork 143
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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
langfuse/model.py
Outdated
for chat_message in messages_with_placeholders_replaced | ||
if "role" in chat_message and "content" in chat_message | ||
] | ||
|
||
def get_langchain_prompt(self, **kwargs): |
There was a problem hiding this comment.
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": |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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.
Important
Adds support for chat message placeholders in langfuse-python SDK, enabling dynamic templating in prompts.
ChatMessageWithPlaceholders
type inchat_message_with_placeholders.py
to handle both regular and placeholder messages.create_prompt_request.py
andchat_prompt.py
to useChatMessageWithPlaceholders
for prompt creation.PlaceholderMessage
model inplaceholder_message.py
for template variables.create_prompt
method inclient.py
to acceptChatMessageWithPlaceholders
.test_prompt.py
andtest_prompt_compilation.py
for placeholder scenarios and edge cases.This description was created by
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.
ChatMessageWithPlaceholders
union type inlangfuse/api/resources/prompts/types/chat_message_with_placeholders.py
supporting both regular chat messages and placeholder messagesChatMessageWithPlaceholders
instead ofChatMessage
inlangfuse/api/resources/prompts/types/create_prompt_request.py
PlaceholderMessage
Pydantic model inlangfuse/api/resources/prompts/types/placeholder_message.py
for template variablestests/test_prompt.py
for placeholder scenarios and edge cases