fix(prompt): preserve langchain placeholder messages - #1840
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: arpan sahu <28574248+arpansahu@users.noreply.github.com>
|
|
|
|
||
| for key in ("name", "tool_call_id", "tool_calls", "invalid_tool_calls"): | ||
| value = getattr(msg, key, None) | ||
| if value: |
There was a problem hiding this comment.
When a LangChain message stores provider or subtype data outside name, tool_call_id, tool_calls, and invalid_tool_calls, this allowlist removes that data from the compiled history, causing downstream model calls to receive incomplete function or tool context.
Knowledge Base Used: Prompt retrieval, compilation, and caching
Prompt To Fix With AI
This is a comment left during a code review.
Path: langfuse/model.py
Line: 427-430
Comment:
**Message metadata is dropped**
When a LangChain message stores provider or subtype data outside `name`, `tool_call_id`, `tool_calls`, and `invalid_tool_calls`, this allowlist removes that data from the compiled history, causing downstream model calls to receive incomplete function or tool context.
**Knowledge Base Used:** [Prompt retrieval, compilation, and caching](https://app.greptile.com/personal-org-4986/-/custom-context/knowledge-base/langfuse/langfuse-python/-/docs/prompt-retrieval-compilation-and-cache.md)
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.|
Disclosure: this contribution was prepared with AI assistance (GitHub Copilot CLI), and The defect, the fix and the regression test were verified locally: the new test fails on If your project would prefer not to take AI-assisted contributions, or you would rather this |
Problem
ChatPromptClient.compile()supports chat placeholders and already preserves dict messages, but it treats LangChainBaseMessageinstances passed to a placeholder as invalid objects. Passing a natural LangChain history like[HumanMessage(...), AIMessage(...)]therefore emits a warning and appends the stringified entire history with roleNOT_GIVEN, losing message roles and content structure.Reproducer
Added
test_langchain_messages_in_message_placeholder_are_preserved, which compiles a chat prompt placeholder withHumanMessage(content="Hello {{name}}")andAIMessage(content="Hi {{name}}").Before the fix, the regression failed because the compiled output contained
role="NOT_GIVEN"and a stringified placeholder list instead of separate user/assistant messages.Fix
Add placeholder-message compilation for LangChain-style message objects by reading their
content, mapping LangChain messagetypevalues such ashumanandaito chat roles, compiling string content variables, and preserving optional tool-related fields.Testing
python -m pytest tests\unit\test_prompt_compilation.py::test_langchain_messages_in_message_placeholder_are_preserved -qfailed with the placeholder warning and incorrectNOT_GIVEN/stringified output..\.venv\Scripts\python.exe -m pytest tests\unit\test_prompt_compilation.py::test_langchain_messages_in_message_placeholder_are_preserved -qpassed..\.venv\Scripts\python.exe -m pytest tests\unit\test_prompt_compilation.py -qpassed (33 passed)..\.venv\Scripts\python.exe -m ruff format langfuse\model.py tests\unit\test_prompt_compilation.py --checkpassed..\.venv\Scripts\python.exe -m ruff check langfuse\model.py tests\unit\test_prompt_compilation.pypassed.Full
tests\unithas pre-existing Windows/env failures unrelated to this change, previously observed in serializer path assertions, prompt atexit subprocess environment, and prompt mock setup.Greptile Summary
This PR adds support for expanding LangChain message objects supplied to chat placeholders and tests variable interpolation for human and AI messages.
HumanMessageandAIMessageplaceholder history.Confidence Score: 4/5
The PR should not merge until LangChain message metadata outside the current allowlist is preserved during placeholder compilation.
The new object-conversion path can silently remove provider-specific function or tool context before compiled messages are sent to a model.
Files Needing Attention: langfuse/model.py
Prompt To Fix All With AI
Reviews (1): Last reviewed commit: "fix(prompt): preserve langchain placehol..." | Re-trigger Greptile
Context used: