From 9efb7aa0393271d61339cb3e85681858e295a2b0 Mon Sep 17 00:00:00 2001 From: AsherLucas Date: Thu, 29 Oct 2026 00:24:28 +0000 Subject: [PATCH] fix(model): use each non-dict placeholder item instead of the whole list --- langfuse/model.py | 2 +- tests/unit/test_prompt.py | 2 +- tests/unit/test_prompt_compilation.py | 31 +++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/langfuse/model.py b/langfuse/model.py index 30c6fe0f3..194c663aa 100644 --- a/langfuse/model.py +++ b/langfuse/model.py @@ -359,7 +359,7 @@ def compile( compiled_messages.append( ChatMessageDict( role="NOT_GIVEN", - content=str(placeholder_value), + content=str(msg), ) ) no_role_content_in_placeholder = f"Placeholder '{placeholder_name}' should contain a list of chat messages with 'role' and 'content' fields. Appended as string." diff --git a/tests/unit/test_prompt.py b/tests/unit/test_prompt.py index eadfb8221..c0e4bc34b 100644 --- a/tests/unit/test_prompt.py +++ b/tests/unit/test_prompt.py @@ -87,7 +87,7 @@ 4, [ "You are a helpful assistant", - "['invalid message', {'role': 'user', 'content': 'valid message'}]", + "invalid message", "valid message", "Help me with coding", ], diff --git a/tests/unit/test_prompt_compilation.py b/tests/unit/test_prompt_compilation.py index 1b96a14dd..74e4ea922 100644 --- a/tests/unit/test_prompt_compilation.py +++ b/tests/unit/test_prompt_compilation.py @@ -932,3 +932,34 @@ def test_tool_calls_preservation_in_message_placeholder(): # Final user message with compiled variable assert compiled_messages[4]["role"] == "user" assert compiled_messages[4]["content"] == "Help me with weather inquiry" + + +def test_compile_non_dict_placeholder_items_use_each_item(): + """Non-dict placeholder items should each be appended as their own string. + + Regression test for langfuse/langfuse-python#1971: previously every + non-dict item produced the same message containing the whole list. + """ + prompt_client = ChatPromptClient( + Prompt_Chat( + type="chat", + name="non_dict_placeholder_test", + version=1, + config={}, + tags=[], + labels=[], + prompt=[ + {"type": "chatmessage", "role": "system", "content": "hi {{x}}"}, + {"type": "placeholder", "name": "history"}, + ], + ) + ) + + compiled_messages = prompt_client.compile(x="1", history=["a", "b"]) + + assert len(compiled_messages) == 3 + assert compiled_messages[0] == {"role": "system", "content": "hi 1"} + assert compiled_messages[1]["role"] == "NOT_GIVEN" + assert compiled_messages[1]["content"] == "a" + assert compiled_messages[2]["role"] == "NOT_GIVEN" + assert compiled_messages[2]["content"] == "b"